RedisSessionRepository implements SessionRepository { @Override public void setSessionManager(final SessionManager sessionManager) { this.sessionManager = sessionManager; MetricRegistry metrics = sessionManager.getMetrics(); if (metrics != null) { metrics.removeMatching(new MetricFilter() { @Override public boolean matches(String name, Metric metric) { return name.startsWith(name(RedisConfiguration.METRIC_PREFIX, "redis")); } }); if (sticky) { failoverMetrics = metrics.meter(name(RedisConfiguration.METRIC_PREFIX, namespace, "redis", "failover")); } redis.startMonitoring(metrics); } expirationManager.startExpiredSessionsTask(sessionManager); } RedisSessionRepository(RedisFacade redis, String namespace, String owner, ExpirationStrategy strategy,
      boolean sticky); @Override void setSessionManager(final SessionManager sessionManager); @Override SessionData getSessionData(String id); @Override CommitTransaction startCommit(SessionData session); @Override void remove(SessionData session); byte[] sessionKey(String sessionId); @Override Object getSessionAttribute(SessionData session, String attribute); @Override boolean prepareRemove(SessionData session); @Override Set<String> getAllKeys(SessionData session); @Override void storeSessionData(SessionData sessionData); @Override void requestFinished(); @Override void setSessionAttribute(SessionData session, String name, Object value); @Override void removeSessionAttribute(SessionData session, String name); @Override boolean cleanSessionsOnShutdown(); @Override Collection<String> getOwnedSessionIds(); @Override void close(); @Override void reset(); @Override void sessionIdChange(SessionData sessionData); @Override boolean isConnected();  }
@Test public void testSetMetrics() { RedisFacade facade = mock(RedisFacade.class); SessionManager sm = mock(SessionManager.class); MetricRegistry metrics = spy(new MetricRegistry()); SessionConfiguration conf = new SessionConfiguration(); when(sm.getConfiguration()).thenReturn(conf); when(sm.getMetrics()).thenReturn(metrics); metrics.meter("com.amadeus.session.myapp.redis.sample"); try (RedisSessionRepository rsr = new RedisSessionRepository(facade, "myapp", "localhost", ExpirationStrategy.NOTIF, false)) { rsr.setSessionManager(sm); verify(metrics).removeMatching(any(MetricFilter.class)); verify(metrics, never()).meter("com.amadeus.session.myapp.redis.failover"); } }
@Test public void testSetMetricsSticky() { RedisFacade facade = mock(RedisFacade.class); SessionManager sm = mock(SessionManager.class); SessionConfiguration conf = new SessionConfiguration(); when(sm.getConfiguration()).thenReturn(conf); MetricRegistry metrics = spy(new MetricRegistry()); when(sm.getMetrics()).thenReturn(metrics ); try (RedisSessionRepository rsr = new RedisSessionRepository(facade, "myapp", "localhost", ExpirationStrategy.ZRANGE, true)) { rsr.setSessionManager(sm); verify(metrics).meter("com.amadeus.session.myapp.redis.failover"); } }
@Test public void testSortedSetExpiration() { RedisFacade facade = mock(RedisFacade.class); try (RedisSessionRepository rsr = new RedisSessionRepository(facade, "myapp", "localhost", ExpirationStrategy.ZRANGE, false)) { SessionConfiguration conf = new SessionConfiguration(); SessionManager sm = mock(SessionManager.class); when(sm.getConfiguration()).thenReturn(conf); rsr.setSessionManager(sm); verify(sm).schedule(any(String.class), any(Runnable.class), eq(10L)); conf.setMaxInactiveInterval(10); sm = mock(SessionManager.class); when(sm.getConfiguration()).thenReturn(conf); rsr.setSessionManager(sm); verify(sm).schedule(any(String.class), any(Runnable.class), eq(2L)); sm = mock(SessionManager.class); when(sm.getConfiguration()).thenReturn(conf); conf.setMaxInactiveInterval(-100); rsr.setSessionManager(sm); verify(sm).schedule(any(String.class), any(Runnable.class), eq(10L)); } }