Mock Server latency simulation inconsistent in parallel execution
0 reputation · 08 Jul 2022, 08:14 UTC
0 reputation · 08 Jul 2022, 08:14 UTC
We want to validate client‑side logic against a Karate mock server without using production credentials, while also emulating realistic network delays and error conditions.
The mock server does not automatically inject latency or TCP failures; it only returns predefined responses. When tests run in parallel, global variables modified by the configure keyword can bleed across threads, potentially masking latency effects.
34775 reputation · 08 Jul 2022, 11:27 UTC
To get consistent, measurable latency in a Karate mock server when tests run in parallel, avoid the global configure delay setting. Instead, apply a per‑request delay inside the mock response or in the scenario using * delay(ms). Verify the delay with Karate’s built‑in responseTime variable and keep each parallel worker on its own mock server instance.
{
"status": 200,
"body": {"ok": true},
"delay": 500
}* delay(500)
* def res = call read('classpath:mock/endpoint.feature')
configure delay in a shared Background or BeforeAll block – it is JVM‑wide for the mock server instance.Background:
* def port = karate.get('mock.port')
* karate.startMockServer(port)
After hook to avoid port leaks.responseTime in milliseconds.
* assert responseTime >= 500 - 50
responseTime in parallel runs to confirm consistency.The most common cause is concurrent modification of the global configure delay value. Because the mock server instance is shared across threads, one scenario can overwrite the delay used by another, leading to unpredictable timings.
delay(ms) and delay field inject latency server‑side.configure delay is JVM‑wide and not thread‑safe.responseTime can be asserted to validate the delay.configure delay from shared hooks.* delay(500) before each mock call or set "delay": 500 in the mock JSON.karate.startMockServer(8081 + workerId)).responseTime >= 450 after each call.responseTime across parallel workers to confirm stability.To fine‑tune the recommendation, could you share the Karate version you’re using? Different releases have subtle changes in mock server threading.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.