Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Forward early websocket client errors to client future, not the webso…
…cket bean (#8300) When the websocket is closed, but the session has not yet been initialized (and OnOpen not been called), change handleCloseReason to instead emit an error on the publisher that is returned by the `WebSocketClient.connect` method. This means the publisher won't get stuck, and the OnClose method won't be called without a session being available. Additionally, I refactored `AbstractNettyWebSocketHandler` and the subclasses a bit. The bodyArgument/pongArgument code, and the old callOpenMethod, were only used for the server handler. Hopefully fixes #7921
- Loading branch information
Showing
4 changed files
with
215 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
http-client/src/test/groovy/io/micronaut/http/client/websocket/ClientWebsocketSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package io.micronaut.http.client.websocket | ||
|
||
import io.micronaut.context.ApplicationContext | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.websocket.WebSocketClient | ||
import io.micronaut.websocket.annotation.ClientWebSocket | ||
import io.micronaut.websocket.annotation.OnClose | ||
import io.micronaut.websocket.annotation.OnMessage | ||
import io.micronaut.websocket.annotation.OnOpen | ||
import io.micronaut.websocket.exceptions.WebSocketClientException | ||
import jakarta.inject.Inject | ||
import jakarta.inject.Singleton | ||
import reactor.core.publisher.Mono | ||
import spock.lang.Specification | ||
|
||
import java.util.concurrent.ExecutionException | ||
|
||
class ClientWebsocketSpec extends Specification { | ||
void 'websocket bean should not open if there is a connection error'() { | ||
given: | ||
def ctx = ApplicationContext.run(['spec.name': 'ClientWebsocketSpec']) | ||
def client = ctx.getBean(WebSocketClient) | ||
def registry = ctx.getBean(ClientBeanRegistry) | ||
def mono = Mono.from(client.connect(ClientBean.class, 'http://does-not-exist')) | ||
|
||
when: | ||
mono.toFuture().get() | ||
then: | ||
def e = thrown ExecutionException | ||
e.cause instanceof WebSocketClientException | ||
|
||
registry.clientBeans.size() == 1 | ||
!registry.clientBeans[0].opened | ||
!registry.clientBeans[0].autoClosed | ||
!registry.clientBeans[0].onClosed | ||
|
||
cleanup: | ||
client.close() | ||
} | ||
|
||
@Singleton | ||
@Requires(property = 'spec.name', value = 'ClientWebsocketSpec') | ||
static class ClientBeanRegistry { | ||
List<ClientBean> clientBeans = new ArrayList<>() | ||
} | ||
|
||
@ClientWebSocket | ||
static class ClientBean implements AutoCloseable { | ||
boolean opened = false | ||
boolean onClosed = false | ||
boolean autoClosed = false | ||
|
||
@Inject | ||
ClientBean(ClientBeanRegistry registry) { | ||
registry.clientBeans.add(this) | ||
} | ||
|
||
@OnOpen | ||
void open() { | ||
opened = true | ||
} | ||
|
||
@OnMessage | ||
void onMessage(String text) { | ||
} | ||
|
||
@OnClose | ||
void onClose() { | ||
onClosed = true | ||
} | ||
|
||
@Override | ||
void close() throws Exception { | ||
autoClosed = true | ||
} | ||
} | ||
} |
Oops, something went wrong.