Wrote more tests and fixed locking bug in eventmanager
parent
975c7f422f
commit
ec95f0f7c3
|
|
@ -51,14 +51,14 @@ public:
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if(newLimit < 10) {
|
if(newLimit < 10) {
|
||||||
add(APIEvent::Type::ParameterOutOfRange, APIEvent::Severity::EventWarning);
|
add(APIEvent::Type::ParameterOutOfRange, APIEvent::Severity::Error);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lk(mutex);
|
std::lock_guard<std::mutex> lk(mutex);
|
||||||
eventLimit = newLimit;
|
eventLimit = newLimit;
|
||||||
if(enforceLimit())
|
if(enforceLimit())
|
||||||
add(APIEvent::Type::TooManyEvents, APIEvent::Severity::EventWarning);
|
add_internal(APIEvent(APIEvent::Type::TooManyEvents, APIEvent::Severity::EventWarning));
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t getEventLimit() const { return eventLimit; }
|
size_t getEventLimit() const { return eventLimit; }
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
#include "icsneo/icsneocpp.h"
|
#include "icsneo/icsneocpp.h"
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
|
||||||
|
|
@ -5,12 +7,37 @@ using namespace icsneo;
|
||||||
|
|
||||||
class EventManagerTest : public ::testing::Test {
|
class EventManagerTest : public ::testing::Test {
|
||||||
protected:
|
protected:
|
||||||
void TearDown() override {
|
void SetUp() override {
|
||||||
EventManager::ResetInstance();
|
EventManager::ResetInstance();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
TEST_F(EventManagerTest, GetLastErrorTest) {
|
// Multithreaded test of GetLastError()
|
||||||
|
TEST_F(EventManagerTest, GetLastErrorMultiThreaded) {
|
||||||
|
std::thread t1( []() {
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::OutputTruncated, APIEvent::Severity::Error));
|
||||||
|
EXPECT_EQ(GetLastError().getType(), APIEvent::Type::OutputTruncated);
|
||||||
|
auto err = GetLastError();
|
||||||
|
EXPECT_EQ(err.getType(), APIEvent::Type::NoErrorFound);
|
||||||
|
EXPECT_EQ(err.getSeverity(), APIEvent::Severity::EventInfo);
|
||||||
|
});
|
||||||
|
|
||||||
|
std::thread t2( []() {
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::OutputTruncated, APIEvent::Severity::Error));
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::ParameterOutOfRange, APIEvent::Severity::Error));
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::SettingsNotAvailable, APIEvent::Severity::Error));
|
||||||
|
EXPECT_EQ(GetLastError().getType(), APIEvent::Type::SettingsNotAvailable);
|
||||||
|
auto err = GetLastError();
|
||||||
|
EXPECT_EQ(err.getType(), APIEvent::Type::NoErrorFound);
|
||||||
|
EXPECT_EQ(err.getSeverity(), APIEvent::Severity::EventInfo);
|
||||||
|
});
|
||||||
|
|
||||||
|
t1.join();
|
||||||
|
t2.join();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that adding 1 error and calling GetLastError() twice will first return the error then return a NoErrorFound info message. Singlethreaded.
|
||||||
|
TEST_F(EventManagerTest, GetLastErrorSingleTest) {
|
||||||
EventManager::GetInstance().add(APIEvent(APIEvent::Type::OutputTruncated, APIEvent::Severity::Error));
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::OutputTruncated, APIEvent::Severity::Error));
|
||||||
EXPECT_EQ(GetLastError().getType(), APIEvent::Type::OutputTruncated);
|
EXPECT_EQ(GetLastError().getType(), APIEvent::Type::OutputTruncated);
|
||||||
auto err = GetLastError();
|
auto err = GetLastError();
|
||||||
|
|
@ -18,6 +45,18 @@ TEST_F(EventManagerTest, GetLastErrorTest) {
|
||||||
EXPECT_EQ(err.getSeverity(), APIEvent::Severity::EventInfo);
|
EXPECT_EQ(err.getSeverity(), APIEvent::Severity::EventInfo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tests that adding multiple errors and calling GetLastError() twice will first return the last error then return a NoErrorFound info message. Singlethreaded.
|
||||||
|
TEST_F(EventManagerTest, GetLastErrorMultipleTest) {
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::OutputTruncated, APIEvent::Severity::Error));
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::ParameterOutOfRange, APIEvent::Severity::Error));
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::SettingsNotAvailable, APIEvent::Severity::Error));
|
||||||
|
EXPECT_EQ(GetLastError().getType(), APIEvent::Type::SettingsNotAvailable);
|
||||||
|
auto err = GetLastError();
|
||||||
|
EXPECT_EQ(err.getType(), APIEvent::Type::NoErrorFound);
|
||||||
|
EXPECT_EQ(err.getSeverity(), APIEvent::Severity::EventInfo);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that adding and removing events properly updates EventCount(). Also tests that EventCount() does not go past the limit.
|
||||||
TEST_F(EventManagerTest, CountTest) {
|
TEST_F(EventManagerTest, CountTest) {
|
||||||
EventManager::GetInstance().add(APIEvent(APIEvent::Type::OutputTruncated, APIEvent::Severity::Error));
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::OutputTruncated, APIEvent::Severity::Error));
|
||||||
EXPECT_EQ(EventCount(), 0);
|
EXPECT_EQ(EventCount(), 0);
|
||||||
|
|
@ -29,4 +68,87 @@ TEST_F(EventManagerTest, CountTest) {
|
||||||
EXPECT_EQ(EventCount(), 1);
|
EXPECT_EQ(EventCount(), 1);
|
||||||
GetEvents();
|
GetEvents();
|
||||||
EXPECT_EQ(EventCount(), 0);
|
EXPECT_EQ(EventCount(), 0);
|
||||||
|
|
||||||
|
SetEventLimit(50);
|
||||||
|
for(int i = 0; i < 60; i++)
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::OutputTruncated, APIEvent::Severity::EventWarning));
|
||||||
|
|
||||||
|
EXPECT_EQ(EventCount(), 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
TEST_F(EventManagerTest, GetTest) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests that setting the event limit works in normal conditions, if the new limit is too small, and if the list needs truncating
|
||||||
|
TEST_F(EventManagerTest, SetEventLimitTest) {
|
||||||
|
// Test if event limit too low to be set
|
||||||
|
EventManager::GetInstance().setEventLimit(9);
|
||||||
|
EXPECT_EQ(GetEventLimit(), 10000);
|
||||||
|
EXPECT_EQ(GetLastError().getType(), APIEvent::Type::ParameterOutOfRange);
|
||||||
|
|
||||||
|
// Test truncating existing list when new limit set
|
||||||
|
for(int i = 0; i < 100; i++)
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::OutputTruncated, APIEvent::Severity::EventWarning));
|
||||||
|
|
||||||
|
EXPECT_EQ(EventCount(), 100);
|
||||||
|
|
||||||
|
SetEventLimit(50);
|
||||||
|
EXPECT_EQ(GetEventLimit(), 50);
|
||||||
|
|
||||||
|
// maybe 5001? since TooManyEvents is in there too.
|
||||||
|
EXPECT_EQ(EventCount(), 50);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests the case where too many warnings are added
|
||||||
|
TEST_F(EventManagerTest, TestAddWarningsOverflow) {
|
||||||
|
|
||||||
|
// space for 49 normal events, 1 reserved for TooManyEvents
|
||||||
|
SetEventLimit(50);
|
||||||
|
|
||||||
|
// 3 of these
|
||||||
|
for(int i = 0; i < 3; i++)
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::OutputTruncated, APIEvent::Severity::EventWarning));
|
||||||
|
|
||||||
|
// 49 of these
|
||||||
|
for(int i = 0; i < 49; i++)
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::ParameterOutOfRange, APIEvent::Severity::EventWarning));
|
||||||
|
|
||||||
|
auto events = GetEvents();
|
||||||
|
for(int i = 0; i < 49; i++)
|
||||||
|
EXPECT_EQ(events.at(i).getType(), APIEvent::Type::ParameterOutOfRange);
|
||||||
|
|
||||||
|
EXPECT_EQ(events.at(49).getType(), APIEvent::Type::TooManyEvents);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Tests the case where too many warnings and info are added
|
||||||
|
TEST_F(EventManagerTest, TestAddWarningsInfoOverflow) {
|
||||||
|
|
||||||
|
// space for 49 normal events, 1 reserved for TooManyEvents
|
||||||
|
SetEventLimit(50);
|
||||||
|
|
||||||
|
// Event list filling: 1 warning, 3 info, 47 warning.
|
||||||
|
// Expect to see: 1 warning, 1 info, 47 warning, 1 TooManyEvents
|
||||||
|
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::SettingsVersionError, APIEvent::Severity::EventWarning));
|
||||||
|
|
||||||
|
// 3 of these
|
||||||
|
for(int i = 0; i < 3; i++)
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::OutputTruncated, APIEvent::Severity::EventInfo));
|
||||||
|
|
||||||
|
// 47 of these
|
||||||
|
for(int i = 0; i < 47; i++)
|
||||||
|
EventManager::GetInstance().add(APIEvent(APIEvent::Type::ParameterOutOfRange, APIEvent::Severity::EventWarning));
|
||||||
|
|
||||||
|
auto events = GetEvents();
|
||||||
|
|
||||||
|
EXPECT_EQ(events.at(0).getType(), APIEvent::Type::SettingsVersionError);
|
||||||
|
|
||||||
|
EXPECT_EQ(events.at(1).getType(), APIEvent::Type::OutputTruncated);
|
||||||
|
|
||||||
|
for(int i = 2; i < 49; i++)
|
||||||
|
EXPECT_EQ(events.at(i).getType(), APIEvent::Type::ParameterOutOfRange);
|
||||||
|
|
||||||
|
EXPECT_EQ(events.at(49).getType(), APIEvent::Type::TooManyEvents);
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue