local cfg = dstest.config({
substrate = "docker",
seed = 0xD157,
accumulation = "single",
})
local server = dstest.setup(cfg, {
image = "kennethreitz/httpbin",
ports = { 80 },
})
local client = dstest.setup(cfg, {
image = "curlimages/curl:latest",
cmd = { "sleep", "300" },
depends = { server },
})
local link = dstest.net.link(client, server, 80)
local proxy_url = string.format("http://%s/get", tostring(link:addr()))
for _ = 1, 20 do
local r = dstest.exec(client, {
"curl", "-s", "-o", "/dev/null", "-w", "%{http_code}",
"--connect-timeout", "2", "--max-time", "5", proxy_url,
})
if tonumber(r.stdout) == 200 then break end
dstest.exec(client, { "sleep", "1" })
end
local function probe(label)
local start = dstest.clock()
local r = dstest.exec(client, {
"curl", "-s", "-o", "/dev/null", "-w", "%{http_code}",
"--connect-timeout", "2", "--max-time", "5", proxy_url,
})
local ms = (dstest.clock().nanos - start.nanos) / 1e6
local code = tonumber(r.stdout)
local ok = r.exit_code == 0 and code == 200
dstest.info(string.format(" %-28s ok=%-5s code=%-4s %.0fms", label, tostring(ok), tostring(code), ms))
return ok
end
local function scenarios()
return coroutine.wrap(function()
coroutine.yield("baseline", function() link:heal() end)
coroutine.yield("latency 150+jitter 50", function() link:latency(150, 50) end)
coroutine.yield("loss 25%", function() link:loss(0.25) end)
coroutine.yield("partition a->b blackhole", function()
link:partition({ direction = "a->b", mode = "blackhole" })
end)
coroutine.yield("partition both reset", function()
link:partition({ direction = "both", mode = "reset" })
end)
coroutine.yield("heal", function() link:heal() end)
end)
end
local results = {}
for label, apply in scenarios() do
dstest.info("scenario: " .. label)
apply()
dstest.exec(client, { "sleep", "1" }) results[#results + 1] = { label = label, ok = probe(label) }
end
assert(results[1].ok, "baseline must be reachable")
assert(results[#results].ok, "post-heal must be reachable")
dstest.dst.clear(client)
dstest.dst.clear(server)
dstest.info("partition example complete")