MemoryStatsCounter implements StatsCounter { @Override public synchronized void startSession(String sessionId, String user, String browser, String version, String route) { if (session2instant.put(sessionId, now()) == null) { session2user.put(sessionId, user); session2browserVersion.put(sessionId, new BrowserVersion(browser, version)); user2browserCount.putIfAbsent(user, new BrowsersCountMap()); user2browserCount.get(user).increment(browser, version); } }  @Override synchronized void startSession(String sessionId, String user, String browser, String version, String route); @Override void updateSession(String sessionId, String route); @Override synchronized void deleteSession(String sessionId, String route); @Override void expireSessionsOlderThan(Duration duration); @Override Set<String> getActiveSessions(); @Override synchronized BrowsersCountMap getStats(String user); @Override int getSessionsCountForUser(String user); @Override int getSessionsCountForUserAndBrowser(String user, String browser, String version);  }
@Test public void testNewSessionsAreNotExpired() throws Exception { storage.startSession("session1", "user", "firefox", "33"); storage.startSession("session2", "user", "firefox", "33"); assertThat(expiredSessions(1000), is(empty())); assertThat(countJsonFor("user"), is("{\"firefox\":{\"33\":2}}")); }
@Test public void testOldSessionsAreExpired() throws Exception { storage.startSession("session1", "user", "firefox", "33"); storage.startSession("session2", "user", "firefox", "33"); Thread.sleep(500); storage.startSession("session3", "user", "firefox", "33"); assertThat(expiredSessions(250), containsInAnyOrder("session1", "session2")); assertThat(countJsonFor("user"), is("{\"firefox\":{\"33\":1}}")); Thread.sleep(500); assertThat(expiredSessions(250), contains("session3")); assertThat(countJsonFor("user"), is("{}")); }
@Test public void testMultipleUsersExpiration() throws Exception { storage.startSession("session1", "user1", "firefox", "33"); Thread.sleep(500); storage.startSession("session2", "user2", "firefox", "33"); assertThat(expiredSessions(250), contains("session1")); assertThat(countJsonFor("user1"), is("{}")); assertThat(countJsonFor("user2"), is("{\"firefox\":{\"33\":1}}")); }