private void setStickyness(final SessionAffinityMode sessionAffinity) {
    if (!sessionAffinity.isSticky()) {
      _tomcat1.getEngine().setJvmRoute(null);
    }
    final SessionManager manager = _tomcat1.getManager();
    manager.setSticky(sessionAffinity.isSticky());

    try {
      waitForReconnect(manager.getMemcachedSessionService().getMemcached(), 1, 500);
    } catch (final InterruptedException e) {
      Thread.currentThread().interrupt();
      throw new RuntimeException(e);
    }
  }
  @Test(enabled = true, dataProviderClass = TestUtils.class, dataProvider = STICKYNESS_PROVIDER)
  public void testExpiredSessionRemovedFromMemcached(
      @Nonnull final SessionAffinityMode sessionAffinity)
      throws IOException, InterruptedException, HttpException {

    setStickyness(sessionAffinity);

    final String sessionId1 = makeRequest(_httpClient, _portTomcat1, null);
    assertNotNull(sessionId1, "No session created.");

    waitForSessionExpiration(sessionAffinity.isSticky());

    assertNull(_memcached.get(sessionId1), "Expired session still existing in memcached");
  }
  @Test(enabled = true, dataProviderClass = TestUtils.class, dataProvider = STICKYNESS_PROVIDER)
  public void testSessionAvailableInMemcachedWithCookiesDisabled(
      final SessionAffinityMode sessionAffinity) throws Exception {
    _tomcat1.stop();
    _tomcat1 =
        tcBuilder()
            .sticky(sessionAffinity.isSticky())
            .cookies(false)
            .jvmRoute("app1")
            .buildAndStart();

    final Response response = get(_httpClient, _portTomcat1, null);
    final String sessionId = response.get(TestServlet.ID);
    assertNotNull(sessionId, "No session created.");
    Thread.sleep(50);
    assertNotNull(_memcached.get(sessionId), "Session not available in memcached.");
  }
  @Test(enabled = true, dataProviderClass = TestUtils.class, dataProvider = STICKYNESS_PROVIDER)
  public void testInvalidatedSessionRemovedFromMemcached(
      @Nonnull final SessionAffinityMode sessionAffinity)
      throws IOException, InterruptedException, HttpException {

    setStickyness(sessionAffinity);

    final String sessionId1 = makeRequest(_httpClient, _portTomcat1, null);
    assertNotNull(sessionId1, "No session created.");

    final Response response = get(_httpClient, _portTomcat1, PATH_INVALIDATE, sessionId1);
    assertNull(response.getResponseSessionId());
    assertEquals(_daemon.getCache().getGetMisses(), 1); // 1 is ok

    assertNull(_memcached.get(sessionId1), "Invalidated session still existing in memcached");
    if (!sessionAffinity.isSticky()) {
      assertNull(
          _memcached.get(createValidityInfoKeyName(sessionId1)),
          "ValidityInfo for invalidated session still exists in memcached.");
    }
  }