Skip to content

Commit

Permalink
add IdleTimeout test, use volatile sessions
Browse files Browse the repository at this point in the history
Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Nov 26, 2020
1 parent ce11fb3 commit 104beb9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public boolean awaitClose(long timeout)

public class ClientToProxy implements WebSocketPartialListener, WebSocketPingPongListener
{
private Session session;
private volatile Session session;
private final CountDownLatch closeLatch = new CountDownLatch(1);
private final AtomicInteger pingsReceived = new AtomicInteger();

Expand Down Expand Up @@ -211,7 +211,7 @@ public void onWebSocketClose(int statusCode, String reason)

public class ProxyToServer implements WebSocketPartialListener, WebSocketPingPongListener
{
private Session session;
private volatile Session session;
private final CountDownLatch closeLatch = new CountDownLatch(1);
private final AtomicInteger pingsReceived = new AtomicInteger();

Expand All @@ -223,6 +223,7 @@ public Session getSession()
public void fail(Throwable failure)
{
// Only ProxyToServer can be failed before it is opened (if ClientToProxy fails before the connect completes).
Session session = this.session;
if (session != null)
session.close(StatusCode.SERVER_ERROR, failure.getMessage());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -203,7 +204,7 @@ public void testServerError() throws Exception
}

@Test
public void testServerErrorClientNoResponse() throws Exception
public void testServerThrowsOnMessage() throws Exception
{
serverSocket = new OnTextThrowingSocket();

Expand All @@ -230,6 +231,37 @@ public void testServerErrorClientNoResponse() throws Exception
assertTrue(webSocketProxy.awaitClose(5000));
}

@Test
public void timeoutTest() throws Exception
{
long clientSessionIdleTimeout = 2000;

EventSocket clientSocket = new EventSocket();
client.connect(clientSocket, proxyUri);
assertTrue(clientSocket.openLatch.await(5, TimeUnit.SECONDS));
assertTrue(serverSocket.openLatch.await(5, TimeUnit.SECONDS));

// Configure infinite idleTimeout on the server session and short timeout on the client session.
clientSocket.session.setIdleTimeout(clientSessionIdleTimeout);
serverSocket.session.setIdleTimeout(-1);

// Send and receive an echo message.
clientSocket.session.getRemote().sendString("test echo message");
assertThat(clientSocket.textMessages.poll(clientSessionIdleTimeout, TimeUnit.SECONDS), is("test echo message"));

// Wait more than the idleTimeout period, the clientToProxy connection should fail which should fail the proxyToServer.
assertTrue(clientSocket.closeLatch.await(clientSessionIdleTimeout * 2, TimeUnit.MILLISECONDS));
assertTrue(serverSocket.closeLatch.await(clientSessionIdleTimeout * 2, TimeUnit.MILLISECONDS));

// Check errors and close status.
assertThat(clientSocket.error.getMessage(), containsString("Idle timeout expired"));
assertThat(clientSocket.closeCode, is(StatusCode.SHUTDOWN));
assertThat(clientSocket.closeReason, containsString("Idle timeout expired"));
assertNull(serverSocket.error);
assertThat(serverSocket.closeCode, is(StatusCode.SHUTDOWN));
assertThat(serverSocket.closeReason, containsString("Idle timeout expired"));
}

@Test
public void testPingPong() throws Exception
{
Expand Down

0 comments on commit 104beb9

Please sign in to comment.