test_eventbus.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <gtest/gtest.h>
  21. #include <chrono>
  22. #include <ctime>
  23. #include <string>
  24. #include <vector>
  25. #include "cnstream_eventbus.hpp"
  26. #include "cnstream_pipeline.hpp"
  27. namespace cnstream {
  28. const EventType g_type = EventType::EVENT_ERROR;
  29. const char *g_message = "test post event";
  30. std::thread::id g_thread_id;
  31. EventHandleFlag TestBusWatcher(const Event &event) {
  32. EXPECT_EQ(event.type, g_type);
  33. EXPECT_STREQ(event.message.c_str(), g_message);
  34. // EXPECT_EQ(event.module_name, module);
  35. EXPECT_EQ(event.thread_id, g_thread_id);
  36. return EventHandleFlag::EVENT_HANDLE_SYNCED;
  37. }
  38. TEST(CoreEventBus, AddBusWatcher) {
  39. Pipeline pipe("pipe");
  40. auto bus = pipe.GetEventBus();
  41. uint32_t num = bus->AddBusWatch(TestBusWatcher);
  42. EXPECT_EQ(num, (uint32_t)2);
  43. pipe.Start();
  44. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  45. pipe.Stop();
  46. }
  47. TEST(CoreEventBus, PostEvent) {
  48. Pipeline pipe("pipe");
  49. auto bus = pipe.GetEventBus();
  50. bus->AddBusWatch(TestBusWatcher);
  51. Event event;
  52. event.type = g_type;
  53. event.message = g_message;
  54. event.stream_id = "test_stream";
  55. event.module_name = "pipe";
  56. event.thread_id = std::this_thread::get_id();
  57. g_thread_id = event.thread_id;
  58. EXPECT_FALSE(bus->PostEvent(event)) << "bus should reject event while pipeline not running";
  59. pipe.Start();
  60. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  61. EXPECT_TRUE(bus->PostEvent(event));
  62. pipe.Stop();
  63. }
  64. TEST(CoreEventBus, PollEvent) {
  65. Pipeline pipe("pipe");
  66. auto bus = pipe.GetEventBus();
  67. Event event;
  68. event.type = EventType::EVENT_WARNING;
  69. event.message = "test poll";
  70. event.stream_id = "test_stream";
  71. event.module_name = "pipe";
  72. EXPECT_EQ(bus->PollEvent().type, EventType::EVENT_STOP);
  73. bus->ClearAllWatchers();
  74. pipe.Start();
  75. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  76. ASSERT_TRUE(bus->PostEvent(event));
  77. Event poll_e = bus->PollEventToTest();
  78. EXPECT_EQ(poll_e.type, event.type);
  79. EXPECT_EQ(poll_e.stream_id, event.stream_id);
  80. EXPECT_EQ(poll_e.message, event.message);
  81. EXPECT_EQ(poll_e.module_name, event.module_name);
  82. pipe.Stop();
  83. }
  84. TEST(CoreEventBus, ClearAllBusWatchers) {
  85. Pipeline pipe("pipe");
  86. auto bus = pipe.GetEventBus();
  87. EXPECT_EQ(bus->GetBusWatchers().size(), uint32_t(1));
  88. bus->AddBusWatch(TestBusWatcher);
  89. EXPECT_EQ(bus->GetBusWatchers().size(), uint32_t(2));
  90. bus->ClearAllWatchers();
  91. EXPECT_EQ(bus->GetBusWatchers().size(), uint32_t(0));
  92. }
  93. } // namespace cnstream