@Test public void flushModeImmediateCreate() { given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); this.redisRepository.setRedisFlushMode(RedisFlushMode.IMMEDIATE); RedisSession session = this.redisRepository.createSession(); Map<String, Object> delta = getDelta(); assertThat(delta.size()).isEqualTo(3); Object creationTime = delta .get(RedisOperationsSessionRepository.CREATION_TIME_ATTR); assertThat(creationTime).isEqualTo(session.getCreationTime().toEpochMilli()); assertThat(delta.get(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR)) .isEqualTo((int) Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS).getSeconds()); assertThat(delta.get(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR)) .isEqualTo(session.getCreationTime().toEpochMilli()); }
@Test public void saveNewSession() { RedisSession session = this.redisRepository.createSession(); given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); this.redisRepository.save(session); Map<String, Object> delta = getDelta(); assertThat(delta.size()).isEqualTo(3); Object creationTime = delta .get(RedisOperationsSessionRepository.CREATION_TIME_ATTR); assertThat(creationTime).isEqualTo(session.getCreationTime().toEpochMilli()); assertThat(delta.get(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR)) .isEqualTo((int) Duration.ofSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS).getSeconds()); assertThat(delta.get(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR)) .isEqualTo(session.getCreationTime().toEpochMilli()); }
this.redis.boundValueOps(sessionKey).append(""); this.redis.boundValueOps(sessionKey).persist(); this.redis.boundHashOps(getSessionKey(session.getId())).persist(); return; this.redis.boundValueOps(sessionKey).append(""); this.redis.boundValueOps(sessionKey).expire(sessionExpireInSeconds, TimeUnit.SECONDS);
@Test public void flushModeSetMaxInactiveIntervalInSeconds() { given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); this.redisRepository.setRedisFlushMode(RedisFlushMode.IMMEDIATE); RedisSession session = this.redisRepository.createSession(); reset(this.boundHashOperations); session.setMaxInactiveInterval(Duration.ofSeconds(1)); verify(this.boundHashOperations).expire(anyLong(), any(TimeUnit.class)); }
@Test public void changeSessionIdWhenNotSaved() { given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); RedisSession createSession = this.redisRepository.createSession(); String originalId = createSession.getId(); String changeSessionId = createSession.changeSessionId(); this.redisRepository.save(createSession); verify(this.redisOperations, never()).rename(anyString(), anyString()); assertThat(originalId).isNotEqualTo(changeSessionId); assertThat(createSession.getId()).isEqualTo(createSession.getId()); }
@Test public void flushModeSetLastAccessedTime() { given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); this.redisRepository.setRedisFlushMode(RedisFlushMode.IMMEDIATE); RedisSession session = this.redisRepository.createSession(); session.setLastAccessedTime(Instant.now()); Map<String, Object> delta = getDelta(2); assertThat(delta.size()).isEqualTo(1); assertThat(delta) .isEqualTo(map(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, session.getLastAccessedTime().toEpochMilli())); }
@Test public void changeSessionIdWhenSaved() { given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); RedisSession session = this.redisRepository.new RedisSession(this.cached); session.setLastAccessedTime(session.getLastAccessedTime()); String originalId = session.getId(); String changeSessionId = session.changeSessionId(); this.redisRepository.save(session); verify(this.redisOperations, times(2)).rename(anyString(), anyString()); assertThat(originalId).isNotEqualTo(changeSessionId); assertThat(session.getId()).isEqualTo(session.getId()); }
@Test public void saveExpired() { RedisSession session = this.redisRepository.new RedisSession(new MapSession()); session.setMaxInactiveInterval(Duration.ZERO); given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); this.redisRepository.save(session); String id = session.getId(); verify(this.redisOperations, atLeastOnce()).delete(getKey("expires:" + id)); verify(this.redisOperations, never()).boundValueOps(getKey("expires:" + id)); }
@Test public void flushModeImmediateRemoveAttribute() { given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); this.redisRepository.setRedisFlushMode(RedisFlushMode.IMMEDIATE); RedisSession session = this.redisRepository.createSession(); String attrName = "someAttribute"; session.removeAttribute(attrName); Map<String, Object> delta = getDelta(2); assertThat(delta.size()).isEqualTo(1); assertThat(delta).isEqualTo( map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), session.getAttribute(attrName))); }
@Test public void changeRedisNamespace() { String namespace = "foo:bar"; this.redisRepository.setRedisKeyNamespace(namespace); RedisSession session = this.redisRepository.new RedisSession(new MapSession()); session.setMaxInactiveInterval(Duration.ZERO); given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); this.redisRepository.save(session); String id = session.getId(); verify(this.redisOperations, atLeastOnce()) .delete(namespace + ":sessions:expires:" + id); verify(this.redisOperations, never()) .boundValueOps(namespace + ":sessions:expires:" + id); }
@Test public void flushModeImmediateSetAttribute() { given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); this.redisRepository.setRedisFlushMode(RedisFlushMode.IMMEDIATE); RedisSession session = this.redisRepository.createSession(); String attrName = "someAttribute"; session.setAttribute(attrName, "someValue"); Map<String, Object> delta = getDelta(2); assertThat(delta.size()).isEqualTo(1); assertThat(delta).isEqualTo( map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), session.getAttribute(attrName))); }
@Test public void saveLastAccessChanged() { RedisSession session = this.redisRepository.new RedisSession( new MapSession(this.cached)); session.setLastAccessedTime(Instant.ofEpochMilli(12345678L)); given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); this.redisRepository.save(session); assertThat(getDelta()) .isEqualTo(map(RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, session.getLastAccessedTime().toEpochMilli())); }
@Before public void setup() { RedisOperationsSessionRepository repository = new RedisOperationsSessionRepository( this.sessionRedisOperations); this.policy = new RedisSessionExpirationPolicy(this.sessionRedisOperations, repository::getExpirationsKey, repository::getSessionKey); this.session = new MapSession(); this.session.setLastAccessedTime(Instant.ofEpochMilli(1429116694675L)); this.session.setId("12345"); given(this.sessionRedisOperations.boundSetOps(anyString())) .willReturn(this.setOperations); given(this.sessionRedisOperations.boundHashOps(anyString())) .willReturn(this.hashOperations); given(this.sessionRedisOperations.boundValueOps(anyString())) .willReturn(this.valueOperations); }
@Test public void delete() { String attrName = "attrName"; MapSession expected = new MapSession(); expected.setLastAccessedTime(Instant.now().minusSeconds(60)); expected.setAttribute(attrName, "attrValue"); given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); Map map = map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), expected.getAttribute(attrName), RedisOperationsSessionRepository.CREATION_TIME_ATTR, expected.getCreationTime().toEpochMilli(), RedisOperationsSessionRepository.MAX_INACTIVE_ATTR, (int) expected.getMaxInactiveInterval().getSeconds(), RedisOperationsSessionRepository.LAST_ACCESSED_ATTR, expected.getLastAccessedTime().toEpochMilli()); given(this.boundHashOperations.entries()).willReturn(map); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); String id = expected.getId(); this.redisRepository.deleteById(id); assertThat(getDelta().get(RedisOperationsSessionRepository.MAX_INACTIVE_ATTR)) .isEqualTo(0); verify(this.redisOperations, atLeastOnce()).delete(getKey("expires:" + id)); verify(this.redisOperations, never()).boundValueOps(getKey("expires:" + id)); }
@Test public void saveRemoveAttribute() { String attrName = "attrName"; RedisSession session = this.redisRepository.new RedisSession(new MapSession()); session.removeAttribute(attrName); given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); this.redisRepository.save(session); assertThat(getDelta()).isEqualTo(map( RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), null)); }
@Test public void saveSetAttribute() { String attrName = "attrName"; RedisSession session = this.redisRepository.new RedisSession(new MapSession()); session.setAttribute(attrName, "attrValue"); given(this.redisOperations.boundHashOps(anyString())) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(anyString())) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(anyString())) .willReturn(this.boundValueOperations); this.redisRepository.save(session); assertThat(getDelta()).isEqualTo( map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), session.getAttribute(attrName))); }
@Test public void saveJavadocSummary() { RedisSession session = this.redisRepository.createSession(); String sessionKey = "spring:session:sessions:" + session.getId(); String backgroundExpireKey = "spring:session:expirations:" + RedisSessionExpirationPolicy.roundUpToNextMinute( RedisSessionExpirationPolicy.expiresInMillis(session)); String destroyedTriggerKey = "spring:session:sessions:expires:" + session.getId(); given(this.redisOperations.boundHashOps(sessionKey)) .willReturn(this.boundHashOperations); given(this.redisOperations.boundSetOps(backgroundExpireKey)) .willReturn(this.boundSetOperations); given(this.redisOperations.boundValueOps(destroyedTriggerKey)) .willReturn(this.boundValueOperations); this.redisRepository.save(session); // the actual data in the session expires 5 minutes after expiration so the data // can be accessed in expiration events // if the session is retrieved and expired it will not be returned since // findById checks if it is expired long fiveMinutesAfterExpires = session.getMaxInactiveInterval().plusMinutes(5) .getSeconds(); verify(this.boundHashOperations).expire(fiveMinutesAfterExpires, TimeUnit.SECONDS); verify(this.boundSetOperations).expire(fiveMinutesAfterExpires, TimeUnit.SECONDS); verify(this.boundSetOperations).add("expires:" + session.getId()); verify(this.boundValueOperations).expire(1800L, TimeUnit.SECONDS); verify(this.boundValueOperations).append(""); }
@Test public void saveJavadoc() { RedisSession session = this.redisRepository.new RedisSession(this.cached); session.setLastAccessedTime(session.getLastAccessedTime()); given(this.redisOperations.boundHashOps("spring:session:sessions:session-id")) .willReturn(this.boundHashOperations); given(this.redisOperations .boundSetOps("spring:session:expirations:1404361860000")) .willReturn(this.boundSetOperations); given(this.redisOperations .boundValueOps("spring:session:sessions:expires:session-id")) .willReturn(this.boundValueOperations); this.redisRepository.save(session); // the actual data in the session expires 5 minutes after expiration so the data // can be accessed in expiration events // if the session is retrieved and expired it will not be returned since // findById checks if it is expired verify(this.boundHashOperations).expire( session.getMaxInactiveInterval().plusMinutes(5).getSeconds(), TimeUnit.SECONDS); }