From 8690dd31ca05492de4ce960ece6dae4179a51446 Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Thu, 28 Jan 2021 00:28:10 +0100 Subject: [PATCH] Add working TopicTree unit test --- tests/Makefile | 6 ++-- tests/TopicTree.cpp | 74 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 69 insertions(+), 11 deletions(-) diff --git a/tests/Makefile b/tests/Makefile index c6ff50c..c846b03 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,9 +1,9 @@ default: - #$(CXX) -std=c++17 -fsanitize=address TopicTree.cpp -o TopicTree - #./TopicTree + $(CXX) -std=c++17 -fsanitize=address TopicTree.cpp -o TopicTree + ./TopicTree $(CXX) -std=c++17 -fsanitize=address HttpRouter.cpp -o HttpRouter ./HttpRouter $(CXX) -std=c++17 -fsanitize=address BloomFilter.cpp -o BloomFilter ./BloomFilter $(CXX) -std=c++17 -fsanitize=address ExtensionsNegotiator.cpp -o ExtensionsNegotiator - ./ExtensionsNegotiator \ No newline at end of file + ./ExtensionsNegotiator diff --git a/tests/TopicTree.cpp b/tests/TopicTree.cpp index 1feb436..db974d4 100644 --- a/tests/TopicTree.cpp +++ b/tests/TopicTree.cpp @@ -7,14 +7,16 @@ void testUnsubscribeInside() { std::cout << "TestUnsubscribeInside" << std::endl; uWS::TopicTree *topicTree; - std::map expectedResult; + std::map> expectedResult; + + topicTree = new uWS::TopicTree([&topicTree, &expectedResult](uWS::Subscriber *s, std::pair dataChannels) { + std::string_view data = dataChannels.second; - topicTree = new uWS::TopicTree([&topicTree, &expectedResult](uWS::Subscriber *s, std::string_view data) { /* Check for unexpected subscribers */ assert(expectedResult.find(s) != expectedResult.end()); /* Check for unexpected data */ - assert(expectedResult[s] == data); + assert(expectedResult[s].first == dataChannels.first && expectedResult[s].second == dataChannels.second); /* This one causes mess-up */ topicTree->unsubscribeAll(s); @@ -28,8 +30,8 @@ void testUnsubscribeInside() { /* Fill out expectedResult */ expectedResult = { - {s1, "Ett!"}, - {s2, "Två!"} + {s1, {"Ett!", "Ett!"}}, + {s2, {"Två!", "Två!"}} }; /* Make sure s1 < s2 */ @@ -44,8 +46,62 @@ void testUnsubscribeInside() { topicTree->subscribe("1", s1); /* This order matters, as it fills triggeredTopics array in order */ - topicTree->publish("1", "Ett!"); - topicTree->publish("2", "Två!"); + topicTree->publish("1", {std::string_view("Ett!"), std::string_view("Ett!")}); + topicTree->publish("2", {std::string_view("Två!"), std::string_view("Ett!")}); + + topicTree->drain(); + + /* Release resources */ + topicTree->unsubscribeAll(s1); + topicTree->unsubscribeAll(s2); + + delete s1; + delete s2; + + delete topicTree; +} + +void testPublisherHoles() { + std::cout << "TestPublisherHoles" << std::endl; + + uWS::TopicTree *topicTree; + std::map> expectedResult; + + topicTree = new uWS::TopicTree([&topicTree, &expectedResult](uWS::Subscriber *s, std::pair dataChannels) { + + /* Check for unexpected subscribers */ + assert(expectedResult.find(s) != expectedResult.end()); + + /* Check for unexpected data */ + assert(expectedResult[s].first == dataChannels.first && expectedResult[s].second == dataChannels.second); + + /* We actually don't use this one */ + return 0; + }); + + uWS::Subscriber *s1 = new uWS::Subscriber(nullptr); + uWS::Subscriber *s2 = new uWS::Subscriber(nullptr); + + /* Fill out expectedResult */ + expectedResult = { + {s1, {"Två!", "Två!"}}, // todo: this one should not receive what he sent himself + {s2, {"Två!", "Två!"}} + }; + + /* Make sure s1 < s2 */ + if (s2 < s1) { + uWS::Subscriber *tmp = s1; + s1 = s2; + s2 = tmp; + } + + /* This order does not matter as it fills a tree */ + topicTree->subscribe("1", s2); + topicTree->subscribe("1", s1); + + /* This order matters, as it fills triggeredTopics array in order */ + //topicTree->publish("1", {std::string_view("Ett!"), std::string_view("Ett!")}); + topicTree->publish("1", {std::string_view("Två!"), std::string_view("Två!")}); topicTree->drain(); @@ -60,5 +116,7 @@ void testUnsubscribeInside() { } int main() { - testUnsubscribeInside(); + //testUnsubscribeInside(); + + testPublisherHoles(); } \ No newline at end of file