Search in sources :

Example 6 with Flower

use of io.vertigo.dynamo.kvstore.data.Flower in project vertigo by KleeGroup.

the class BerkeleyKVStoreManagerTest method testTimeToLive.

@Test
public void testTimeToLive() {
    try (VTransactionWritable transaction = transactionManager.createCurrentTransaction()) {
        final int nbFlowers = kvStoreManager.count("flowers");
        Assert.assertEquals(0, nbFlowers);
        // put a flower a t+0s (expire a T+10s)
        final Flower tulip1 = buildFlower("tulip", 100);
        kvStoreManager.put("flowers", "1", tulip1);
        sleep(2);
        // put a flower a t+2s (expire a T+12s)
        final Flower tulip2 = buildFlower("tulip", 110);
        kvStoreManager.put("flowers", "2", tulip2);
        sleep(2);
        // put a flower a t+4s (expire a T+14s)
        final Flower tulip3 = buildFlower("tulip", 120);
        kvStoreManager.put("flowers", "3", tulip3);
        sleep(2);
        // count after 3 inserts and T+6s
        // can't use count as it doesnt detect too old element (needs daemon)
        final long nbFlowers2 = kvStoreManager.findAll("flowers", 0, 1000, Flower.class).size();
        Assert.assertEquals(3, nbFlowers2);
        sleep(3);
        // find unexpired element
        final Optional<Flower> tulip1Load = kvStoreManager.find("flowers", "1", Flower.class);
        Assert.assertTrue(tulip1Load.isPresent());
        // count after 3 inserts and T+9s
        final long nbFlowers3 = kvStoreManager.findAll("flowers", 0, 1000, Flower.class).size();
        Assert.assertEquals(3, nbFlowers3);
        sleep(2);
        // count after 3 inserts and T+11s
        final long nbFlowers4 = kvStoreManager.findAll("flowers", 0, 1000, Flower.class).size();
        Assert.assertEquals(2, nbFlowers4);
        sleep(2);
        // count after 3 inserts and T+13s
        final long nbFlowers5 = kvStoreManager.findAll("flowers", 0, 1000, Flower.class).size();
        Assert.assertEquals(1, nbFlowers5);
        sleep(2);
        // count after 3 inserts and 15s
        final long nbFlowers6 = kvStoreManager.findAll("flowers", 0, 1000, Flower.class).size();
        Assert.assertEquals(0, nbFlowers6);
        // find expired element
        final Optional<Flower> tulip1Reload = kvStoreManager.find("flowers", "1", Flower.class);
        Assert.assertFalse(tulip1Reload.isPresent());
    }
}
Also used : Flower(io.vertigo.dynamo.kvstore.data.Flower) VTransactionWritable(io.vertigo.commons.transaction.VTransactionWritable) AbstractKVStoreManagerTest(io.vertigo.dynamo.kvstore.AbstractKVStoreManagerTest) Test(org.junit.Test)

Aggregations

VTransactionWritable (io.vertigo.commons.transaction.VTransactionWritable)6 Flower (io.vertigo.dynamo.kvstore.data.Flower)6 Test (org.junit.Test)6 AbstractKVStoreManagerTest (io.vertigo.dynamo.kvstore.AbstractKVStoreManagerTest)3 VSystemException (io.vertigo.lang.VSystemException)1 ListBuilder (io.vertigo.util.ListBuilder)1