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.

95 lines
2.1 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2004 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/event.h"
  11. #include "rtc_base/platform_thread.h"
  12. #include "test/gtest.h"
  13. namespace rtc {
  14. TEST(EventTest, InitiallySignaled) {
  15. Event event(false, true);
  16. ASSERT_TRUE(event.Wait(0));
  17. }
  18. TEST(EventTest, ManualReset) {
  19. Event event(true, false);
  20. ASSERT_FALSE(event.Wait(0));
  21. event.Set();
  22. ASSERT_TRUE(event.Wait(0));
  23. ASSERT_TRUE(event.Wait(0));
  24. event.Reset();
  25. ASSERT_FALSE(event.Wait(0));
  26. }
  27. TEST(EventTest, AutoReset) {
  28. Event event;
  29. ASSERT_FALSE(event.Wait(0));
  30. event.Set();
  31. ASSERT_TRUE(event.Wait(0));
  32. ASSERT_FALSE(event.Wait(0));
  33. }
  34. class SignalerThread {
  35. public:
  36. SignalerThread() : thread_(&ThreadFn, this, "EventPerf") {}
  37. void Start(Event* writer, Event* reader) {
  38. writer_ = writer;
  39. reader_ = reader;
  40. thread_.Start();
  41. }
  42. void Stop() {
  43. stop_event_.Set();
  44. thread_.Stop();
  45. }
  46. static void ThreadFn(void* param) {
  47. auto* me = static_cast<SignalerThread*>(param);
  48. while (!me->stop_event_.Wait(0)) {
  49. me->writer_->Set();
  50. me->reader_->Wait(Event::kForever);
  51. }
  52. }
  53. Event stop_event_;
  54. Event* writer_;
  55. Event* reader_;
  56. PlatformThread thread_;
  57. };
  58. // These tests are disabled by default and only intended to be run manually.
  59. TEST(EventTest, DISABLED_PerformanceSingleThread) {
  60. static const int kNumIterations = 10000000;
  61. Event event;
  62. for (int i = 0; i < kNumIterations; ++i) {
  63. event.Set();
  64. event.Wait(0);
  65. }
  66. }
  67. TEST(EventTest, DISABLED_PerformanceMultiThread) {
  68. static const int kNumIterations = 10000;
  69. Event read;
  70. Event write;
  71. SignalerThread thread;
  72. thread.Start(&read, &write);
  73. for (int i = 0; i < kNumIterations; ++i) {
  74. write.Set();
  75. read.Wait(Event::kForever);
  76. }
  77. write.Set();
  78. thread.Stop();
  79. }
  80. } // namespace rtc