Search in sources :

Example 1 with ApplicationContext

use of io.micronaut.context.ApplicationContext in project redisson by redisson.

the class RedissonSessionTest method testSessionExpiration.

@Test
public void testSessionExpiration() throws ExecutionException, InterruptedException {
    Map<String, Object> map = new HashMap<>();
    map.put("redisson.threads", "10");
    map.put("micronaut.session.http.redisson.enabled", "true");
    map.put("redisson.singleServerConfig.address", "redis://127.0.0.1:6379");
    ApplicationContext ac = ApplicationContext.run(map);
    RedissonClient rc = ac.getBean(RedissonClient.class);
    rc.getKeys().flushall();
    RedissonSessionStore sessionStore = ac.getBean(RedissonSessionStore.class);
    RedissonSession session = sessionStore.newSession();
    session.put("username", "oleg");
    session.put("foo", new MyObject("myname"));
    session.setMaxInactiveInterval(Duration.ofSeconds(30));
    RedissonSession saved = sessionStore.save(session).get();
    testData(saved);
    Thread.sleep(30500);
    Optional<RedissonSession> noSession = sessionStore.findSession(saved.getId()).get();
    assertThat(noSession).isEmpty();
    Thread.sleep(10000);
    assertThat(rc.getKeys().count()).isZero();
    ac.stop();
}
Also used : ApplicationContext(io.micronaut.context.ApplicationContext) RedissonClient(org.redisson.api.RedissonClient) RedissonSessionStore(org.redisson.micronaut.session.RedissonSessionStore) RedissonSession(org.redisson.micronaut.session.RedissonSession) Test(org.junit.jupiter.api.Test)

Example 2 with ApplicationContext

use of io.micronaut.context.ApplicationContext in project redisson by redisson.

the class RedissonSessionTest method testSessionCreate.

@Test
public void testSessionCreate() throws ExecutionException, InterruptedException {
    Map<String, Object> map = new HashMap<>();
    map.put("redisson.threads", "10");
    map.put("micronaut.session.http.redisson.enabled", "true");
    map.put("redisson.singleServerConfig.address", "redis://127.0.0.1:6379");
    ApplicationContext ac = ApplicationContext.run(map);
    RedissonClient rc = ac.getBean(RedissonClient.class);
    AppListener listener = ac.getBean(AppListener.class);
    rc.getKeys().flushall();
    RedissonSessionStore sessionStore = ac.getBean(RedissonSessionStore.class);
    RedissonSession session = sessionStore.newSession();
    session.put("username", "oleg");
    session.put("foo", new MyObject("myname"));
    RedissonSession saved = sessionStore.save(session).get();
    testData(saved);
    assertThat(listener.getEvents()).hasSize(1);
    assertThat(listener.getEvents().get(0)).isInstanceOf(SessionCreatedEvent.class);
    listener.getEvents().clear();
    RedissonSession loaded = sessionStore.findSession(saved.getId()).get().get();
    testData(loaded);
    loaded.put("key", "value");
    loaded.remove("username");
    loaded.setLastAccessedTime(Instant.now());
    loaded.setMaxInactiveInterval(Duration.ofMinutes(1));
    sessionStore.save(loaded).get();
    assertThat(listener.getEvents()).isEmpty();
    loaded = sessionStore.findSession(saved.getId()).get().get();
    assertThat(listener.getEvents()).isEmpty();
    assertThat(loaded.contains("username")).isFalse();
    assertThat(((MyObject) loaded.get("foo").get()).getName()).isEqualTo("myname");
    assertThat(loaded.get("key").get()).isEqualTo("value");
    assertThat(loaded.isExpired()).isFalse();
    assertThat(loaded.getCreationTime().getEpochSecond()).isEqualTo(saved.getCreationTime().getEpochSecond());
    assertThat(loaded.getMaxInactiveInterval()).isEqualTo(Duration.ofMinutes(1));
    assertThat(loaded.getId()).isEqualTo(saved.getId());
    Boolean deleted = sessionStore.deleteSession(saved.getId()).get();
    assertThat(deleted).isTrue();
    Thread.sleep(1500);
    assertThat(listener.getEvents()).hasSize(1);
    assertThat(listener.getEvents().get(0)).isInstanceOf(SessionDeletedEvent.class);
    Optional<RedissonSession> noSession = sessionStore.findSession(saved.getId()).get();
    assertThat(noSession).isEmpty();
    Thread.sleep(11000);
    assertThat(rc.getKeys().count()).isZero();
    ac.stop();
}
Also used : ApplicationContext(io.micronaut.context.ApplicationContext) RedissonClient(org.redisson.api.RedissonClient) RedissonSessionStore(org.redisson.micronaut.session.RedissonSessionStore) RedissonSession(org.redisson.micronaut.session.RedissonSession) Test(org.junit.jupiter.api.Test)

