test_ModuleHTTP.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include <gtest/gtest.h>
  2. #include <json.hpp>
  3. #include <httplib.h>
  4. //#define private public
  5. #include <ProcessorManual.h>
  6. #include <ModuleHTTP.h>
  7. #include <ModuleMath.h>
  8. #include <ModuleSwitch.h>
  9. #include <math.h>
  10. #include <thread>
  11. using namespace mdd;
  12. using namespace httplib;
  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. json body;
  39. body["status"] = status;
  40. std::cout << status << std::endl;
  41. res.set_content(body.dump(), "application/json");
  42. });
  43. svr.Post("/update",
  44. [&](const Request& req, Response& res, const ContentReader &content_reader) {
  45. status = "updating...";
  46. std::string body;
  47. content_reader([&](const char *data, size_t data_length) {
  48. body.append(data, data_length);
  49. return true;
  50. });
  51. body = std::string(R"()") + body;
  52. json inputs = json::parse(body.c_str());
  53. for (int i = 0; i < inputs.size(); ++i) {
  54. input[i]["value"] = inputs[i]["value"];
  55. }
  56. output[0]["value"] = input[0]["value"].get<int>() + input[1]["value"].get<int>();
  57. output[1]["value"] = input[2]["value"].get<int>() * input[2]["value"].get<int>();
  58. output[2]["value"] = input[3]["value"].get<int>() - input[4]["value"].get<int>();
  59. std::cout << output[0].dump() << std::endl;
  60. std::cout << output[1].dump() << std::endl;
  61. std::cout << output[2].dump() << std::endl;
  62. res.set_content(output.dump(), "application/json");
  63. status = "ready";
  64. }
  65. );
  66. svr.Get("/stop", [&](const Request& req, Response& res) {
  67. svr.stop();
  68. });
  69. svr.listen("localhost", 8888);
  70. }
  71. TEST(ModuleHTTP, updateLayout_intern){
  72. std::thread server (serverThread);
  73. ModuleHTTP module("", "localhost", 8888);
  74. Client cli("localhost",8888);
  75. cli.Get("/stop");
  76. server.join();
  77. auto inputs_types = module.getInputs();
  78. auto inputs_ids = module.getInputIDs();
  79. for (int i = 0; i < 5; ++i) {
  80. EXPECT_EQ(inputs_types[i], "INPUT");
  81. EXPECT_EQ(module.getInput(inputs_ids[i])->getValue()["value"].get<int>(), i);
  82. }
  83. auto outputs_types = module.getOutputs();
  84. auto outputs_ids = module.getOutputIDs();
  85. for (int i = 0; i < 3; ++i) {
  86. EXPECT_EQ(outputs_types[i], "OUTPUT");
  87. EXPECT_EQ(module.getOutput(outputs_ids[i])->getValue()["value"].get<int>(), i * 2);
  88. }
  89. }
  90. TEST(ModuleHTTP, updateLayout_extern){
  91. ModuleHTTP module("../../../lib/test/server/server.py","localhost",8888);
  92. auto inputs_types = module.getInputs();
  93. auto inputs_ids = module.getInputIDs();
  94. for(int i = 0; i < 5; ++i){
  95. EXPECT_EQ(inputs_types[i], "INPUT");
  96. EXPECT_EQ(module.getInput(inputs_ids[i])->getValue()["value"].get<int>(), i);
  97. }
  98. auto outputs_types = module.getOutputs();
  99. auto outputs_ids = module.getOutputIDs();
  100. for(int i = 0; i < 3; ++i){
  101. EXPECT_EQ(outputs_types[i], "OUTPUT");
  102. EXPECT_EQ(module.getOutput(outputs_ids[i])->getValue()["value"].get<int>(), i*2);
  103. }
  104. Client cli("localhost",8888);
  105. cli.Get("/stop");
  106. }
  107. TEST(ModuleHTTP, update_intern){
  108. std::thread server (serverThread);
  109. usleep(500000);
  110. ModuleHTTP module("", "localhost", 8888);
  111. auto inputs_types = module.getInputs();
  112. auto inputs_ids = module.getInputIDs();
  113. for (int i = 0; i < 5; ++i) {
  114. module.getInput(inputs_ids[i])->setDefaultValue()["value"] = 10-i;
  115. }
  116. module.update();
  117. Client cli("localhost",8888);
  118. cli.Get("/stop");
  119. server.join();
  120. inputs_types = module.getInputs();
  121. inputs_ids = module.getInputIDs();
  122. for (int i = 0; i < 5; ++i) {
  123. EXPECT_EQ(inputs_types[i], "INPUT");
  124. EXPECT_EQ(module.getInput(inputs_ids[i])->getValue()["value"].get<int>(), 10-i);
  125. }
  126. auto outputs_types = module.getOutputs();
  127. auto outputs_ids = module.getOutputIDs();
  128. EXPECT_EQ(module.getOutput(outputs_ids[0])->getValue()["value"].get<int>(), 19);
  129. EXPECT_EQ(module.getOutput(outputs_ids[1])->getValue()["value"].get<int>(), 64);
  130. EXPECT_EQ(module.getOutput(outputs_ids[2])->getValue()["value"].get<int>(), 1);
  131. }