/* github issue 585 */ @Test public void testIssue585ZeroTTLShouldPreventEvictionWithSet() throws InterruptedException { Config config = new Config(); 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); Thread.sleep(2000); assertEquals("value2", map.get("key")); h.getLifecycleService().shutdown(); }
@Test public void testPutWithTwoMember() throws Exception { Config config = new Config(); HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config); HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config); assertEquals(2, h1.getCluster().getMembers().size()); assertEquals(2, h2.getCluster().getMembers().size()); IMap imap1 = h1.getMap("default"); IMap imap2 = h2.getMap("default"); assertEquals(0, imap1.size()); assertEquals(0, imap2.size()); CMap cmap1 = getCMap(h1, "default"); CMap cmap2 = getCMap(h2, "default"); assertNotNull(cmap1); assertNotNull(cmap2); Object key = "1"; Object value = "value"; Data dKey = toData(key); Data dValue = toData(value); imap1.put(key, value, 5, TimeUnit.SECONDS); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); Record record1 = cmap1.getRecord(dKey); Record record2 = cmap2.getRecord(dKey); long now = System.currentTimeMillis(); long millisLeft1 = record1.getExpirationTime() - now; long millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 5000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 5000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); assertEquals(dValue, record1.getValueData()); assertEquals(dValue, record2.getValueData()); imap1.set("2", "value2", 5, TimeUnit.SECONDS); assertEquals("value2", imap1.get("2")); assertEquals("value2", imap2.get("2")); Thread.sleep(6000); assertNull(imap1.get("2")); assertNull(imap2.get("2")); now = System.currentTimeMillis(); assertFalse(record1.isValid(now)); assertFalse(record2.isValid(now)); Thread.sleep(23000); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); imap1.put(key, value, 10, TimeUnit.SECONDS); assertTrue(migrateKey(key, h1, h1, 0)); assertTrue(migrateKey(key, h1, h2, 1)); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); record1 = cmap1.getRecord(dKey); record2 = cmap2.getRecord(dKey); now = System.currentTimeMillis(); millisLeft1 = record1.getExpirationTime() - now; millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 11000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 11000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals( 1, cmap1.getMapIndexService().getOwnedRecords().size() + cmap2.getMapIndexService().getOwnedRecords().size()); assertTrue(migrateKey(key, h1, h2, 0)); assertTrue(migrateKey(key, h1, h1, 1)); cmap1.startCleanup(true); cmap2.startCleanup(true); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); now = System.currentTimeMillis(); millisLeft1 = record1.getExpirationTime() - now; millisLeft2 = record2.getExpirationTime() - now; assertTrue(millisLeft1 <= 10000 && millisLeft1 > 0); assertTrue(millisLeft2 <= 10000 && millisLeft2 > 0); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); Thread.sleep(11000); now = System.currentTimeMillis(); assertFalse(record1.isValid(now)); assertFalse(record2.isValid(now)); Thread.sleep(20000); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); imap1.put("1", "value1"); record1 = cmap1.getRecord(dKey); record2 = cmap2.getRecord(dKey); assertEquals(1, cmap1.mapRecords.size()); assertEquals(1, cmap2.mapRecords.size()); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(1, cmap2.getMapIndexService().getOwnedRecords().size()); now = System.currentTimeMillis(); assertEquals(Long.MAX_VALUE, record1.getExpirationTime()); assertEquals(Long.MAX_VALUE, record2.getExpirationTime()); assertTrue(record1.isActive()); assertTrue(record2.isActive()); assertTrue(record1.isValid(now)); assertTrue(record2.isValid(now)); assertEquals(1, record1.valueCount()); assertEquals(1, record2.valueCount()); imap1.remove("1"); assertEquals(0, cmap1.getMapIndexService().getOwnedRecords().size()); assertEquals(0, cmap2.getMapIndexService().getOwnedRecords().size()); Thread.sleep(20000); assertEquals(0, cmap1.mapRecords.size()); assertEquals(0, cmap2.mapRecords.size()); assertEquals(0, cmap1.mapIndexService.size()); assertEquals(0, cmap2.mapIndexService.size()); }