Skip to content

Commit

Permalink
Destroy the session in a follow-up request to ensure the session has …
Browse files Browse the repository at this point in the history
…been flushed to the session store at least once (vert-x3#2674)

Making sure that the session has been flushed to the session store,
ensures that the serialization/deserialization process of the stored
object has been tested as well.
  • Loading branch information
fbuetler committed Nov 7, 2024
1 parent 98bd9a7 commit 6a5e2c5
Showing 1 changed file with 29 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -224,44 +224,58 @@ private boolean testSessionBlocking(String sessionId, Function<Session, Boolean>

@Test
public void testDestroySession() throws Exception {
// given
router.route().handler(SessionHandler.create(store));
AtomicReference<String> rid = new AtomicReference<>();
AtomicInteger requestCount = new AtomicInteger();
final AtomicReference<String> sid = new AtomicReference<>();
final AtomicInteger requestCount = new AtomicInteger();
router.route().handler(rc -> {
Session sess = rc.session();
assertNotNull(sess);
assertTrue(System.currentTimeMillis() - sess.lastAccessed() < 500);
assertNotNull(sess.id());
switch (requestCount.get()) {
case 0:
rid.set(sess.id());
sid.set(sess.id());
sess.put("foo", "bar");
sess.destroy();
break;
case 1:
assertFalse(rid.get().equals(sess.id())); // New session
assertNull(sess.get("foo"));
rid.set(sess.id());
assertEquals(sid.get(), sess.id());
// Destroy session in a follow-up request, so that it has been flushed to
// the session store
sess.destroy();
break;
case 2:
assertFalse(sid.get().equals(sess.id())); // New session
assertNull(sess.get("foo"));
sid.set(sess.id());
sess.destroy(); // Destroy session in the same request as it is created
break;
}
requestCount.incrementAndGet();
rc.response().end();
});

// when
final AtomicReference<String> sessionCookie = new AtomicReference<>();
testRequest(HttpMethod.GET, "/", null, resp -> {
assertTrue(resp.headers().getAll("set-cookie").size() == 1); // expect new session cookie only
String setCookie = resp.headers().get("set-cookie");
assertNull(setCookie);
// the cookie got destroyed even before the end of the request, so no side
// effects are expected
assertNotNull(setCookie);
sessionCookie.set(setCookie.split(";")[0]);
}, 200, "OK", null);
testRequest(HttpMethod.GET, "/", req -> req.putHeader("cookie", sessionCookie.get()), resp -> {
assertTrue(resp.headers().getAll("set-cookie").size() == 1); // expect expired session cookie only
}, 200, "OK", null);
testRequest(HttpMethod.GET, "/", null, null, 200, "OK", null);
testRequest(HttpMethod.GET, "/", req -> req.putHeader("cookie", sessionCookie.get()), null, 200, "OK", null);

// then
Thread.sleep(500); // Needed because session.destroy is async
CountDownLatch latch1 = new CountDownLatch(1);
store.get(rid.get(), onSuccess(res -> {
CountDownLatch latch = new CountDownLatch(1);
store.get(sid.get(), onSuccess(res -> {
assertNull(res);
latch1.countDown();
latch.countDown();
}));
awaitLatch(latch1);
awaitLatch(latch);
}

@Test
Expand Down

0 comments on commit 6a5e2c5

Please sign in to comment.