use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.
the class EvictionTest method testContainsKeyShouldDelayEviction.
/**
* Test for issue 614
*/
@Test
public void testContainsKeyShouldDelayEviction() throws InterruptedException {
String mapName = randomMapName();
final int waitSeconds = 2;
Config config = getConfig();
config.getMapConfig(mapName).setMaxIdleSeconds(30);
HazelcastInstance instance = createHazelcastInstance(config);
final IMap<Object, Object> map = instance.getMap(mapName);
map.put(1, 1);
sleepSeconds(waitSeconds);
EntryView<Object, Object> entryView = map.getEntryView(1);
final long lastAccessTime = entryView.getLastAccessTime();
//1. Shift lastAccessTime.
map.containsKey(1);
entryView = map.getEntryView(1);
final long lastAccessTimeAfterContainsOperation = entryView.getLastAccessTime();
//2. Expecting lastAccessTime to be shifted by containsKey operation.
final long diffSecs = TimeUnit.MILLISECONDS.toSeconds(lastAccessTimeAfterContainsOperation - lastAccessTime);
//3. So there should be a diff at least waitSeconds.
final String failureMessage = format("Diff seconds %d, wait seconds %d", diffSecs, waitSeconds);
assertTrue(failureMessage, diffSecs >= waitSeconds);
}
use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.
the class EvictionTest method testIssue585ZeroTTLShouldPreventEvictionWithSet.
@Test
public void testIssue585ZeroTTLShouldPreventEvictionWithSet() throws InterruptedException {
Config config = getConfig();
config.getGroupConfig().setName("testIssue585ZeroTTLShouldPreventEvictionWithSet");
NearCacheConfig nearCacheConfig = new NearCacheConfig();
config.getMapConfig("default").setNearCacheConfig(nearCacheConfig);
int n = 1;
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(n);
HazelcastInstance h = factory.newHazelcastInstance(config);
IMap<String, String> map = h.getMap("testIssue585ZeroTTLShouldPreventEvictionWithSet");
map.set("key", "value", 1, TimeUnit.SECONDS);
map.set("key", "value2", 0, TimeUnit.SECONDS);
sleepSeconds(2);
assertEquals("value2", map.get("key"));
}
use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.
the class EvictionTest method testIssue304EvictionDespitePut.
@Test
public void testIssue304EvictionDespitePut() throws InterruptedException {
Config config = getConfig();
config.getGroupConfig().setName("testIssue304EvictionDespitePut");
MapConfig mapConfig = config.getMapConfig("testIssue304EvictionDespitePut");
mapConfig.setMaxIdleSeconds(5);
HazelcastInstance hazelcastInstance = createHazelcastInstance(config);
IMap<String, Long> map = hazelcastInstance.getMap("testIssue304EvictionDespitePut");
final AtomicInteger evictCount = new AtomicInteger(0);
map.addEntryListener(new EntryAdapter<String, Long>() {
public void entryEvicted(EntryEvent<String, Long> event) {
evictCount.incrementAndGet();
}
}, true);
String key = "key";
for (int i = 0; i < 5; i++) {
map.put(key, System.currentTimeMillis());
sleepMillis(500);
}
assertEquals(evictCount.get(), 0);
assertNotNull(map.get(key));
}
use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.
the class EvictionTest method testEvictionSpeedTest.
// current eviction check period is 1 second.
// about 30000 records can be put in one second, so the size should be adapted
@Test
public void testEvictionSpeedTest() throws InterruptedException {
final int k = 3;
final int size = 10000;
final CountDownLatch latch = new CountDownLatch(k);
final String mapName = "testEvictionSpeedTest";
Config config = getConfig();
final MapConfig mapConfig = config.getMapConfig(mapName);
mapConfig.setEvictionPolicy(EvictionPolicy.LRU);
mapConfig.setEvictionPercentage(25);
final MaxSizeConfig maxSizeConfig = new MaxSizeConfig();
maxSizeConfig.setMaxSizePolicy(MaxSizeConfig.MaxSizePolicy.PER_NODE);
maxSizeConfig.setSize(size);
mapConfig.setMaxSizeConfig(maxSizeConfig);
TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(k);
final HazelcastInstance[] instances = factory.newInstances(config);
final AtomicBoolean success = new AtomicBoolean(true);
new Thread() {
final IMap map = instances[0].getMap(mapName);
public void run() {
try {
Thread.sleep(1000);
while (latch.getCount() != 0) {
try {
int mapSize = map.size();
if (mapSize > (size * k + size * k * 10 / 100)) {
success.set(false);
break;
}
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
for (int i = 0; i < k; i++) {
final IMap<String, Integer> map = instances[i].getMap(mapName);
new Thread() {
public void run() {
for (int j = 0; j < size; j++) {
map.put(k + "-" + j, j);
}
latch.countDown();
}
}.start();
}
assertTrue(latch.await(10, TimeUnit.MINUTES));
assertTrue(success.get());
}
use of com.hazelcast.core.HazelcastInstance in project hazelcast by hazelcast.
the class EvictionTest method createMapWithReadBackupDataEnabled.
private IMap<Integer, Integer> createMapWithReadBackupDataEnabled(int maxIdleSeconds) {
final String mapName = randomMapName();
Config config = getConfig();
config.getMapConfig(mapName).setMaxIdleSeconds(maxIdleSeconds).setReadBackupData(true);
TestHazelcastInstanceFactory hazelcastInstanceFactory = createHazelcastInstanceFactory(2);
HazelcastInstance[] hazelcastInstances = hazelcastInstanceFactory.newInstances(config);
return hazelcastInstances[0].getMap(mapName);
}
Aggregations