Search in sources :

Example 1 with RedissonSession

use of org.redisson.micronaut.session.RedissonSession 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 RedissonSession

use of org.redisson.micronaut.session.RedissonSession 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 RedissonSession

use of org.redisson.micronaut.session.RedissonSession 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)3 Test (org.junit.jupiter.api.Test)3 RedissonClient (org.redisson.api.RedissonClient)3 RedissonSession (org.redisson.micronaut.session.RedissonSession)3 RedissonSessionStore (org.redisson.micronaut.session.RedissonSessionStore)3