Files
moloapp/tests/example/data/singleflight_test.cpp
T

23 lines
1.1 KiB
C++
Raw Normal View History

// Route: /data/singleflight (concurrent loads collapse into one)
#include "support/Assert.hpp"
using namespace molo::example::test;
using molo::example::fw::Method;
static const char* kForm = "application/x-www-form-urlencoded";
int main() {
Fixture f;
check(f.client->get("/data/singleflight").status == 200, "status 200");
auto res = f.client->post("/data/singleflight/run", "callers=8", kForm);
check(contains(res.body, "8 callers, 1 load(s)"), "eight callers share one load");
auto one = f.client->post("/data/singleflight/run", "callers=1", kForm);
check(contains(one.body, "1 callers, 1 load(s)"), "single caller loads once");
auto max = f.client->post("/data/singleflight/run", "callers=16", kForm);
check(contains(max.body, "16 callers, 1 load(s)"), "cap of 16 threads still collapses");
auto bad = f.client->post("/data/singleflight/run", "callers=17", kForm);
check(bad.status == 422 && contains(bad.body, "must be between 1 and 16"), "422 above thread cap");
check(contains(f.client->get("/data/singleflight").body, ">3<"), "side effect: three runs counted");
return finish("data/singleflight");
}