Skip to content

Commit

Permalink
support multiple websocket clients in a scenario karatelabs#395
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrthomas committed Nov 2, 2018
1 parent 8cb881e commit a626597
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public class ScenarioContext {

// websocket
private final Object LOCK = new Object();
private WebSocketClient webSocketClient;
private List<WebSocketClient> webSocketClients;
private Object signalResult;

public void logLastPerfEvent(String failureMessage) {
Expand Down Expand Up @@ -150,11 +150,7 @@ public HttpRequest getPrevRequest() {

public HttpClient getHttpClient() {
return client;
}

public WebSocketClient getWebSocketClient() {
return webSocketClient;
}
}

public int getCallDepth() {
return callDepth;
Expand Down Expand Up @@ -288,7 +284,7 @@ private ScenarioContext(ScenarioContext sc, ScenarioInfo info) {
prevRequest = sc.prevRequest;
prevPerfEvent = sc.prevPerfEvent;
callResults = sc.callResults;
webSocketClient = sc.webSocketClient;
webSocketClients = sc.webSocketClients;
signalResult = sc.signalResult;
}

Expand Down Expand Up @@ -825,16 +821,22 @@ public void embed(byte[] bytes, String contentType) {
}

// websocket / async =======================================================

private WebSocketClient createWebSocketClient(String url, Consumer<String> textHandler, Consumer<byte[]> binaryHandler) {
WebSocketClient webSocketClient = new WebSocketClient(url, textHandler, binaryHandler);
if (webSocketClients == null) {
webSocketClients = new ArrayList();
}
webSocketClients.add(webSocketClient);
return webSocketClient;
}

public void webSocket(String url, Consumer<String> textHandler) {
webSocket(url, textHandler, null);
public WebSocketClient webSocket(String url, Consumer<String> textHandler) {
return webSocket(url, textHandler, null);
}

public void webSocket(String url, Consumer<String> textHandler, Consumer<byte[]> binaryHandler) {
if (webSocketClient != null) {
webSocketClient.close();
}
webSocketClient = new WebSocketClient(url, textHandler, binaryHandler);
public WebSocketClient webSocket(String url, Consumer<String> textHandler, Consumer<byte[]> binaryHandler) {
return createWebSocketClient(url, textHandler, binaryHandler);
}

public void signal(Object result) {
Expand Down Expand Up @@ -902,8 +904,8 @@ public void submit(String name) {
}

public void stop() {
if (webSocketClient != null) {
webSocketClient.close();
if (webSocketClients != null) {
webSocketClients.forEach(WebSocketClient::close);
}
if (driver != null) {
driver.quit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,7 @@ public WebSocketClient webSocket(String url, Consumer<String> textHandler) {
}

public WebSocketClient webSocket(String url, Consumer<String> textHandler, Consumer<byte[]> binaryHandler) {
context.webSocket(url, textHandler, binaryHandler);
return context.getWebSocketClient();
return context.webSocket(url, textHandler, binaryHandler);
}

public void signal(Object result) {
Expand Down
4 changes: 2 additions & 2 deletions karate-demo/src/test/java/demo/websocket/echo.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Feature: public test at
http://www.websocket.org/echo.html

Scenario: text messages
Given def handler = function(msg) { karate.signal(msg) }
Given def handler = function(msg){ karate.signal(msg) }
And def socket = karate.webSocket('ws://echo.websocket.org', handler)
When eval socket.send('hello world!')
And def result = karate.listen(5000)
Expand All @@ -14,7 +14,7 @@ Scenario: text messages
Then match result == 'another test'

Scenario: binary message
Given def handler = function(msg) { karate.signal(msg) }
Given def handler = function(msg){ karate.signal(msg) }
And def socket = karate.webSocket('ws://echo.websocket.org', null, handler)
And bytes data = read('../upload/test.pdf')
When eval socket.sendBytes(data)
Expand Down

0 comments on commit a626597

Please sign in to comment.