You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

urErrorManagerTest.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #include <iostream>
  2. #include "gtest/gtest.h"
  3. #include "ovtAssert.h"
  4. #include "ovtTestFixtureCommon.h"
  5. // DO NOT USE a global Test::ScopedTest<Test::SKernelFixture> variable here
  6. // because it causes a bug due to plugins global descriptors beeing destroyed before the kernel context.
  7. OpenViBE::Kernel::IKernelContext* context = nullptr;
  8. TEST(error_manager_test_case, test_init)
  9. {
  10. // here we use assert because we want to fail directly
  11. // in order to avoid a segfault
  12. ASSERT_TRUE(context != nullptr);
  13. auto& errorManager = context->getErrorManager();
  14. // check manager is correctly initialized
  15. EXPECT_FALSE(errorManager.hasError());
  16. EXPECT_TRUE(errorManager.getLastError() == nullptr);
  17. EXPECT_TRUE(std::string(errorManager.getLastErrorString()).empty());
  18. EXPECT_EQ(errorManager.getLastErrorType(), OpenViBE::Kernel::ErrorType::NoErrorFound);
  19. EXPECT_NO_THROW(errorManager.releaseErrors());
  20. }
  21. TEST(error_manager_test_case, test_push)
  22. {
  23. // here we use assert because we want to fail directly
  24. // in order to avoid a segfault
  25. ASSERT_TRUE(context != nullptr);
  26. auto& errorManager = context->getErrorManager();
  27. // push an error
  28. errorManager.pushError(OpenViBE::Kernel::ErrorType::Overflow, "An integer overflow error occurred");
  29. EXPECT_TRUE(errorManager.hasError());
  30. EXPECT_STREQ(errorManager.getLastErrorString(), "An integer overflow error occurred");
  31. EXPECT_EQ(errorManager.getLastErrorType(), OpenViBE::Kernel::ErrorType::Overflow);
  32. // test match error features returned direclty by manager match error features
  33. auto error = errorManager.getLastError();
  34. ASSERT_TRUE(error != nullptr);
  35. EXPECT_STREQ(error->getErrorString(), "An integer overflow error occurred");
  36. EXPECT_EQ(error->getErrorType(), OpenViBE::Kernel::ErrorType::Overflow);
  37. EXPECT_STREQ(error->getErrorLocation(), "NoLocationInfo:0");
  38. EXPECT_TRUE(error->getNestedError() == nullptr);
  39. // push another error
  40. errorManager.pushErrorAtLocation(OpenViBE::Kernel::ErrorType::BadAlloc, "Memory allocation failed", "urErrorManagerTest.cpp", 64);
  41. // test top error has changed
  42. EXPECT_STREQ(errorManager.getLastErrorString(), "Memory allocation failed");
  43. EXPECT_EQ(errorManager.getLastErrorType(), OpenViBE::Kernel::ErrorType::BadAlloc);
  44. error = errorManager.getLastError();
  45. ASSERT_TRUE(error != nullptr);
  46. EXPECT_STREQ(error->getErrorString(), "Memory allocation failed");
  47. EXPECT_EQ(error->getErrorType(), OpenViBE::Kernel::ErrorType::BadAlloc);
  48. EXPECT_STREQ(error->getErrorLocation(), "urErrorManagerTest.cpp:64");
  49. auto nestedError = error->getNestedError();
  50. ASSERT_TRUE(nestedError != nullptr);
  51. EXPECT_STREQ(nestedError->getErrorString(), "An integer overflow error occurred");
  52. EXPECT_EQ(nestedError->getErrorType(), OpenViBE::Kernel::ErrorType::Overflow);
  53. EXPECT_STREQ(nestedError->getErrorLocation(), "NoLocationInfo:0");
  54. EXPECT_TRUE(nestedError->getNestedError() == nullptr);
  55. }
  56. TEST(error_manager_test_case, test_release)
  57. {
  58. // here we use assert because we want to fail directly
  59. // in order to avoid a segfault
  60. ASSERT_TRUE(context != nullptr);
  61. auto& errorManager = context->getErrorManager();
  62. errorManager.releaseErrors();
  63. // check manager is correctly released
  64. EXPECT_FALSE(errorManager.hasError());
  65. EXPECT_TRUE(errorManager.getLastError() == nullptr);
  66. EXPECT_TRUE(std::string(errorManager.getLastErrorString()).empty());
  67. EXPECT_EQ(errorManager.getLastErrorType(), OpenViBE::Kernel::ErrorType::NoErrorFound);
  68. // add an error after release
  69. errorManager.pushErrorAtLocation(OpenViBE::Kernel::ErrorType::ResourceNotFound, "File not found on system", "urErrorManagerTest.cpp", 93);
  70. EXPECT_TRUE(errorManager.hasError());
  71. EXPECT_STREQ(errorManager.getLastErrorString(), "File not found on system");
  72. EXPECT_EQ(errorManager.getLastErrorType(), OpenViBE::Kernel::ErrorType::ResourceNotFound);
  73. auto error = errorManager.getLastError();
  74. ASSERT_TRUE(error != nullptr);
  75. EXPECT_STREQ(error->getErrorString(), "File not found on system");
  76. EXPECT_EQ(error->getErrorType(), OpenViBE::Kernel::ErrorType::ResourceNotFound);
  77. EXPECT_STREQ(error->getErrorLocation(), "urErrorManagerTest.cpp:93");
  78. EXPECT_TRUE(error->getNestedError() == nullptr);
  79. }
  80. TEST(error_manager_test_case, test_stress_push)
  81. {
  82. // here we use assert because we want to fail directly
  83. // in order to avoid a segfault
  84. ASSERT_TRUE(context != nullptr);
  85. auto& errorManager = context->getErrorManager();
  86. errorManager.releaseErrors();
  87. const size_t expectedErrorCount = 10;
  88. for (size_t i = 0; i < expectedErrorCount; ++i) { errorManager.pushError(OpenViBE::Kernel::ErrorType::Unknown, "Error"); }
  89. size_t errorCount = 0;
  90. auto error = errorManager.getLastError();
  91. while (error)
  92. {
  93. errorCount++;
  94. error = error->getNestedError();
  95. }
  96. EXPECT_EQ(errorCount, expectedErrorCount);
  97. }
  98. int urErrorManagerTest(int argc, char* argv[])
  99. {
  100. OVT_ASSERT(argc >= 2, "Failure retrieve test parameters");
  101. OpenViBE::Test::ScopedTest<OpenViBE::Test::SKernelFixture> fixture;
  102. fixture->setConfigFile(argv[1]);
  103. context = fixture->context;
  104. testing::InitGoogleTest(&argc, argv);
  105. ::testing::GTEST_FLAG(filter) = "error_manager_test_case.*";
  106. return RUN_ALL_TESTS();
  107. }