Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset the signal after listening for a specific message. This allows… #2004

Merged
merged 1 commit into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ public void signal(Object result) {
public synchronized Object listen(long timeout) {
try {
logger.trace("entered listen wait state");
return SIGNAL.get(timeout, TimeUnit.MILLISECONDS);
Object result = SIGNAL.get(timeout, TimeUnit.MILLISECONDS);
SIGNAL = new CompletableFuture();
return result;
} catch (Exception e) {
logger.error("listen timed out: {}", e + "");
return null;
Expand Down
22 changes: 22 additions & 0 deletions karate-demo/src/test/java/demo/websocket/websocket.feature
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,25 @@ Scenario: using the websocket instance to send as well as receive messages
* socket.send('Billie')
* def result = socket.listen(5000)
* match result == 'hello Billie !'

Scenario: listen for multiple websocket messages
* def handler = function(msg){ return msg.startsWith('hello') }
* def socket = karate.webSocket(demoBaseUrl + '/websocket', handler)
* socket.send('Billie')
* def result = socket.listen(5000)
* match result == 'hello Billie !'
* socket.send('Bob')
* def result = socket.listen(5000)
* match result == 'hello Bob !'

Scenario: change the websocket handler for messages
* def handler = function(msg){ return msg.contains('Billie') }
* def socket = karate.webSocket(demoBaseUrl + '/websocket', handler)
* socket.send('Billie')
* def result = socket.listen(5000)
* match result == 'hello Billie !'
* def handler = function(msg){ return msg.contains('Bob') }
* socket.setTextHandler(karate.toJava(handler))
* socket.send('Bob')
* def result = socket.listen(5000)
* match result == 'hello Bob !'