123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #include <gtest/gtest.h>
- #include <json.hpp>
- #include <httplib.h>
- //#define private public
- #include <ProcessorStandard.h>
- #include <ModuleSwitch.h>
- #include <Generator.h>
- #include <math.h>
- #include <thread>
- using namespace mdd;
- using namespace httplib;
- /*
- void serverThread()
- {
- Server svr;
- json input;
- json output;
- std::string status = "ready";
- for (int i = 0; i < 5; ++i) {
- json entity;
- entity["type"] = "INPUT";
- entity["value"] = { i };
- input.push_back(entity);
- }
- for (int i = 0; i < 3; ++i) {
- json entity;
- entity["type"] = "OUTPUT";
- entity["value"] = { i * 2 };
- output.push_back(entity);
- }
- svr.Get("/inputs", [&](const Request& req, Response& res) {
- res.set_content(input.dump(), "application/json");
- });
- svr.Get("/outputs", [&](const Request& req, Response& res) {
- res.set_content(output.dump(), "application/json");
- });
- svr.Get("/status", [&](const Request& req, Response& res) {
- try{
- json body;
- body["status"] = status;
- std::cout << status << std::endl;
- res.set_content(body.dump(), "application/json");
- }
- catch(std::exception){
- std::cout << "Exeption caught"<<std::endl;
- }
- });
- svr.Post("/update",
- [&](const Request& req, Response& res, const ContentReader &content_reader) {
- status = "updating...";
- std::string body;
- content_reader([&](const char *data, size_t data_length) {
- body.append(data, data_length);
- return true;
- });
- body = std::string(R"()") + body;
- json inputs = json::parse(body.c_str());
- for (int i = 0; i < inputs.size(); ++i) {
- input[i]["value"] = inputs[i]["value"];
- }
- output[0]["value"][0] = input[0]["value"][0].get<int>() + input[1]["value"][0].get<int>();
- output[1]["value"][0] = input[2]["value"][0].get<int>() * input[2]["value"][0].get<int>();
- output[2]["value"][0] = input[3]["value"][0].get<int>() - input[4]["value"][0].get<int>();
- //std::cout << output[0].dump() << std::endl;
- //std::cout << output[1].dump() << std::endl;
- //std::cout << output[2].dump() << std::endl;
- res.set_content(output.dump(), "application/json");
- status = "ready";
- }
- );
- svr.Get("/stop", [&](const Request& req, Response& res) {
- std::cout << "server stoped"<<std::endl;
- svr.stop();
- });
- svr.listen("localhost", 8888);
- std::cout << "server closed"<<std::endl;
- }
- TEST(ModuleHTTP, updateLayout_intern){
- std::thread server (serverThread);
- auto module = GetGenerators()["mdd::ModuleHTTP"]->Generate();// ("", "localhost", 8888);
- Client cli("localhost",8888);
- cli.Get("/stop");
- server.join();
- auto inputs_types = module.getInputs();
- auto inputs_ids = module.getInputIDs();
- for (int i = 0; i < 5; ++i) {
- EXPECT_EQ(inputs_types[i], "INPUT");
- EXPECT_EQ((int)module.getInput(inputs_ids[i])->getValue()[0], i);
- }
- auto outputs_types = module.getOutputs();
- auto outputs_ids = module.getOutputIDs();
- for (int i = 0; i < 3; ++i) {
- EXPECT_EQ(outputs_types[i], "OUTPUT");
- EXPECT_EQ((int)module.getOutput(outputs_ids[i])->getValue()[0], i * 2);
- }
- }
- TEST(ModuleHTTP, updateLayout_extern){
- ModuleHTTP module("../../../lib/test/server/server.py","localhost",8889);
-
- auto inputs_types = module.getInputs();
- auto inputs_ids = module.getInputIDs();
- for(int i = 0; i < 5; ++i){
- EXPECT_EQ(inputs_types[i], "INPUT");
- EXPECT_EQ((int)module.getInput(inputs_ids[i])->getValue()[0], i);
- }
- auto outputs_types = module.getOutputs();
- auto outputs_ids = module.getOutputIDs();
- for(int i = 0; i < 3; ++i){
- EXPECT_EQ(outputs_types[i], "OUTPUT");
- EXPECT_EQ((int)module.getOutput(outputs_ids[i])->getValue()[0], i*2);
- }
- Client cli("localhost",8889);
- cli.Get("/stop");
- }
- TEST(ModuleHTTP, update_intern){
- std::thread server (serverThread);
- ModuleHTTP module("", "localhost", 8888);
- //std::cout << "WORKED" << std::endl;
- auto inputs_types = module.getInputs();
- auto inputs_ids = module.getInputIDs();
- for (int i = 0; i < 5; ++i) {
- module.getInput(inputs_ids[i])->setValue() = { (double)(10 - i) };
- }
- module.update();
- Client cli("localhost", 8888);
- cli.Get("/stop");
- server.join();
- inputs_types = module.getInputs();
- inputs_ids = module.getInputIDs();
- for (int i = 0; i < 5; ++i) {
- EXPECT_EQ(inputs_types[i], "INPUT");
- EXPECT_EQ(module.getInput(inputs_ids[i])->getValue()[0], 10-i);
- }
- //std::cout << "WORKED" << std::endl;
- auto outputs_types = module.getOutputs();
- auto outputs_ids = module.getOutputIDs();
- //std::cout << module.getOutput(outputs_ids[0])->getValue() << std::endl;
- EXPECT_EQ((int)module.getOutput(outputs_ids[1])->getValue()[0], 64);
- EXPECT_EQ((int)module.getOutput(outputs_ids[2])->getValue()[0], 1);
- EXPECT_EQ((int)module.getOutput(outputs_ids[0])->getValue()[0], 19);
- }
- //*/
|