fabric8io / mockwebserver

An extension of okhttp's mockwebserver, that provides a DSL and is easier to use
Apache License 2.0
113 stars 38 forks source link

WebSocket session is not closed after all expected messages were received #75

Open kuzznya opened 2 years ago

kuzznya commented 2 years ago

Existing WebSocket session is not closed after processing all expected messages with such configuration:

server.expect().withPath("/ws")
    .andUpgradeToWebSocket()
    .open()
    .expect("input")
    .andEmit(new OutputStreamMessage("output"))
    .once()
    .done()
    .always();

If my understanding is correct, session should close after "input" is received (if not, please correct me and I'll close the issue). This is not happening due to the check in io.fabric8.mockserver.internal.WebSocketSession::closeActiveSocketsIfApplicable:

public void closeActiveSocketsIfApplicable() {
        if (pendingMessages.isEmpty() && requestEvents.isEmpty() && httpRequestEvents.isEmpty()
          && sentWebSocketMessagesRequestEvents.isEmpty()) {
            activeSockets.forEach(ws -> ws.close(1000, "Closing..."));
        }
 }

i.e. requestEvents.isEmpty() - it is never empty when there were some expected messages because even after receiving expected message it still contains entry "input": <Empty queue>

shawkins commented 1 year ago

This is still an issue, proactive closure support is inhibited.

Also related to this the mock server does not respect an incoming close message - it likely should echo that back to be well behaved. - that appeared to be the case behaviorally in a test, but upon checking the source it does indeed immediately echo.