Added event callback functionality. EventManager now uses multiple mutexes to lock events, errors, callbacks, and downgradedThreads separately. Wrote single-threaded test for event callbacks.

This commit is contained in:
EricLiu2000
2019-08-02 15:00:31 -04:00
parent f9712a4bcd
commit 4f735a651c
5 changed files with 177 additions and 25 deletions
+70
View File
@@ -1,4 +1,5 @@
#include <thread>
#include <memory>
#include "icsneo/icsneocpp.h"
#include "gtest/gtest.h"
@@ -13,6 +14,75 @@ protected:
}
};
TEST_F(EventManagerTest, SingleThreadCallbacksTest) {
int callCounter = 0;
// increments counter when baudrate events show up
int id1 = EventManager::GetInstance().addEventCallback(EventCallback([&callCounter](std::shared_ptr<APIEvent>){
callCounter++;
}, EventFilter(APIEvent::Type::BaudrateNotFound)));
// increments counter when infos show up
int id2 = EventManager::GetInstance().addEventCallback(EventCallback([&callCounter](std::shared_ptr<APIEvent>) {
callCounter++;
}, EventFilter(APIEvent::Severity::EventInfo)));
EXPECT_EQ(callCounter, 0);
EventManager::GetInstance().add(APIEvent(APIEvent::Type::DeviceCurrentlyClosed, APIEvent::Severity::EventWarning));
EXPECT_EQ(callCounter, 0);
EventManager::GetInstance().add(APIEvent(APIEvent::Type::BaudrateNotFound, APIEvent::Severity::EventWarning));
EXPECT_EQ(callCounter, 1);
EventManager::GetInstance().add(APIEvent(APIEvent::Type::DeviceCurrentlyClosed, APIEvent::Severity::EventInfo));
EXPECT_EQ(callCounter, 2);
EventManager::GetInstance().add(APIEvent(APIEvent::Type::BaudrateNotFound, APIEvent::Severity::EventInfo));
EXPECT_EQ(callCounter, 4);
EXPECT_EQ(EventManager::GetInstance().removeEventCallback(id2), true);
EventManager::GetInstance().add(APIEvent(APIEvent::Type::DeviceCurrentlyClosed, APIEvent::Severity::EventInfo));
EXPECT_EQ(callCounter, 4);
EventManager::GetInstance().add(APIEvent(APIEvent::Type::BaudrateNotFound, APIEvent::Severity::EventInfo));
EXPECT_EQ(callCounter, 5);
// increments counter when device currently open shows up
int id3 = EventManager::GetInstance().addEventCallback(EventCallback([&callCounter](std::shared_ptr<APIEvent>) {
callCounter++;
}, EventFilter(APIEvent::Type::DeviceCurrentlyOpen)));
EventManager::GetInstance().add(APIEvent(APIEvent::Type::DeviceCurrentlyOpen, APIEvent::Severity::EventInfo));
EXPECT_EQ(callCounter, 6);
EXPECT_EQ(EventManager::GetInstance().removeEventCallback(id2), false);
EXPECT_EQ(EventManager::GetInstance().removeEventCallback(id1), true);
EventManager::GetInstance().add(APIEvent(APIEvent::Type::BaudrateNotFound, APIEvent::Severity::EventInfo));
EXPECT_EQ(callCounter, 6);
EventManager::GetInstance().add(APIEvent(APIEvent::Type::DeviceCurrentlyOpen, APIEvent::Severity::EventInfo));
EXPECT_EQ(callCounter, 7);
EXPECT_EQ(EventManager::GetInstance().removeEventCallback(id3), true);
EventManager::GetInstance().add(APIEvent(APIEvent::Type::DeviceCurrentlyOpen, APIEvent::Severity::EventInfo));
EXPECT_EQ(callCounter, 7);
}
TEST_F(EventManagerTest, ErrorDowngradingTest) {
// Check that main thread has no errors
EXPECT_EQ(GetLastError().getType(), APIEvent::Type::NoErrorFound);