@Test public void testTtlOverride() throws Exception { final String id = "testTtlAnnotation"; EntityManager<TtlEntity, String> entityPersister = new DefaultEntityManager.Builder<TtlEntity, String>() .withEntityType(TtlEntity.class) .withKeyspace(keyspace) .withColumnFamily(CF_SAMPLE_ENTITY) .withTTL(5) .build(); TtlEntity origEntity = createTtlEntity(id); entityPersister.put(origEntity); // use low-level astyanax API to confirm the write { ColumnList<String> cl = keyspace.prepareQuery(CF_SAMPLE_ENTITY).getKey(id).execute().getResult(); // test column number Assert.assertEquals(1, cl.size()); // test column value Assert.assertEquals(origEntity.getColumn(), cl.getColumnByName("column").getStringValue()); // custom ttl Assert.assertEquals(5, cl.getColumnByName("column").getTtl()); } TtlEntity getEntity = entityPersister.get(id); Assert.assertEquals(origEntity, getEntity); // entity should still be alive after 3s since TTL is overriden to 5s Thread.sleep(1000 * 3); getEntity = entityPersister.get(id); Assert.assertEquals(origEntity, getEntity); // entity should expire after 3s since 6s have passed with 5s TTL Thread.sleep(1000 * 3); // use low-level astyanax API to confirm the TTL expiration { ColumnList<String> cl = keyspace.prepareQuery(CF_SAMPLE_ENTITY).getKey(id).execute().getResult(); Assert.assertEquals(0, cl.size()); } }
private TtlEntity createTtlEntity(String id) { TtlEntity e = new TtlEntity(); e.setId(id); e.setColumn(RandomStringUtils.randomAlphanumeric(4)); return e; }