@Test public void testTTL() throws Exception { Config config = new Config(); FactoryImpl mockFactory = mock(FactoryImpl.class); // we mocked the node // do not forget to shutdown the connectionManager // so that server socket can be released. Node node = new Node(mockFactory, config); node.serviceThread = Thread.currentThread(); CMap cmap = new CMap(node.concurrentMapManager, "c:myMap"); Object key = "1"; Object value = "istanbul"; Data dKey = toData(key); Data dValue = toData(value); Request reqPut = newPutRequest(dKey, dValue); reqPut.ttl = 3000; cmap.put(reqPut); assertTrue(cmap.mapRecords.containsKey(toData(key))); Data actualValue = cmap.get(newGetRequest(dKey)); assertThat(toObject(actualValue), equalTo(value)); assertEquals(1, cmap.mapRecords.size()); Record record = cmap.getRecord(dKey); assertNotNull(record); assertTrue(record.isActive()); assertTrue(record.isValid()); assertEquals(1, cmap.size()); assertNotNull(cmap.get(newGetRequest(dKey))); assertEquals(dValue, cmap.get(newGetRequest(dKey))); assertTrue(record.getRemainingTTL() > 1000); Thread.sleep(1000); assertTrue(record.getRemainingTTL() < 2100); cmap.put(newPutRequest(dKey, dValue)); assertTrue(record.getRemainingTTL() > 2001); assertTrue(record.isActive()); assertTrue(record.isValid()); Thread.sleep(1000); assertTrue(record.getRemainingTTL() < 2100); cmap.put(newPutRequest(dKey, dValue)); assertTrue(record.getRemainingTTL() > 2001); assertTrue(record.isActive()); assertTrue(record.isValid()); Thread.sleep(5000); assertEquals(0, cmap.size()); assertTrue(cmap.evict(newEvictRequest(dKey))); assertTrue(cmap.shouldPurgeRecord(record, System.currentTimeMillis() + 10000)); cmap.removeAndPurgeRecord(record); assertEquals(0, cmap.mapRecords.size()); assertEquals(0, cmap.size()); assertEquals(0, cmap.mapIndexService.size()); node.connectionManager.shutdown(); }
@Test public void testPut() throws Exception { Config config = new Config(); FactoryImpl mockFactory = mock(FactoryImpl.class); // we mocked the node // do not forget to shutdown the connectionManager // so that server socket can be released. Node node = new Node(mockFactory, config); node.serviceThread = Thread.currentThread(); CMap cmap = new CMap(node.concurrentMapManager, "c:myMap"); Object key = "1"; Object value = "istanbul"; Data dKey = toData(key); Data dValue = toData(value); cmap.put(newPutRequest(dKey, dValue)); assertTrue(cmap.mapRecords.containsKey(toData(key))); Data actualValue = cmap.get(newGetRequest(dKey)); assertThat(toObject(actualValue), equalTo(value)); assertEquals(1, cmap.mapRecords.size()); Record record = cmap.getRecord(dKey); assertNotNull(record); assertTrue(record.isActive()); assertTrue(record.isValid()); assertEquals(1, cmap.size()); cmap.remove(newRemoveRequest(dKey)); assertTrue(System.currentTimeMillis() - record.getRemoveTime() < 100); assertEquals(1, cmap.mapRecords.size()); record = cmap.getRecord(dKey); assertNotNull(record); assertFalse(record.isActive()); assertFalse(record.isValid()); assertEquals(0, cmap.size()); cmap.put(newPutRequest(dKey, dValue, 1000)); assertEquals(0, record.getRemoveTime()); assertTrue(cmap.mapRecords.containsKey(toData(key))); Thread.sleep(1500); assertEquals(0, cmap.size()); assertFalse(cmap.containsKey(newContainsRequest(dKey, null))); node.connectionManager.shutdown(); }