Example 3 with ApplicationContext

use of io.micronaut.context.ApplicationContext in project redisson by redisson.

the class RedissonCacheTest method testCache.

@Test
public void testCache() throws InterruptedException {
    Map<String, Object> map = new HashMap<>();
    map.put("redisson.threads", "10");
    map.put("redisson.single-server-config.address", "redis://127.0.0.1:6379");
    // map.put("redisson.clusterServersConfig.scanInterval", "3333");
    // map.put("redisson.clusterServersConfig.nodeAddresses", Arrays.asList("redis://127.0.0.2:6379","redis://127.0.0.3:6379"));
    map.put("redisson.caches.test.expire-after-write", "10s");
    map.put("redisson.caches.test.expire-after-access", "3s");
    ApplicationContext ac = ApplicationContext.run(map);
    RedissonClient client = ac.getBean(RedissonClient.class);
    assertThat(client).isNotNull();
    RedissonSyncCache cache = ac.getBean(RedissonSyncCache.class, Qualifiers.byName("test"));
    cache.put(1, 2);
    Thread.sleep(3500);
    assertThat(cache.get(1, Integer.class).isPresent()).isFalse();
    cache.put(3, 4);
    Thread.sleep(2000);
    cache.get(3, Integer.class);
    Thread.sleep(2000);
    assertThat(cache.get(3, Integer.class).isPresent()).isTrue();
}
Also used : ApplicationContext(io.micronaut.context.ApplicationContext) RedissonClient(org.redisson.api.RedissonClient) HashMap(java.util.HashMap) RedissonSyncCache(org.redisson.micronaut.cache.RedissonSyncCache) Test(org.junit.jupiter.api.Test)

Example 4 with ApplicationContext

use of io.micronaut.context.ApplicationContext in project redisson by redisson.

the class RedissonSessionTest method testWriteBehind.

@Test
public void testWriteBehind() throws ExecutionException, InterruptedException {
    Map<String, Object> map = new HashMap<>();
    map.put("redisson.threads", "10");
    map.put("micronaut.session.http.redisson.enabled", "true");
    map.put("micronaut.session.http.redisson.updateMode", "WRITE_BEHIND");
    map.put("redisson.singleServerConfig.address", "redis://127.0.0.1:6379");
    ApplicationContext ac = ApplicationContext.run(map);
    RedissonClient rc = ac.getBean(RedissonClient.class);
    rc.getKeys().flushall();
    RedissonSessionStore sessionStore = ac.getBean(RedissonSessionStore.class);
    RedissonSession session = sessionStore.newSession();
    session.put("key1", "oleg");
    session.put("key2", new MyObject("myname"));
    session.setMaxInactiveInterval(Duration.ofSeconds(30));
    RedissonSession saved = sessionStore.save(session).get();
    saved.remove("key2");
    saved.put("key1", "alba");
    RedissonSession s = sessionStore.findSession(saved.getId()).get().get();
    assertThat(s.get("key1").get()).isEqualTo("alba");
    assertThat(s.contains("key2")).isFalse();
    ac.stop();
}
Also used : ApplicationContext(io.micronaut.context.ApplicationContext) RedissonClient(org.redisson.api.RedissonClient) RedissonSessionStore(org.redisson.micronaut.session.RedissonSessionStore) RedissonSession(org.redisson.micronaut.session.RedissonSession) Test(org.junit.jupiter.api.Test)

Aggregations

ApplicationContext (io.micronaut.context.ApplicationContext)4 Test (org.junit.jupiter.api.Test)4 RedissonClient (org.redisson.api.RedissonClient)4 RedissonSession (org.redisson.micronaut.session.RedissonSession)3 RedissonSessionStore (org.redisson.micronaut.session.RedissonSessionStore)3 HashMap (java.util.HashMap)1 RedissonSyncCache (org.redisson.micronaut.cache.RedissonSyncCache)1