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();
}
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);
}
});
}
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());
}
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);
}
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());
}
Aggregations