@Test
  public void timeout() throws Exception {
    TestFactory f = new TestFactory();
    SessionManager<Integer> s = new SessionManager<Integer>(f, TEST_LIFESPAN);
    assertEquals(TEST_LIFESPAN, s.getLifespan());

    SessionHolder<Integer> h = new SessionHolder<Integer>(TEST_USER, TEST_CONTEXT);
    s.put(TEST_SESSION_ID, h);
    assertSame(h, s.get(TEST_SESSION_ID));
    assertEquals(TEST_SESSION_ID, f.getLastSessionId());
    for (int i = 0; i < 10; i++) {
      Thread.sleep(TEST_LIFESPAN / 2);
      assertSame(h, s.get(TEST_SESSION_ID));
    }
    Thread.sleep(TEST_LIFESPAN * 2);
    assertSingleEvent(
        Level.INFO,
        TEST_CATEGORY,
        "Session "
            + TEST_SESSION_ID.toString()
            + " has expired; creation context: "
            + TEST_CONTEXT.toString(),
        TEST_CATEGORY);
    assertNull(s.get(TEST_SESSION_ID));
    assertEquals(TEST_SESSION, f.getLastRemovedSession());
  }
  @Test
  public void basicsNoFactory() throws Exception {
    SessionManager<Integer> s = new SessionManager<Integer>();
    assertEquals(SessionManager.INFINITE_SESSION_LIFESPAN, s.getLifespan());
    SessionHolder<Integer> h = new SessionHolder<Integer>(TEST_USER, TEST_CONTEXT);
    s.put(TEST_SESSION_ID, h);
    assertSame(h, s.get(TEST_SESSION_ID));
    s.remove(TEST_SESSION_ID);
    assertNull(s.get(TEST_SESSION_ID));

    // Removal of nonexistent session ID.

    s.remove(TEST_SESSION_ID);
  }
  @Test
  public void basics() throws Exception {
    TestFactory f = new TestFactory();
    SessionManager<Integer> s = new SessionManager<Integer>(f);
    assertSame(f, s.getSessionFactory());
    assertEquals(SessionManager.INFINITE_SESSION_LIFESPAN, s.getLifespan());

    assertNull(s.getServerId());
    s.setServerId(TEST_SERVER_ID);
    assertEquals(TEST_SERVER_ID, s.getServerId());
    s.setServerId(null);
    assertNull(s.getServerId());

    assertNull(s.get(TEST_SESSION_ID));

    SessionHolder<Integer> hD = new SessionHolder<Integer>(TEST_USER, TEST_CONTEXT);
    long time = hD.getLastAccess();
    Thread.sleep(100);
    s.put(TEST_SESSION_ID, hD);
    assertTrue(hD.getLastAccess() > time);
    assertEquals(TEST_SESSION_ID, f.getLastSessionId());

    time = hD.getLastAccess();
    Thread.sleep(100);
    assertSame(hD, s.get(TEST_SESSION_ID));
    assertTrue(hD.getLastAccess() > time);

    SessionHolder<Integer> h = new SessionHolder<Integer>(TEST_USER, TEST_CONTEXT);
    s.put(TEST_SESSION_ID, h);
    assertSame(h, s.get(TEST_SESSION_ID));

    assertNull(s.get(TEST_SESSION_ID_D));
    s.put(TEST_SESSION_ID_D, hD);
    assertSame(hD, s.get(TEST_SESSION_ID_D));
    assertSame(h, s.get(TEST_SESSION_ID));
    assertEquals(TEST_SESSION_ID_D, f.getLastSessionId());

    s.remove(TEST_SESSION_ID);
    assertNull(s.get(TEST_SESSION_ID));
    assertSame(hD, s.get(TEST_SESSION_ID_D));
    assertEquals(TEST_SESSION, f.getLastRemovedSession());

    // Removal of nonexistent session ID.

    s.remove(TEST_SESSION_ID);
  }