Skip to content

Commit

Permalink
feat: allow further customization of websocket webclient (#3315)
Browse files Browse the repository at this point in the history
---------

Signed-off-by: Pablo Hernán Carle <[email protected]>
Signed-off-by: Pavel Jares <[email protected]>
Co-authored-by: Pablo Hernán Carle <[email protected]>
Co-authored-by: Pavel Jares <[email protected]>
  • Loading branch information
3 people authored Feb 20, 2024
1 parent 0c6894e commit 7e8d855
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,19 @@ public class WebSocketClientFactory {

@Autowired
public WebSocketClientFactory(
SslContextFactory.Client jettyClientSslContextFactory,
@Value("${server.webSocket.maxIdleTimeout:3600000}") int maxIdleWebSocketTimeout
SslContextFactory.Client jettyClientSslContextFactory,
@Value("${server.webSocket.maxIdleTimeout:3600000}") int maxIdleWebSocketTimeout,
@Value("${server.webSocket.connectTimeout:15000}") long connectTimeout,
@Value("${server.webSocket.stopTimeout:30000}") long stopTimeout,
@Value("${server.webSocket.asyncWriteTimeout:60000}") long asyncWriteTimeout
) {
log.debug("Creating Jetty WebSocket client, with SslFactory: {}",
jettyClientSslContextFactory);
WebSocketClient wsClient = new WebSocketClient(new HttpClient(jettyClientSslContextFactory));
wsClient.setMaxIdleTimeout(maxIdleWebSocketTimeout);
wsClient.setConnectTimeout(connectTimeout);
wsClient.setStopTimeout(stopTimeout);
wsClient.setAsyncWriteTimeout(asyncWriteTimeout);
client = new JettyWebSocketClient(wsClient);
client.start();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* This program and the accompanying materials are made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-v20.html
*
* SPDX-License-Identifier: EPL-2.0
*
* Copyright Contributors to the Zowe Project.
*/

package org.zowe.apiml.gateway.ws;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.websocket.api.WebSocketPolicy;
import org.eclipse.jetty.websocket.client.WebSocketClient;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.socket.client.jetty.JettyWebSocketClient;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.*;

@SpringBootTest(
properties = {
"server.webSocket.maxIdleTimeout=10000",
"server.webSocket.connectTimeout=1000",
"server.webSocket.stopTimeout=500",
"server.webSocket.asyncWriteTimeout=1500",
},
classes = { WebSocketClientFactory.class }
)
@MockBean(SslContextFactory.Client.class)
@ActiveProfiles("WebSocketClientFactoryContextTest")
public class WebSocketClientFactoryContextTest {

@Autowired
private WebSocketClientFactory webSocketClientFactory;

@Nested
class GivenWebSocketClientParametrization {

@Test
void thenBeanIsInitialized() {
assertNotNull(webSocketClientFactory);

JettyWebSocketClient jettyWebSocketClient = (JettyWebSocketClient) ReflectionTestUtils.getField(webSocketClientFactory, "client");
WebSocketClient webSocketClient = (WebSocketClient) ReflectionTestUtils.getField(jettyWebSocketClient, "client");

WebSocketPolicy policy = webSocketClient.getPolicy();
HttpClient httpClient = webSocketClient.getHttpClient();

assertEquals(10000, policy.getIdleTimeout());
assertEquals(1000, httpClient.getConnectTimeout());
assertEquals(500, webSocketClient.getStopTimeout());
assertEquals(1500, policy.getAsyncWriteTimeout());
}

}

@Nested
class GivenFactory {

private JettyWebSocketClient client = mock(JettyWebSocketClient.class);
private WebSocketClientFactory factory = new WebSocketClientFactory(client);

@Test
void whenIsRunning_thenStop() {
doReturn(true).when(client).isRunning();
factory.closeClient();
verify(client).stop();
}

@Test
void whenIsNotRunning_thenDontStop() {
factory.closeClient();
verify(client, never()).stop();
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class CreatedInstanceWithConfig {
@BeforeEach
void setUp() {
SslContextFactory.Client sslClient = mock(SslContextFactory.Client.class);
this.webSocketClientFactory = new WebSocketClientFactory(sslClient, 1234);
this.webSocketClientFactory = new WebSocketClientFactory(sslClient, 1234, 0, 0, 0);
}

@Test
Expand Down

0 comments on commit 7e8d855

Please sign in to comment.