From 730cf29479618e6b2cd8f92a93478d9761496255 Mon Sep 17 00:00:00 2001 From: heck Date: Thu, 8 Jul 2021 18:44:13 +0200 Subject: [PATCH] Test: PityTest - update tests --- test/test_execmodes.cc | 45 +++++++++++++++++++++------------------- test/test_pitytree.cc | 4 ++-- test/test_processdirs.cc | 41 ++++++++++++++++++------------------ test/test_swarm.cc | 29 ++++++++++++++++++++------ 4 files changed, 70 insertions(+), 49 deletions(-) diff --git a/test/test_execmodes.cc b/test/test_execmodes.cc index da7af9c..f6daa60 100644 --- a/test/test_execmodes.cc +++ b/test/test_execmodes.cc @@ -36,28 +36,31 @@ int main(int argc, char* argv[]) // NEW API { - // Subprocess 1 - TestUnit grp1 = TestUnit{ "grp1", - do_some_work, - &ctxe, - TestUnit::ExecutionMode::PROCESS_PARALLEL }; - grp1.addCopy(TestUnit("test1.1", do_some_work)); - grp1.addCopy(TestUnit("test1.2", do_some_work)); - - // Subprocess 2 - TestUnit grp2 = TestUnit{ "grp2", - do_some_work, - &ctxe, - TestUnit::ExecutionMode::PROCESS_PARALLEL }; - grp2.addCopy(TestUnit("unit_2.1", do_some_work)); - grp2.addCopy(TestUnit("unit_2.2", do_some_work)); - // Suite - TestUnit root = TestUnit{ "Test Execution Model" }; - root.addRef(grp1); - root.addRef(grp2); - - root.run(); + TestUnit suite = TestUnit("Test Execution Model"); + + // Groups + auto grp1 = suite.addNew( + "grp1", + do_some_work, + &ctxe, + TestUnit::ExecutionMode::PROCESS_PARALLEL); + + auto grp2 = suite.addNew( + "grp2", + do_some_work, + &ctxe, + TestUnit::ExecutionMode::PROCESS_PARALLEL); + + // Units + grp1.addNew("test1.1", do_some_work); + grp1.addNew("test1.2", do_some_work); + + // Units + grp2.addNew("unit_2.1", do_some_work); + grp2.addNew("unit_2.2", do_some_work); + + suite.run(); } // Old API { diff --git a/test/test_pitytree.cc b/test/test_pitytree.cc index 4dd496b..601ee2f 100644 --- a/test/test_pitytree.cc +++ b/test/test_pitytree.cc @@ -130,10 +130,10 @@ int main(int argc, char *argv[]) PITYASSERT(b.getChildRefs().size() == 0, "8"); // Create a copy of the node in the parent node - b.addCopy(ANode("c")).addCopy(ANode("d")); + b.addNew("c").addNew("d"); std::cout << a.to_string() << std::endl; - b.addCopy(BNode("c1")).addCopy(BNode("e")); + b.addNew("c1").addNew("e"); std::cout << a.to_string() << std::endl; b.getChild("c1").getChild("e").addCopy(ANode(a), "a1"); diff --git a/test/test_processdirs.cc b/test/test_processdirs.cc index be5d531..58aec8b 100644 --- a/test/test_processdirs.cc +++ b/test/test_processdirs.cc @@ -21,77 +21,78 @@ void printHomeDir(TestUnit& myself) int main(int argc, char* argv[]) { PityUnit<>::debug_log_enabled = false; + // Suite TestUnit suite = TestUnit{ "test_processdirs" }; // 1 suite - .addCopy(TestUnit( + .addNew( "node 1", [](TestUnit& pity, TestContext* ctx) { PITYASSERT(pity.processDir() == "./pitytest_data/test_processdirs/", "node 1"); return 0; - })) - .addCopy(TestUnit("node 1.1", [](TestUnit& pity, TestContext* ctx) { + }) + .addNew("node 1.1", [](TestUnit& pity, TestContext* ctx) { PITYASSERT(pity.processDir() == "./pitytest_data/test_processdirs/", "node 1.1"); return 0; - })); + }); // 2 suite - .addCopy(TestUnit( + .addNew( "node 2", [](TestUnit& pity, TestContext* ctx) { PITYASSERT(pity.processDir() == "./pitytest_data/test_processdirs/", "node 2"); return 0; - })) - .addCopy(TestUnit( + }) + .addNew( "node 2.1", [](TestUnit& pity, TestContext* ctx) { PITYASSERT(pity.processDir() == "./pitytest_data/test_processdirs/node_2_1/", ""); return 0; }, nullptr, - PityUnit<>::ExecutionMode::PROCESS_PARALLEL)) - .addCopy(TestUnit("node 2.1.1", [](TestUnit& pity, TestContext* ctx) { + PityUnit<>::ExecutionMode::PROCESS_PARALLEL) + .addNew("node 2.1.1", [](TestUnit& pity, TestContext* ctx) { PITYASSERT(pity.processDir() == "./pitytest_data/test_processdirs/node_2_1/", ""); return 0; - })); + }); // 3 suite - .addCopy(TestUnit( + .addNew( "node 3", [](TestUnit& pity, TestContext* ctx) { PITYASSERT(pity.processDir() == "./pitytest_data/test_processdirs/node_3/", ""); return 0; }, nullptr, - PityUnit<>::ExecutionMode::PROCESS_PARALLEL)) - .addCopy(TestUnit( + PityUnit<>::ExecutionMode::PROCESS_PARALLEL) + .addNew( "node 3.1", [](TestUnit& pity, TestContext* ctx) { PITYASSERT(pity.processDir() == "./pitytest_data/test_processdirs/node_3/", ""); return 0; - })) - .addCopy(TestUnit( + }) + .addNew( "node 3.1.1", [](TestUnit& pity, TestContext* ctx) { PITYASSERT(pity.processDir() == "./pitytest_data/test_processdirs/node_3/", ""); return 0; - })) - .addCopy(TestUnit( + }) + .addNew( "node 3.1.1", [](TestUnit& pity, TestContext* ctx) { PITYASSERT(pity.processDir() == "./pitytest_data/test_processdirs/node_3_1_1/", ""); return 0; }, nullptr, - PityUnit<>::ExecutionMode::PROCESS_PARALLEL)) - .addCopy(TestUnit("node 3.1.1.1", [](TestUnit& pity, TestContext* ctx) { + PityUnit<>::ExecutionMode::PROCESS_PARALLEL) + .addNew("node 3.1.1.1", [](TestUnit& pity, TestContext* ctx) { PITYASSERT(pity.processDir() == "./pitytest_data/test_processdirs/node_3_1_1/", ""); return 0; - })); + }); suite.run(); } \ No newline at end of file diff --git a/test/test_swarm.cc b/test/test_swarm.cc index 61102ad..fae4f50 100644 --- a/test/test_swarm.cc +++ b/test/test_swarm.cc @@ -4,16 +4,24 @@ using namespace pEp; using namespace pEp::Adapter; using namespace pEp::PityTest11; -int test_node1(PityUnit& unit, PityPerspective* ctx) +using TextCTX = PityPerspective; +using TestUnit = PityUnit; + +int test_init(PityUnit& unit, PityPerspective* ctx) { unit.log("ModelName:" + ctx->model.getName()); unit.log("perspective name:" + ctx->own_name); unit.log("perspective partner:" + ctx->cpt_name); unit.log("HOME: " + std::string(getenv("HOME"))); + return 0; +} +int test_run(PityUnit& unit, PityPerspective* ctx) +{ std::string msg = "Message from: " + unit.getPathShort(); - int throttle = 1000; - while (true) { + int throttle = 10; + int cycles = 10; + for (int i = 0; i < cycles; i++) { Utils::sleep_millis(throttle); for (const auto& peer : ctx->peers) { unit.transport()->sendMsg(peer, msg); @@ -26,14 +34,23 @@ int test_node1(PityUnit& unit, PityPerspective* ctx) return 0; } +int test_finish(PityUnit& unit, PityPerspective* ctx) +{ + unit.log("DONE"); + return 0; +} + int main(int argc, char* argv[]) { int nodesCount = 3; - PityModel model{ "test_swarm", nodesCount }; - PitySwarm swarm{ model }; + PityModel model{ "model_swarm", nodesCount }; + PitySwarm swarm{ "swarm1", model }; + std::cout << swarm.getSwarmUnit().to_string() << std::endl; for (int i = 0; i < nodesCount; i++) { - swarm.addTestUnit(i, "test1", &test_node1); + swarm.addTestUnit(i, TestUnit("test1", &test_init)); + swarm.addTestUnit(i, TestUnit("test1", &test_run)); + swarm.addTestUnit(i, TestUnit("test1", &test_finish)); } swarm.run();