Search in sources :

Example 66 with IMap

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

the class MapLoaderTest method testNullKey_loadAllKeys.

@Ignore("See https://github.com/hazelcast/hazelcast/issues/11931")
@Test
public void testNullKey_loadAllKeys() {
    String name = "testNullIn_loadAllKeys";
    MapStoreConfig mapStoreConfig = new MapStoreConfig().setEnabled(true).setInitialLoadMode(MapStoreConfig.InitialLoadMode.LAZY).setImplementation(new MapLoader<String, String>() {

        @Override
        public String load(String key) {
            if (key.equals("1")) {
                return "1";
            }
            if (key.equals("2")) {
                return "2";
            }
            if (key.equals("3")) {
                return "3";
            }
            return null;
        }

        @Override
        public Map<String, String> loadAll(Collection keys) {
            Map<String, String> val = new HashMap<>();
            if (keys.contains("1")) {
                val.put("1", "1");
            }
            if (keys.contains("2")) {
                val.put("2", "2");
            }
            if (keys.contains("3")) {
                val.put("3", "3");
            }
            return val;
        }

        @Override
        public Iterable<String> loadAllKeys() {
            List<String> keys = new ArrayList<>();
            keys.add("1");
            keys.add(null);
            keys.add("3");
            return keys;
        }
    });
    Config config = getConfig();
    config.getMapConfig(name).setMapStoreConfig(mapStoreConfig);
    HazelcastInstance instance = createHazelcastInstance(config);
    IMap<String, String> map = instance.getMap(name);
    expectedException.expect(NullPointerException.class);
    expectedException.expectMessage(startsWith("Key loaded by a MapLoader cannot be null"));
    map.size();
}
Also used : MapConfig(com.hazelcast.config.MapConfig) IndexConfig(com.hazelcast.config.IndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) MapStoreConfig(com.hazelcast.config.MapStoreConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Collection(java.util.Collection) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) IMap(com.hazelcast.map.IMap) Ignore(org.junit.Ignore) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 67 with IMap

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

the class MapLoaderTest method testMapCanBeLoaded_whenLoadAllKeysThrowsExceptionFirstTime.

@Test(timeout = MINUTE)
public void testMapCanBeLoaded_whenLoadAllKeysThrowsExceptionFirstTime() {
    Config config = getConfig();
    MapLoader failingMapLoader = new FailingMapLoader();
    MapStoreConfig mapStoreConfig = new MapStoreConfig().setImplementation(failingMapLoader);
    MapConfig mapConfig = config.getMapConfig(getClass().getName()).setMapStoreConfig(mapStoreConfig);
    final ILogger logger = Logger.getLogger(LoggingLifecycleListener.class);
    HazelcastInstance[] hz = createHazelcastInstanceFactory(2).newInstances(config, 2);
    final IMap map = hz[0].getMap(mapConfig.getName());
    Throwable exception = null;
    try {
        map.get(generateKeyNotOwnedBy(hz[0]));
    } catch (Throwable e) {
        exception = e;
    }
    assertNotNull("Exception wasn't propagated", exception);
    // In the first map load, partitions are notified asynchronously
    // by the com.hazelcast.map.impl.MapKeyLoader.sendKeyLoadCompleted
    // method and also some partitions are notified twice.
    // Because of this, a subsequent map load might get completed with the
    // results of the first map load.
    // This is why a subsequent map load might fail with the exception from
    // a previous load. In this case, we need to try again.
    // An alternative would be to wait for all partitions to be notified by
    // the result from the first load before initiating a second load but
    // unfortunately we can't observe this as some partitions are completed
    // twice and we might just end up observing the first completion.
    assertTrueEventually(() -> {
        try {
            map.loadAll(true);
            assertEquals(1, map.size());
        } catch (IllegalStateException e) {
            logger.info("Map load observed result from a previous load, retrying...", e);
        }
    });
}
Also used : IMap(com.hazelcast.map.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MapLoader(com.hazelcast.map.MapLoader) MapConfig(com.hazelcast.config.MapConfig) IndexConfig(com.hazelcast.config.IndexConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) ILogger(com.hazelcast.logging.ILogger) MapStoreConfig(com.hazelcast.config.MapStoreConfig) MapConfig(com.hazelcast.config.MapConfig) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 68 with IMap

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

the class MapStoreTest method testMapGetAll.

@Test(timeout = 120000)
public void testMapGetAll() {
    final Map<String, String> _map = new HashMap<>();
    _map.put("key1", "value1");
    _map.put("key2", "value2");
    _map.put("key3", "value3");
    final AtomicBoolean loadAllCalled = new AtomicBoolean(false);
    final AtomicBoolean loadCalled = new AtomicBoolean(false);
    TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(2);
    Config config = getConfig();
    MapStoreConfig mapStoreConfig = new MapStoreConfig();
    mapStoreConfig.setEnabled(true);
    mapStoreConfig.setImplementation(new MapLoader<String, String>() {

        public String load(String key) {
            loadCalled.set(true);
            return _map.get(key);
        }

        public Map<String, String> loadAll(Collection<String> keys) {
            loadAllCalled.set(true);
            final HashMap<String, String> temp = new HashMap<>();
            for (String key : keys) {
                String value = _map.get(key);
                if (value != null) {
                    temp.put(key, value);
                }
            }
            return temp;
        }

        public Set<String> loadAllKeys() {
            return _map.keySet();
        }
    });
    String mapName = "default";
    config.getMapConfig(mapName).setMapStoreConfig(mapStoreConfig);
    HazelcastInstance instance = nodeFactory.newHazelcastInstance(config);
    nodeFactory.newHazelcastInstance(config);
    IMap<String, String> map = instance.getMap(mapName);
    final HashSet<String> keys = new HashSet<>(3);
    keys.add("key1");
    keys.add("key3");
    keys.add("key4");
    final Map<String, String> subMap = map.getAll(keys);
    assertEquals(2, subMap.size());
    assertEquals("value1", subMap.get("key1"));
    assertEquals("value3", subMap.get("key3"));
    assertTrue(loadAllCalled.get());
    assertFalse(loadCalled.get());
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) MapConfig(com.hazelcast.config.MapConfig) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Config(com.hazelcast.config.Config) MapStoreConfig(com.hazelcast.config.MapStoreConfig) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) IMap(com.hazelcast.map.IMap) HashSet(java.util.HashSet) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 69 with IMap

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

