Search in sources :

Example 1 with MapStoreAdapter

use of com.hazelcast.map.MapStoreAdapter in project hazelcast by hazelcast.

the class MapTransactionTest method testGetForUpdate_LoadsKeyFromMapLoader_whenKeyExistsInDb.

@Test
public void testGetForUpdate_LoadsKeyFromMapLoader_whenKeyExistsInDb() {
    final String mapName = randomMapName();
    final String valueFromDB = randomString();
    final MapStoreAdapter mock = mock(MapStoreAdapter.class);
    when(mock.load(anyObject())).thenReturn(valueFromDB);
    Config config = new Config();
    MapStoreConfig storeConfig = new MapStoreConfig();
    storeConfig.setEnabled(true).setImplementation(mock);
    config.getMapConfig(mapName).setMapStoreConfig(storeConfig);
    HazelcastInstance instance = createHazelcastInstance(config);
    instance.executeTransaction(new TransactionalTask<Object>() {

        @Override
        public Object execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> map = context.getMap(mapName);
            Object value = map.getForUpdate(1);
            assertEquals(valueFromDB, value);
            verify(mock, times(1)).load(anyObject());
            return null;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) MapStoreAdapter(com.hazelcast.map.MapStoreAdapter) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) Mockito.anyObject(org.mockito.Mockito.anyObject) MapStoreConfig(com.hazelcast.config.MapStoreConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 2 with MapStoreAdapter

use of com.hazelcast.map.MapStoreAdapter in project hazelcast by hazelcast.

the class MapTransactionTest method testGet_LoadsKeyFromMapLoader_whenKeyExistsInDb.

@Test
public void testGet_LoadsKeyFromMapLoader_whenKeyExistsInDb() {
    final String mapName = randomMapName();
    final String valueFromDB = randomString();
    final MapStoreAdapter mock = mock(MapStoreAdapter.class);
    when(mock.load(anyObject())).thenReturn(valueFromDB);
    Config config = getConfig();
    MapStoreConfig storeConfig = new MapStoreConfig();
    storeConfig.setEnabled(true).setImplementation(mock);
    config.getMapConfig(mapName).setMapStoreConfig(storeConfig);
    HazelcastInstance instance = createHazelcastInstance(config);
    instance.executeTransaction(new TransactionalTask<Object>() {

        @Override
        public Object execute(TransactionalTaskContext context) throws TransactionException {
            TransactionalMap<Object, Object> map = context.getMap(mapName);
            Object value = map.get(1);
            assertEquals(valueFromDB, value);
            verify(mock, times(1)).load(anyObject());
            return null;
        }
    });
}
Also used : TransactionalMap(com.hazelcast.transaction.TransactionalMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) MapStoreAdapter(com.hazelcast.map.MapStoreAdapter) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) Mockito.anyObject(org.mockito.Mockito.anyObject) MapStoreConfig(com.hazelcast.config.MapStoreConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 3 with MapStoreAdapter

use of com.hazelcast.map.MapStoreAdapter in project hazelcast by hazelcast.

the class MapTransactionTest method testFailingMapStore.

@Test
public // TODO: @mm - Review following case...
void testFailingMapStore() throws TransactionException {
    final String map = "map";
    final String anotherMap = "anotherMap";
    Config config = getConfig();
    config.getMapConfig(map).setMapStoreConfig(new MapStoreConfig().setEnabled(true).setImplementation(new MapStoreAdapter() {

        public void store(Object key, Object value) {
            throw new ExpectedRuntimeException("Map store intentionally failed :) ");
        }
    }));
    final TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    final HazelcastInstance h1 = factory.newHazelcastInstance(config);
    final HazelcastInstance h2 = factory.newHazelcastInstance(config);
    try {
        h1.executeTransaction(options, new TransactionalTask<Boolean>() {

            public Boolean execute(TransactionalTaskContext context) throws TransactionException {
                assertNull(context.getMap(map).put("1", "value1"));
                assertNull(context.getMap(anotherMap).put("1", "value1"));
                return true;
            }
        });
        fail();
    } catch (ExpectedRuntimeException expected) {
    }
    assertNull(h2.getMap(map).get("1"));
    assertEquals("value1", h2.getMap(anotherMap).get("1"));
}
Also used : ExpectedRuntimeException(com.hazelcast.test.ExpectedRuntimeException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TransactionException(com.hazelcast.transaction.TransactionException) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) MapStoreAdapter(com.hazelcast.map.MapStoreAdapter) TransactionalTaskContext(com.hazelcast.transaction.TransactionalTaskContext) Mockito.anyObject(org.mockito.Mockito.anyObject) MapStoreConfig(com.hazelcast.config.MapStoreConfig) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 4 with MapStoreAdapter

use of com.hazelcast.map.MapStoreAdapter in project hazelcast by hazelcast.

the class MapLoaderTest method createMapLoader.

private MapStore<Integer, Integer> createMapLoader(final AtomicInteger loadAllCounter) {
    return new MapStoreAdapter<Integer, Integer>() {

        @Override
        public Map<Integer, Integer> loadAll(Collection<Integer> keys) {
            loadAllCounter.addAndGet(keys.size());
            Map<Integer, Integer> map = new HashMap<>();
            for (Integer key : keys) {
                map.put(key, key);
            }
            return map;
        }

        @Override
        public Integer load(Integer key) {
            loadAllCounter.incrementAndGet();
            return super.load(key);
        }
    };
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MapStoreAdapter(com.hazelcast.map.MapStoreAdapter) Collection(java.util.Collection)

Example 5 with MapStoreAdapter

use of com.hazelcast.map.MapStoreAdapter in project hazelcast by hazelcast.

the class ClientHazelcastRunningInForkJoinPoolTest method setup.

@Before
public void setup() {
    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    mapStoreConfig.setEnabled(true);
    mapStoreConfig.setImplementation(new MapStoreAdapter() {

        @Override
        public Object load(Object key) {
            sleepSeconds(1000);
            return super.load(key);
        }
    });
    MapConfig mapConfig = new MapConfig(slowLoadingMapName);
    mapConfig.setMapStoreConfig(mapStoreConfig);
    Config config = getConfig().addMapConfig(mapConfig);
    server = hazelcastFactory.newHazelcastInstance(config);
    client = hazelcastFactory.newHazelcastClient();
    serverMap = server.getMap(defaultMapName);
    clientMap = client.getMap(slowLoadingMapName);
}
Also used : Config(com.hazelcast.config.Config) MapConfig(com.hazelcast.config.MapConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) MapStoreAdapter(com.hazelcast.map.MapStoreAdapter) MapStoreConfig(com.hazelcast.config.MapStoreConfig) MapConfig(com.hazelcast.config.MapConfig) Before(org.junit.Before)

Aggregations

MapStoreAdapter (com.hazelcast.map.MapStoreAdapter)9 Config (com.hazelcast.config.Config)8 MapStoreConfig (com.hazelcast.config.MapStoreConfig)7 NightlyTest (com.hazelcast.test.annotation.NightlyTest)6 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)6 QuickTest (com.hazelcast.test.annotation.QuickTest)6 Test (org.junit.Test)6 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 TransactionException (com.hazelcast.transaction.TransactionException)5 TransactionalTaskContext (com.hazelcast.transaction.TransactionalTaskContext)5 Mockito.anyObject (org.mockito.Mockito.anyObject)5 TransactionalMap (com.hazelcast.transaction.TransactionalMap)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClientConfig (com.hazelcast.client.config.ClientConfig)1 MapConfig (com.hazelcast.config.MapConfig)1 NearCacheConfig (com.hazelcast.config.NearCacheConfig)1 NearCacheTestUtils.getBaseConfig (com.hazelcast.internal.nearcache.impl.NearCacheTestUtils.getBaseConfig)1 ExpectedRuntimeException (com.hazelcast.test.ExpectedRuntimeException)1 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)1 Collection (java.util.Collection)1