Commit 31dca64f authored by Ian Roddis's avatar Ian Roddis

Refining test to capture behaviour that is broken

parent 576db850
......@@ -11,30 +11,36 @@
using namespace std;
using namespace Pistache;
static const size_t N_LETTERS = 26;
static const size_t LETTER_REPEATS = 100000;
static const size_t SET_REPEATS = 10;
static const uint16_t PORT = 9080;
void dumpData(const Rest::Request&req, Http::ResponseWriter response) {
UNUSED(req);
auto stream = response.stream(Http::Code::Ok);
std::streamsize n = 1000;
char letter = 'A';
std::mutex responseGuard;
std::vector<std::thread> workers;
auto sendPayload =
[&responseGuard, &stream, n](char let) -> void {
size_t chunk_size = n / 10;
for (size_t s = 0; s < SET_REPEATS; ++s) {
for (size_t i = 0; i < N_LETTERS; ++i ) {
std::thread job([&,i]() -> void {
const size_t nchunks = 10;
size_t chunk_size = LETTER_REPEATS / nchunks;
std::string payload(chunk_size, letter + i);
{
std::unique_lock<std::mutex> l(responseGuard);
for (size_t chunk = 0; chunk < 10; ++chunk) {
std::string payload(chunk_size * chunk, let);
stream.write(payload.c_str(), n);
std::unique_lock<std::mutex> lock(responseGuard);
for (size_t chunk = 0; chunk < nchunks; ++chunk) {
stream.write(payload.c_str(), chunk_size);
stream.flush();
}
}
};
for (size_t i ; i < 26; ++i) {
workers.emplace_back(std::thread(sendPayload, letter + i));
});
workers.push_back(std::move(job));
}
}
for (auto &w : workers) { w.join(); }
......@@ -43,17 +49,9 @@ void dumpData(const Rest::Request&req, Http::ResponseWriter response) {
TEST(stream, from_description)
{
Http::Client client;
auto client_opts = Http::Client::options()
.threads(3)
.maxConnectionsPerHost(3);
client.init(client_opts);
Address addr(Ipv4::any(), 9080);
Address addr(Ipv4::any(), PORT);
const size_t threads = 20;
auto pid = fork();
if ( pid == 0) {
std::shared_ptr<Http::Endpoint> endpoint;
Rest::Description desc("Rest Description Test", "v1");
Rest::Router router;
......@@ -76,17 +74,13 @@ TEST(stream, from_description)
endpoint = std::make_shared<Pistache::Http::Endpoint>(addr);
endpoint->init(opts);
endpoint->setHandler(router.handler());
endpoint->serve();
endpoint->shutdown();
return;
}
endpoint->serveThreaded();
std::stringstream ss;
// from https://stackoverflow.com/questions/6624667/can-i-use-libcurls-curlopt-writefunction-with-a-c11-lambda-expression#14720398
typedef size_t(*CURL_WRITEFUNCTION_PTR)(void*, size_t, size_t, void*);
auto curl_callback = [](void *ptr, size_t size, size_t nmemb, void *stream) -> size_t {
std::cout.write((char *)ptr, size * nmemb);
auto ss = static_cast<std::stringstream *>(stream);
(*ss).write(static_cast<char *>(ptr), size* nmemb);
return size * nmemb;
......@@ -100,16 +94,10 @@ TEST(stream, from_description)
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, static_cast<CURL_WRITEFUNCTION_PTR>(curl_callback));
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &ss);
res = curl_easy_perform(curl);
if (res != CURLE_OK) {
throw std::runtime_error(curl_easy_strerror(res));
}
curl_easy_cleanup(curl);
}
endpoint->shutdown();
ASSERT_EQ(ss.str().size(), 26000);
kill(pid, SIGTERM);
int r;
waitpid(pid, &r, 0);
client.shutdown();
ASSERT_EQ(res, CURLE_OK);
ASSERT_EQ(ss.str().size(), SET_REPEATS * LETTER_REPEATS * N_LETTERS);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment