drogon/examples/simple_example_test/HttpPipeliningTest.cc

105 lines
3.1 KiB
C++
Raw Normal View History

2019-04-07 04:15:02 +00:00
#include <drogon/HttpClient.h>
#include <drogon/HttpAppFramework.h>
#include <string>
#include <iostream>
#include <atomic>
using namespace drogon;
int main()
{
trantor::Logger::setLogLevel(trantor::Logger::TRACE);
auto client = HttpClient::newHttpClient("127.0.0.1", 8848);
client->setPipeliningDepth(64);
int counter = -1;
int n = 0;
auto request1 = HttpRequest::newHttpRequest();
request1->setPath("/pipe");
request1->setMethod(Head);
client->sendRequest(
request1, [&counter, &n](ReqResult r, const HttpResponsePtr &resp) {
if (r == ReqResult::Ok)
{
auto counterHeader = resp->getHeader("counter");
int c = atoi(counterHeader.data());
if (c <= counter)
{
LOG_ERROR << "The response was received in "
"the wrong order!";
exit(-1);
}
else
{
counter = c;
n++;
}
if (resp->getBody().length() > 0)
{
LOG_ERROR << "The response has a body:" << resp->getBody();
exit(-1);
}
}
else
{
exit(-1);
}
});
auto request2 = HttpRequest::newHttpRequest();
request2->setPath("/drogon.jpg");
client->sendRequest(request2, [](ReqResult r, const HttpResponsePtr &resp) {
if (r == ReqResult::Ok)
{
if (resp->getBody().length() != 44618)
{
LOG_ERROR << "The response is error!";
exit(-1);
}
}
else
{
exit(-1);
}
});
auto request = HttpRequest::newHttpRequest();
request->setPath("/pipe");
for (int i = 0; i < 19; i++)
2019-04-07 04:15:02 +00:00
{
2019-05-18 12:39:57 +00:00
client->sendRequest(
request, [&counter, &n](ReqResult r, const HttpResponsePtr &resp) {
if (r == ReqResult::Ok)
2019-04-07 04:15:02 +00:00
{
2019-05-18 12:39:57 +00:00
auto counterHeader = resp->getHeader("counter");
int c = atoi(counterHeader.data());
if (c <= counter)
{
LOG_ERROR
<< "The response was received in the wrong order!";
exit(-1);
}
else
{
counter = c;
n++;
if (n == 20)
{
LOG_DEBUG << "Good!";
app().getLoop()->quit();
}
}
if (resp->getBody().length() == 0)
{
LOG_ERROR << "The response hasn't a body!";
exit(-1);
}
2019-04-07 04:15:02 +00:00
}
else
{
2019-05-18 12:39:57 +00:00
exit(-1);
2019-04-07 04:15:02 +00:00
}
2019-05-18 12:39:57 +00:00
});
2019-04-07 04:15:02 +00:00
}
app().run();
}