重新使用空手道DSL的websocket连接

时间:2019-04-02 17:51:44

标签: websocket karate

使用Websocket时,您通常不会连续打开和关闭连接,这就是为什么我尝试对功能文件中的所有测试使用相同的Websocket,而我的编程逻辑也要求这样做,因为我目前不支持以下逻辑会话: Websocket的顶部。

我尝试了以下callcallonce的不同变体,但对我不起作用。

Feature: Notify about created fixtures

    Background:
        * def openWs = call read ('classpath:openWebsocket.js')
        * def ws = callonce openWs

    Scenario: Request a new session
        * def createSessionRequest = { 'action': 'POST', 'resource': 'sessions', 'requestId': 'df9a64de-3c22-40f8-807a-7eba5a432c95', 'version': 1 }

        * string createSessionRequest = createSessionRequest
        * eval ws.send(createSessionRequest)

        * json response = karate.listen(5000)
        * match response == { requestId: 'df9a64de-3c22-40f8-807a-7eba5a432c95', status: 201, version: 1, payload: { sessionId: '#string' } }

    Scenario: Subscribe for fixture updates
        * def subscribeFixtureRequest = { 'action': 'SUB', 'resource': 'fixtures', 'requestId': 'fa9eb7a5-d658-4f4b-bfa5-bc3bf2909cf3', 'version': 1 }

        * string subscribeFixtureRequest = subscribeFixtureRequest
        * eval ws.send(subscribeFixtureRequest)

        * json response = karate.listen(5000)
        * match response == { requestId: 'fa9eb7a5-d658-4f4b-bfa5-bc3bf2909cf3', status: 200, version: 1 }

看起来它缓存了websocket实例,但是在第一个场景之后仍然关闭它,因此第二个场景失败了。

日志如下所示(删除了一些不相关的行):

19:42:13.003 [main] TRACE com.intuit.karate - callonce waiting for lock: openWs
19:42:13.003 [main] INFO  com.intuit.karate - >> lock acquired, begin callonce: openWs
19:42:13.083 [nioEventLoopGroup-3-1] DEBUG c.i.k.netty.WebSocketClientHandler - websocket client connected
19:42:13.083 [main] INFO  com.intuit.karate - << lock released, cached callonce: openWs
19:42:13.092 [main] TRACE c.i.karate.netty.WebSocketClient - sent: {"action":"POST","resource":"sessions","requestId":"df9a64de-3c22-40f8-807a-7eba5a432c95","version":1}
19:42:13.093 [main] TRACE com.intuit.karate - entered listen wait state
19:42:13.169 [nioEventLoopGroup-3-1] TRACE c.i.k.netty.WebSocketClientHandler - websocket received text
19:42:13.169 [nioEventLoopGroup-3-1] TRACE com.intuit.karate - signal called: {"payload":{"sessionId":"ee542db6-808c-4100-b009-f9e22dce28db"},"requestId":"df9a64de-3c22-40f8-807a-7eba5a432c95","status":201,"version":1}
19:42:13.170 [main] TRACE com.intuit.karate - exit listen wait state, result: {"payload":{"sessionId":"ee542db6-808c-4100-b009-f9e22dce28db"},"requestId":"df9a64de-3c22-40f8-807a-7eba5a432c95","status":201,"version":1}
19:42:13.203 [nioEventLoopGroup-3-1] DEBUG c.i.k.netty.WebSocketClientHandler - websocket closing
19:42:13.204 [nioEventLoopGroup-3-1] DEBUG c.i.k.netty.WebSocketClientHandler - websocket client disconnected
19:42:13.249 [main] TRACE com.intuit.karate - callonce cache hit for: openWs
19:42:13.256 [main] TRACE c.i.karate.netty.WebSocketClient - sent: {"action":"SUB","resource":"fixtures","requestId":"fa9eb7a5-d658-4f4b-bfa5-bc3bf2909cf3","version":1}
19:42:13.257 [main] TRACE com.intuit.karate - entered listen wait state
19:42:18.257 [main] TRACE com.intuit.karate - exit listen wait state, result: null

有没有办法自己控制websocket的打开和关闭?

编辑:很遗憾,我检查了源代码并将其紧紧地绑在ScenarioExecutionUnit上,这在Scenario的结尾将其关闭。似乎需要对空手道进行代码更改以支持此操作。

1 个答案:

答案 0 :(得分:1)

空手道将在将来对此提供支持:https://github.com/intuit/karate/issues/718