Skip to content

Commit

Permalink
Issue #3734 - throw ISE for WebSocket suspend after close (jetty-9.4) (
Browse files Browse the repository at this point in the history
…#4098)

* Issue #3734 - throw ISE for WebSocket suspend after close

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>

* Issue #3734 - suspend is error if onClose() has been called

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Sep 25, 2019
1 parent 11b60db commit 3edc6c9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class SuspendResumeTest
Expand Down Expand Up @@ -195,8 +196,7 @@ public void testSuspendAfterClose() throws Exception
assertThat(clientSocket.closeCode, is(StatusCode.NORMAL));
assertThat(serverSocket.closeCode, is(StatusCode.NORMAL));

// suspend the client so that no read events occur
SuspendToken suspendToken = clientSocket.session.suspend();
suspendToken.resume();
// suspend after closed throws ISE
assertThrows(IllegalStateException.class, () -> clientSocket.session.suspend());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem
private final EventDriver websocket;
private final Executor executor;
private final WebSocketPolicy policy;
private final AtomicBoolean closed = new AtomicBoolean();
private final AtomicBoolean onCloseCalled = new AtomicBoolean(false);
private ClassLoader classLoader;
private ExtensionFactory extensionFactory;
private RemoteEndpointFactory remoteEndpointFactory;
Expand All @@ -80,7 +80,6 @@ public class WebSocketSession extends ContainerLifeCycle implements Session, Rem
private UpgradeRequest upgradeRequest;
private UpgradeResponse upgradeResponse;
private CompletableFuture<Session> openFuture;
private AtomicBoolean onCloseCalled = new AtomicBoolean(false);

public WebSocketSession(WebSocketContainerScope containerScope, URI requestURI, EventDriver websocket, LogicalConnection connection)
{
Expand Down Expand Up @@ -338,10 +337,9 @@ public void incomingFrame(Frame frame)
public boolean isOpen()
{
if (this.connection == null)
{
return false;
}
return !closed.get() && this.connection.isOpen();

return !onCloseCalled.get() && this.connection.isOpen();
}

@Override
Expand Down Expand Up @@ -546,6 +544,9 @@ public void setUpgradeResponse(UpgradeResponse response)
@Override
public SuspendToken suspend()
{
if (onCloseCalled.get())
throw new IllegalStateException("Not open");

return connection.suspend();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,8 @@ Action getAction(ByteBuffer buffer)

/**
* Requests that reads from the connection be suspended.
*
* @return whether the suspending was successful
*/
boolean suspending()
void suspending()
{
synchronized (this)
{
Expand All @@ -101,9 +99,7 @@ boolean suspending()
{
case READING:
state = State.SUSPENDING;
return true;
case EOF:
return false;
break;
default:
throw new IllegalStateException(toString(state));
}
Expand Down Expand Up @@ -131,8 +127,6 @@ ByteBuffer resume()
ByteBuffer bb = buffer;
buffer = null;
return bb;
case EOF:
return null;
default:
throw new IllegalStateException(toString(state));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class ReadStateTest
{
Expand All @@ -50,7 +49,7 @@ public void testSuspendingThenResume()
ReadState readState = new ReadState();
assertThat("Initially reading", readState.isReading(), is(true));

assertTrue(readState.suspending());
readState.suspending();
assertThat("Suspending doesn't take effect immediately", readState.isSuspended(), is(false));

assertNull(readState.resume());
Expand All @@ -64,7 +63,7 @@ public void testSuspendingThenSuspendThenResume()
ReadState readState = new ReadState();
assertThat("Initially reading", readState.isReading(), is(true));

assertThat(readState.suspending(), is(true));
readState.suspending();
assertThat("Suspending doesn't take effect immediately", readState.isSuspended(), is(false));

ByteBuffer content = BufferUtil.toBuffer("content");
Expand All @@ -84,8 +83,8 @@ public void testEof()

assertThat(readState.isReading(), is(false));
assertThat(readState.isSuspended(), is(true));
assertThat(readState.suspending(), is(false));
assertThrows(IllegalStateException.class, readState::suspending);
assertThat(readState.getAction(content), is(ReadState.Action.EOF));
assertNull(readState.resume());
assertThrows(IllegalStateException.class, readState::resume);
}
}

0 comments on commit 3edc6c9

Please sign in to comment.