我正在尝试使用 karate.signal 和 karate.listen 编写异步 Webhook 回调测试,如 https://github.com/karatelabs/karate/tree/master#async 中所述 我已将测试分成两个功能文件。一种用于提出空手道的请求,另一种用于处理空手道的请求。然而,即使发出信号调用,监听也总是超时。
测试功能文件
Feature: Check Redis Event Q impl
Background:
* url baseUrl
* def startmock = (mockName) => karate.start({mock:mockName,port:mockServerPort}).port
* def mockServerUrl = 'http://localhost:'+ startmock('localTestServer.mock') +'/api/response'
Scenario: Run ack immediately
Given path servicePrefix, 'testq'
And request { cbUrl:'#(mockServerUrl)' , deferCount : 0, deferSecs:0 }
When method POST
Then status 200
* listen 1000
* print listenResult
* json data = listenResult
* match data.count == 0
模拟服务器功能文件
Feature: stateful mock server
Background:
* def prop = function(name){ return java.lang.System.getProperty(name) }
* def uuid = function(){ return java.util.UUID.randomUUID() + '' }
* print "started mock"
Scenario: pathMatches('/api/response') && methodIs('post')
* def responseStatus = 200
* print 'signalling'
* karate.signal(request)
* print 'signalled'
# This a catch all scenario handles every request, that hasn't been handled before.
Scenario:
* print 'No dedicated scenario matches incoming request.'
* print 'With Headers:'
* print requestHeaders
* print 'With Request Parameters'
* print requestParams
* print 'And Request:'
* print request
* def responseStatus = 404
* def response = {status:'OK'}
但是监听总是超时并且没有得到有信号的对象。日志似乎表明监听和信号正在发生,但监听总是超时
日志片段
10:59:23.721 [main] DEBUG com.intuit.karate - entered listen state with timeout: 1000
10:59:23.776 [pool-2-thread-1] INFO c.a.connect.bridge.http.HttpClient - Doing POST to url http://localhost:60812/api/response with body {"count":0,"time":0} and headers DefaultHttpHeaders[x-request-id: f9f0a598-fef3-4cc6-8c7e-a42c9aafa551, x-trail: e0c13a9f-0bdd-43a2-97a3-f79e52731f67;MISC_Q_c2715d2e-684a-46dd-9408-89d308c2f6cd;f9f0a598-fef3-4cc6-8c7e-a42c9aafa551, content-type: application/json]
10:59:23.886 [armeria-common-worker-nio-9-1] INFO c.l.a.i.common.JavaVersionSpecific - Using the APIs optimized for: Java 9+
10:59:23.925 [armeria-common-worker-nio-9-1] DEBUG com.intuit.karate - scenario matched at line 10: pathMatches('/api/response') && methodIs('post')
10:59:23.928 [armeria-common-worker-nio-9-1] INFO com.intuit.karate - [print] signalling
10:59:23.938 [armeria-common-worker-nio-9-1] INFO com.intuit.karate - [print] signalled
10:59:23.950 [pool-2-thread-1] INFO c.a.c.c.w.RedisQTestEventProcessor - Finished Event
10:59:23.956 [pool-2-thread-1] INFO c.a.c.b.worker.redisq.LeaseHandler - Worker Task Done
10:59:24.725 [main] ERROR com.intuit.karate - listen timed out: java.util.concurrent.TimeoutException
10:59:24.726 [main] DEBUG com.intuit.karate - exit listen state with result: null
完整日志
10:59:23.435 [main] DEBUG com.intuit.karate - request:
1 > POST http://localhost:60811//testq
1 > Content-Type: application/json; charset=UTF-8
1 > Content-Length: 80
1 > Host: localhost:60811
1 > Connection: Keep-Alive
1 > User-Agent: Apache-HttpClient/4.5.13 (Java/11.0.20.1)
1 > Accept-Encoding: gzip,deflate
{"cbUrl":"http:\/\/localhost:60812\/api\/response","deferCount":0,"deferSecs":0}
10:59:23.547 [nioEventLoopGroup-4-1] INFO c.a.c.bridge.http.HttpRequestHandler - Got body {"cbUrl":"http:\/\/localhost:60812\/api\/response","deferCount":0,"deferSecs":0}
10:59:23.657 [nioEventLoopGroup-4-1] INFO c.a.c.b.w.redisq.WorkerPollerMgr - Q Insert
10:59:23.713 [nioEventLoopGroup-4-1] INFO c.a.c.bridge.http.RequestIdStamper - done
10:59:23.719 [main] DEBUG com.intuit.karate - response time in milliseconds: 283
1 < 200
1 < content-length: 15
1 < content-type: text/plain
1 < x-request-id: e0c13a9f-0bdd-43a2-97a3-f79e52731f67
EmptyResource{}
10:59:23.721 [main] DEBUG com.intuit.karate - entered listen state with timeout: 1000
10:59:23.776 [pool-2-thread-1] INFO c.a.connect.bridge.http.HttpClient - Doing POST to url http://localhost:60812/api/response with body {"count":0,"time":0} and headers DefaultHttpHeaders[x-request-id: f9f0a598-fef3-4cc6-8c7e-a42c9aafa551, x-trail: e0c13a9f-0bdd-43a2-97a3-f79e52731f67;MISC_Q_c2715d2e-684a-46dd-9408-89d308c2f6cd;f9f0a598-fef3-4cc6-8c7e-a42c9aafa551, content-type: application/json]
10:59:23.886 [armeria-common-worker-nio-9-1] INFO c.l.a.i.common.JavaVersionSpecific - Using the APIs optimized for: Java 9+
10:59:23.925 [armeria-common-worker-nio-9-1] DEBUG com.intuit.karate - scenario matched at line 10: pathMatches('/api/response') && methodIs('post')
10:59:23.928 [armeria-common-worker-nio-9-1] INFO com.intuit.karate - [print] signalling
10:59:23.938 [armeria-common-worker-nio-9-1] INFO com.intuit.karate - [print] signalled
10:59:23.950 [pool-2-thread-1] INFO c.a.c.c.w.RedisQTestEventProcessor - Finished Event
10:59:23.956 [pool-2-thread-1] INFO c.a.c.b.worker.redisq.LeaseHandler - Worker Task Done
10:59:24.725 [main] ERROR com.intuit.karate - listen timed out: java.util.concurrent.TimeoutException
10:59:24.726 [main] DEBUG com.intuit.karate - exit listen state with result: null
10:59:24.732 [main] INFO com.intuit.karate - [print] null
10:59:24.733 [main] ERROR com.intuit.karate - classpath:com/eventq/eventq.feature:16
* json data = listenResult
cannot convert to json: [type: NULL, value: null]
classpath:com/eventq/eventq.feature:16
---------------------------------------------------------
feature: classpath:com/eventq/eventq.feature
scenarios: 1 | passed: 0 | failed: 1 | time: 1.6729
---------------------------------------------------------
org.opentest4j.AssertionFailedError: cannot convert to json: [type: NULL, value: null]
我正在使用空手道版本1.2.0
您不能在两个不同的功能文件或场景之间进行交谈,请仔细阅读:https://stackoverflow.com/a/46080568/143475