the class PostProcessingMapStoreTest method testEntryListenerIncludesTheProcessedValue_onPutAll.

@Test
public void testEntryListenerIncludesTheProcessedValue_onPutAll() {
    IMap<Integer, SampleObject> map = createInstanceAndGetMap();
    int count = 10;
    final CountDownLatch latch = new CountDownLatch(count);
    map.addEntryListener((EntryAddedListener<Integer, SampleObject>) event -> {
        assertEquals(event.getKey() + 1, event.getValue().version);
        latch.countDown();
    }, true);
    Map<Integer, SampleObject> localMap = new HashMap<>();
    for (int i = 0; i < count; i++) {
        localMap.put(i, new SampleObject(i));
    }
    map.putAll(localMap);
    assertOpenEventually(latch);
}
Also used : ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) RunWith(org.junit.runner.RunWith) Parameters(org.junit.runners.Parameterized.Parameters) HashMap(java.util.HashMap) PostProcessingMapStore(com.hazelcast.map.PostProcessingMapStore) MapStore(com.hazelcast.map.MapStore) MapConfig(com.hazelcast.config.MapConfig) Arrays.asList(java.util.Arrays.asList) After(org.junit.After) Map(java.util.Map) Before(org.junit.Before) UseParametersRunnerFactory(org.junit.runners.Parameterized.UseParametersRunnerFactory) Config(com.hazelcast.config.Config) HazelcastInstance(com.hazelcast.core.HazelcastInstance) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) HazelcastParametrizedRunner(com.hazelcast.test.HazelcastParametrizedRunner) Parameter(org.junit.runners.Parameterized.Parameter) EntryUpdatedListener(com.hazelcast.map.listener.EntryUpdatedListener) HazelcastTestSupport(com.hazelcast.test.HazelcastTestSupport) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) HazelcastParallelParametersRunnerFactory(com.hazelcast.test.HazelcastParallelParametersRunnerFactory) Test(org.junit.Test) Category(org.junit.experimental.categories.Category) EntryAddedListener(com.hazelcast.map.listener.EntryAddedListener) Serializable(java.io.Serializable) CountDownLatch(java.util.concurrent.CountDownLatch) EntryProcessor(com.hazelcast.map.EntryProcessor) MapStoreConfig(com.hazelcast.config.MapStoreConfig) Assert.assertEquals(org.junit.Assert.assertEquals) IMap(com.hazelcast.map.IMap) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 70 with IMap

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

the class PluggableMemoryInfoAccessorTest method testPluggedMemoryInfoAccessorUsed.

/**
 * Used {@link ZeroMemoryInfoAccessor} to evict every put, map should not contain any entry after this test run.
 */
@Test
public void testPluggedMemoryInfoAccessorUsed() throws Exception {
    Config config = getConfig();
    MapConfig mapConfig = config.getMapConfig("test");
    EvictionConfig evictionConfig = mapConfig.getEvictionConfig();
    evictionConfig.setEvictionPolicy(LFU).setMaxSizePolicy(MaxSizePolicy.FREE_HEAP_PERCENTAGE).setSize(50);
    HazelcastInstance node = createHazelcastInstance(config);
    IMap map = node.getMap("test");
    for (int i = 0; i < 1000; i++) {
        map.put(i, i);
    }
    assertEquals(0, map.size());
}
Also used : IMap(com.hazelcast.map.IMap) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) MapConfig(com.hazelcast.config.MapConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) EvictionConfig(com.hazelcast.config.EvictionConfig) MapConfig(com.hazelcast.config.MapConfig) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

IMap (com.hazelcast.map.IMap)292 Test (org.junit.Test)259 QuickTest (com.hazelcast.test.annotation.QuickTest)237 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)228 HazelcastInstance (com.hazelcast.core.HazelcastInstance)139 Config (com.hazelcast.config.Config)103 HazelcastTestSupport.randomString (com.hazelcast.test.HazelcastTestSupport.randomString)82 Map (java.util.Map)73 CountDownLatch (java.util.concurrent.CountDownLatch)65 MapStoreConfig (com.hazelcast.config.MapStoreConfig)54 Category (org.junit.experimental.categories.Category)51 Assert.assertEquals (org.junit.Assert.assertEquals)50 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)48 HashMap (java.util.HashMap)48 Collection (java.util.Collection)41 RunWith (org.junit.runner.RunWith)41 MapConfig (com.hazelcast.config.MapConfig)36 Set (java.util.Set)34 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)33 AssertTask (com.hazelcast.test.AssertTask)32