test_ModuleHTTP.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include <gtest/gtest.h>
  2. #include <json.hpp>
  3. #include <httplib.h>
  4. //#define private public
  5. #include <ProcessorStandard.h>
  6. #include <ModuleSwitch.h>
  7. #include <Generator.h>
  8. #include <math.h>
  9. #include <thread>
  10. using namespace mdd;
  11. using namespace httplib;
  12. /*
  13. void serverThread()
  14. {
  15. Server svr;
  16. json input;
  17. json output;
  18. std::string status = "ready";
  19. for (int i = 0; i < 5; ++i) {
  20. json entity;
  21. entity["type"] = "INPUT";
  22. entity["value"] = { i };
  23. input.push_back(entity);
  24. }
  25. for (int i = 0; i < 3; ++i) {
  26. json entity;
  27. entity["type"] = "OUTPUT";
  28. entity["value"] = { i * 2 };
  29. output.push_back(entity);
  30. }
  31. svr.Get("/inputs", [&](const Request& req, Response& res) {
  32. res.set_content(input.dump(), "application/json");
  33. });
  34. svr.Get("/outputs", [&](const Request& req, Response& res) {
  35. res.set_content(output.dump(), "application/json");
  36. });
  37. svr.Get("/status", [&](const Request& req, Response& res) {
  38. try{
  39. json body;
  40. body["status"] = status;
  41. std::cout << status << std::endl;
  42. res.set_content(body.dump(), "application/json");
  43. }
  44. catch(std::exception){
  45. std::cout << "Exeption caught"<<std::endl;
  46. }
  47. });
  48. svr.Post("/update",
  49. [&](const Request& req, Response& res, const ContentReader &content_reader) {
  50. status = "updating...";
  51. std::string body;
  52. content_reader([&](const char *data, size_t data_length) {
  53. body.append(data, data_length);
  54. return true;
  55. });
  56. body = std::string(R"()") + body;
  57. json inputs = json::parse(body.c_str());
  58. for (int i = 0; i < inputs.size(); ++i) {
  59. input[i]["value"] = inputs[i]["value"];
  60. }
  61. output[0]["value"][0] = input[0]["value"][0].get<int>() + input[1]["value"][0].get<int>();
  62. output[1]["value"][0] = input[2]["value"][0].get<int>() * input[2]["value"][0].get<int>();
  63. output[2]["value"][0] = input[3]["value"][0].get<int>() - input[4]["value"][0].get<int>();
  64. //std::cout << output[0].dump() << std::endl;
  65. //std::cout << output[1].dump() << std::endl;
  66. //std::cout << output[2].dump() << std::endl;
  67. res.set_content(output.dump(), "application/json");
  68. status = "ready";
  69. }
  70. );
  71. svr.Get("/stop", [&](const Request& req, Response& res) {
  72. std::cout << "server stoped"<<std::endl;
  73. svr.stop();
  74. });
  75. svr.listen("localhost", 8888);
  76. std::cout << "server closed"<<std::endl;
  77. }
  78. TEST(ModuleHTTP, updateLayout_intern){
  79. std::thread server (serverThread);
  80. auto module = GetGenerators()["mdd::ModuleHTTP"]->Generate();// ("", "localhost", 8888);
  81. Client cli("localhost",8888);
  82. cli.Get("/stop");
  83. server.join();
  84. auto inputs_types = module.getInputs();
  85. auto inputs_ids = module.getInputIDs();
  86. for (int i = 0; i < 5; ++i) {
  87. EXPECT_EQ(inputs_types[i], "INPUT");
  88. EXPECT_EQ((int)module.getInput(inputs_ids[i])->getValue()[0], i);
  89. }
  90. auto outputs_types = module.getOutputs();
  91. auto outputs_ids = module.getOutputIDs();
  92. for (int i = 0; i < 3; ++i) {
  93. EXPECT_EQ(outputs_types[i], "OUTPUT");
  94. EXPECT_EQ((int)module.getOutput(outputs_ids[i])->getValue()[0], i * 2);
  95. }
  96. }
  97. TEST(ModuleHTTP, updateLayout_extern){
  98. ModuleHTTP module("../../../lib/test/server/server.py","localhost",8889);
  99. auto inputs_types = module.getInputs();
  100. auto inputs_ids = module.getInputIDs();
  101. for(int i = 0; i < 5; ++i){
  102. EXPECT_EQ(inputs_types[i], "INPUT");
  103. EXPECT_EQ((int)module.getInput(inputs_ids[i])->getValue()[0], i);
  104. }
  105. auto outputs_types = module.getOutputs();
  106. auto outputs_ids = module.getOutputIDs();
  107. for(int i = 0; i < 3; ++i){
  108. EXPECT_EQ(outputs_types[i], "OUTPUT");
  109. EXPECT_EQ((int)module.getOutput(outputs_ids[i])->getValue()[0], i*2);
  110. }
  111. Client cli("localhost",8889);
  112. cli.Get("/stop");
  113. }
  114. TEST(ModuleHTTP, update_intern){
  115. std::thread server (serverThread);
  116. ModuleHTTP module("", "localhost", 8888);
  117. //std::cout << "WORKED" << std::endl;
  118. auto inputs_types = module.getInputs();
  119. auto inputs_ids = module.getInputIDs();
  120. for (int i = 0; i < 5; ++i) {
  121. module.getInput(inputs_ids[i])->setValue() = { (double)(10 - i) };
  122. }
  123. module.update();
  124. Client cli("localhost", 8888);
  125. cli.Get("/stop");
  126. server.join();
  127. inputs_types = module.getInputs();
  128. inputs_ids = module.getInputIDs();
  129. for (int i = 0; i < 5; ++i) {
  130. EXPECT_EQ(inputs_types[i], "INPUT");
  131. EXPECT_EQ(module.getInput(inputs_ids[i])->getValue()[0], 10-i);
  132. }
  133. //std::cout << "WORKED" << std::endl;
  134. auto outputs_types = module.getOutputs();
  135. auto outputs_ids = module.getOutputIDs();
  136. //std::cout << module.getOutput(outputs_ids[0])->getValue() << std::endl;
  137. EXPECT_EQ((int)module.getOutput(outputs_ids[1])->getValue()[0], 64);
  138. EXPECT_EQ((int)module.getOutput(outputs_ids[2])->getValue()[0], 1);
  139. EXPECT_EQ((int)module.getOutput(outputs_ids[0])->getValue()[0], 19);
  140. }
  141. //*/