Asynchronous To Synchronous
WebSocket messages are asynchronous in nature. To make writing tests easier, WebTau receives and stores all messages behind the scene. Messages are exposed via session.received special value. You can validate, wait on or poll from in a synchronous manner.
Wait For A Specific Message
Use and waitTo to make sure a specific message was received. WebTau will comb through and discard all the received messages that do not match a criteria. Messages are processed in receive order. Groovy def wsSession = websocket.connect("/prices") wsSession.received.waitTo == [ price: greaterThan(100), symbol: "IBM"] Check web-socket/import-and-dependencies Import And Dependencies for prerequisites. Java var wsSession = websocket.connect("/prices"); wsSession.received.waitTo(equal(map( "price", greaterThan(100), "symbol", "IBM"))); Check web-socket/import-and-dependencies Import And Dependencies for prerequisites. > waiting for received from ws://localhost:44553/prices to equal {"price": <greater than 100>, "symbol": "IBM"} > [1/25] polling websocket message {"symbol": "IBM", "price": 77} . [1/25] polled new message (25ms) > [25/25] polling websocket message {"symbol": "IBM", "price": 101} . [25/25] polled new message (0ms) . received from ws://localhost:44553/prices equals {"price": <greater than 100>, "symbol": "IBM"} (1s 29ms)
Wait For A Specific Message Using Value Path
Use get(path) to narrow to a specific response value Groovy wsSession.received.price.waitToBe > 100 Note: Groovy dynamic nature allows you bypass explicit get Java wsSession.received.get("price").waitToBe(greaterThan(100)); > waiting for received.price from ws://localhost:44553/prices to be greater than 100 > [1/25] polling websocket message {"symbol": "IBM", "price": 77} . [1/25] polled new message (0ms) > [25/25] polling websocket message {"symbol": "IBM", "price": 101} . [25/25] polled new message (0ms) . received.price from ws://localhost:44553/prices greater than 100 (1s 1ms) Use [idx] To deal with a list response: Groovy wsSession.received[2].price.waitToBe > 30 Java wsSession.received.get("[2].price").waitToBe(greaterThan(30)); > waiting for received[2].price from ws://localhost:44553/prices to be greater than 30 > polling websocket message [{"symbol": "IBM", "price": 20}, {"symbol": "IBM", "price": 30}, {"symbol": "IBM", "price": 33}] . polled new message (0ms) . received[2].price from ws://localhost:44553/prices greater than 30 (0ms)
WebTau receives and stores asynchronous messages behind the scene. Use pollAsText to access messages in a synchronous manner. If message is not yet received, pollAsText will wait for a configured ( webSocketPollTimeout ) milliseconds (default 5 seconds). In example above, we wait until the price becomes greater than 100. All the messages including the matched one are discarded after waitTo . returns next received message or wait for the message to come: Groovy def nextMessage = wsSession.received.pollAsText() nextMessage.should == "{\"symbol\":\"IBM\",\"price\":102}" def nextNextMessage = wsSession.received.pollAsText(100) // explicit timeout in milliseconds for new message to arrive nextNextMessage.should == "{\"symbol\":\"IBM\",\"price\":103}" Java String nextMessage = wsSession.received.pollAsText(); actual(nextMessage).should(equal("{\"symbol\":\"IBM\",\"price\":102}")); String nextNextMessage = wsSession.received.pollAsText(100); // explicit timeout in milliseconds for new message to arrive actual(nextNextMessage).should(equal("{\"symbol\":\"IBM\",\"price\":103}")); Note: if there are no already received messages, and no new message arrives within a wait time, null will be returned.
Use to convert JSON message to a list or a map. Groovy def message = wsSession.received.poll() message.symbol.should == "IBM" Java Map<String, ?> message = wsSession.received.poll(); actual(message.get("symbol")).should(equal("IBM"));
By default, WebTau keeps only 1000 messages. If new messages arrive, the old ones get discarded. Use config value to change number of messages to keep.
Number Of Received Messages
Use to wait for a certain number of messages: Groovy wsSession.received.count.waitTo == 53 Java wsSession.received.count.waitTo(equal(53)); > waiting for count of messages received from ws://localhost:33587/prices to equal 53 . count of messages received from ws://localhost:33587/prices equals 53 (124ms)
Use to remove all already received messages. Next will wait for a new message to arrive as all the received messages will be discarded. Groovy wsSession.received.discard() def nextMessage = wsSession.received.pollAsText(1) nextMessage.should == null Java wsSession.received.discard(); String nextMessage = wsSession.received.pollAsText(1); actual(nextMessage).should(equal(null)); . discarded all messages received from ws://localhost:33587/prices (0ms) > polling websocket message . no new message is polled (1ms)