|
17 | 17 |
|
18 | 18 | package org.apache.shenyu.springboot.plugin.websocket; |
19 | 19 |
|
| 20 | +import io.netty.channel.ChannelInboundHandlerAdapter; |
| 21 | +import io.netty.channel.ChannelPipeline; |
| 22 | +import io.netty.channel.embedded.EmbeddedChannel; |
| 23 | +import io.netty.handler.timeout.IdleStateHandler; |
| 24 | +import io.netty.handler.timeout.ReadTimeoutHandler; |
| 25 | +import io.netty.handler.timeout.WriteTimeoutHandler; |
20 | 26 | import org.apache.shenyu.common.config.ShenyuConfig; |
21 | 27 | import org.apache.shenyu.common.enums.PluginEnum; |
22 | 28 | import org.apache.shenyu.plugin.api.context.ShenyuContextDecorator; |
|
31 | 37 | import org.springframework.test.util.ReflectionTestUtils; |
32 | 38 | import org.springframework.web.reactive.socket.client.ReactorNettyWebSocketClient; |
33 | 39 | import org.springframework.web.reactive.socket.server.WebSocketService; |
| 40 | +import reactor.core.publisher.Mono; |
| 41 | +import reactor.netty.DisposableServer; |
| 42 | +import reactor.netty.http.client.HttpClient; |
34 | 43 | import reactor.netty.http.client.WebsocketClientSpec; |
| 44 | +import reactor.netty.http.server.HttpServer; |
35 | 45 |
|
| 46 | +import java.net.URI; |
| 47 | +import java.time.Duration; |
| 48 | +import java.util.concurrent.TimeUnit; |
36 | 49 | import java.util.function.Supplier; |
37 | 50 |
|
38 | 51 | import static org.assertj.core.api.Assertions.assertThat; |
39 | 52 | import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 53 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
40 | 54 |
|
41 | 55 | /** |
42 | 56 | * Test case for {@link WebSocketPluginConfiguration}. |
@@ -89,6 +103,56 @@ public void testReactorNettyWebSocketClient() { |
89 | 103 | ); |
90 | 104 | } |
91 | 105 |
|
| 106 | + @Test |
| 107 | + public void testRemoveHttpTimeoutHandlers() { |
| 108 | + EmbeddedChannel channel = new EmbeddedChannel(); |
| 109 | + ChannelPipeline pipeline = channel.pipeline(); |
| 110 | + pipeline.addLast(new IdleStateHandler(3, 3, 3, TimeUnit.SECONDS)); |
| 111 | + pipeline.addLast(new ReadTimeoutHandler(3, TimeUnit.SECONDS)); |
| 112 | + pipeline.addLast(new WriteTimeoutHandler(3, TimeUnit.SECONDS)); |
| 113 | + pipeline.addLast("keep", new ChannelInboundHandlerAdapter()); |
| 114 | + |
| 115 | + WebSocketPluginConfiguration.removeHandler(pipeline, IdleStateHandler.class); |
| 116 | + WebSocketPluginConfiguration.removeHandler(pipeline, ReadTimeoutHandler.class); |
| 117 | + WebSocketPluginConfiguration.removeHandler(pipeline, WriteTimeoutHandler.class); |
| 118 | + |
| 119 | + assertThat(pipeline.get(IdleStateHandler.class)).isNull(); |
| 120 | + assertThat(pipeline.get(ReadTimeoutHandler.class)).isNull(); |
| 121 | + assertThat(pipeline.get(WriteTimeoutHandler.class)).isNull(); |
| 122 | + assertThat(pipeline.get("keep")).isNotNull(); |
| 123 | + channel.finishAndReleaseAll(); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + public void testWebSocketSurvivesHttpTimeouts() { |
| 128 | + DisposableServer server = HttpServer.create() |
| 129 | + .port(0) |
| 130 | + .handle((request, response) -> response.sendWebsocket((in, out) -> out.send(in.receive().retain()))) |
| 131 | + .bindNow(); |
| 132 | + try { |
| 133 | + // Mirrors the shared client built by HttpClientFactory: the three connection-level |
| 134 | + // timeout handlers are added through doOnConnected and responseTimeout is 3s by default. |
| 135 | + HttpClient httpClient = HttpClient.create() |
| 136 | + .responseTimeout(Duration.ofSeconds(3)) |
| 137 | + .doOnConnected(connection -> { |
| 138 | + connection.addHandlerLast(new IdleStateHandler(3, 3, 3, TimeUnit.SECONDS)); |
| 139 | + connection.addHandlerLast(new WriteTimeoutHandler(3, TimeUnit.SECONDS)); |
| 140 | + connection.addHandlerLast(new ReadTimeoutHandler(3, TimeUnit.SECONDS)); |
| 141 | + }); |
| 142 | + ReactorNettyWebSocketClient client = |
| 143 | + new ReactorNettyWebSocketClient(WebSocketPluginConfiguration.webSocketHttpClient(httpClient)); |
| 144 | + client.execute(URI.create("ws://localhost:" + server.port() + "/"), session -> |
| 145 | + session.send(Mono.just(session.textMessage("first"))) |
| 146 | + .then(Mono.delay(Duration.ofSeconds(4))) |
| 147 | + .then(Mono.fromRunnable(() -> assertTrue(session.isOpen()))) |
| 148 | + .then(session.send(Mono.just(session.textMessage("second")))) |
| 149 | + .then()) |
| 150 | + .block(Duration.ofSeconds(15)); |
| 151 | + } finally { |
| 152 | + server.disposeNow(); |
| 153 | + } |
| 154 | + } |
| 155 | + |
92 | 156 | @Test |
93 | 157 | public void testHandshakeWebSocketService() { |
94 | 158 | applicationContextRunner.run(context -> { |
|
0 commit comments