Commit 88a45474 by Amir Aharon

add more tests

parent 89ee2b31
Showing with 73 additions and 8 deletions
......@@ -167,7 +167,7 @@ void pubsubtest(cMicroservice_Client *p_Client) {
// for convenience
using json = nlohmann::json;
static const int ITERATIONS = 2;
static const int ITERATIONS = 10;
static const char *const JSON_CONTENT = "{\n"
......@@ -513,37 +513,102 @@ void runEvppRequestsTest(){
t.Stop(true);
}
void printObjectDoc(rapidjson::Document &t_ObjectDoc){
std::ostringstream c_OutputStream;
if(!t_ObjectDoc.IsNull()) {
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
t_ObjectDoc.Accept(writer);
c_OutputStream << nsMicroservice_Constants::SUCCESS_REST_RESPONSE_TEMPLATE << buffer.GetString() << '}';
} else {
c_OutputStream << nsMicroservice_Constants::SUCCESS_NULL_REST_RESPONSE_TEMPLATE << '}';
}
std::cout << c_OutputStream.str().c_str() << std::endl;
}
void runMSClientEvppTest(){
std::string body = "This is expected to be sent back as part of response body.";
auto p_msClient = ClientFactory::createEvppCommandImpl("evpp-test");
std::atomic_int testPassedNumber(0);
std::map<std::string,std::string> headers;
headers["Content-Type"] = "application/json";
//std::string uri = "http://echo.jpillora.com";
std::string uri = "https://postman-echo.com";
MSCommandParams msCmndParams;
msCmndParams.WithEntity(uri)
.WithParamsString("post")
//.WithRequestParams("q=base")
.WithRequestParams("q=base")
.WithHeadersMap(&headers)
.WithContent(body);
p_msClient->AsyncCreate(&msCmndParams,[&testPassedNumber](cMicroservice_BaseRestResponse* p_baseRestResponse){
auto f = [&testPassedNumber](cMicroservice_BaseRestResponse* p_baseRestResponse){
if(p_baseRestResponse->IsSuccess()){
std::cout << __PRETTY_FUNCTION__ << ": Success" << std::endl;
std::cout << __PRETTY_FUNCTION__ << ": Success, Object =>" << std::endl;
printObjectDoc(p_baseRestResponse->GetObjectNode());
testPassedNumber++;
} else {
std::cout << __PRETTY_FUNCTION__ << ": Error" << std::endl;
}
testPassedNumber++;
});
};
for (int i = 0; i < ITERATIONS; i++)
{
msCmndParams.WithParamsString("post");
p_msClient->AsyncCreate(&msCmndParams, f);
msCmndParams.WithParamsString("get");
p_msClient->AsyncRead(&msCmndParams, f);
msCmndParams.WithParamsString("put");
p_msClient->AsyncUpdate(&msCmndParams, f);
msCmndParams.WithParamsString("delete");
p_msClient->AsyncDelete(&msCmndParams, f);
}
while (testPassedNumber < 4 * ITERATIONS)
{
usleep(1);
}
while (testPassedNumber < 1) {
}
static int responsed = 0;
static void HandleHTTPResponse(const std::shared_ptr<evpp::httpc::EvppResponse>& response, evpp::httpc::PostEvppRequest* request) {
LOG_INFO << "http_code=" << response->http_code()
<< " URL=http://" << request->host() << request->uri()
<< " BODY = [" << response->body().ToString() << "]";
const char* header = response->FindHeader("Connection");
if (header) {
LOG_INFO << "HTTP HEADER Connection=" << header;
}
responsed++;
assert(request == response->request());
delete request; // The request MUST BE deleted in EventLoop thread.
}
void test_evpp(){
evpp::EventLoopThread t;
string url = "https://postman-echo.com/post";
t.Start(true);
evpp::httpc::PostEvppRequest* r = new evpp::httpc::PostEvppRequest(t.loop(), url, evpp::Duration(3.0));
r->Execute(std::bind(&HandleHTTPResponse, std::placeholders::_1, r));
r = new evpp::httpc::PostEvppRequest(t.loop(), url, evpp::Duration(3.0));
r->Execute(std::bind(&HandleHTTPResponse, std::placeholders::_1, r));
r = new evpp::httpc::PostEvppRequest(t.loop(), url, evpp::Duration(3.0));
r->Execute(std::bind(&HandleHTTPResponse, std::placeholders::_1, r));
r = new evpp::httpc::PostEvppRequest(t.loop(), url, evpp::Duration(3.0));
r->Execute(std::bind(&HandleHTTPResponse, std::placeholders::_1, r));
while (responsed != 4) {
usleep(1);
}
t.Stop(true);
LOG_INFO << "EventLoopThread stopped.";
}
int main(int argc, char *argv[])
{
//auto duration = CommonUtils::measureFunc<>(runEvppRequestsTest);
auto duration = CommonUtils::measureFunc<>(runMSClientEvppTest);
// auto duration = CommonUtils::measureFunc<>(test_evpp);
std::cout <<" Testing " << ITERATIONS << " with map serialization json took: " << duration << "msec" << '\n';
//runRestZmqTest();
......
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 sign in to comment