Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(324)

Side by Side Diff: base/synchronization/waitable_event_unittest.cc

Issue 2758853002: Ensure consistent return ordering in base::WaitableEvent::WaitMany (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/synchronization/waitable_event_posix.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/synchronization/waitable_event.h" 5 #include "base/synchronization/waitable_event.h"
6 6
7 #include <stddef.h> 7 #include <stddef.h>
8 8
9 #include <algorithm>
10
9 #include "base/compiler_specific.h" 11 #include "base/compiler_specific.h"
10 #include "base/threading/platform_thread.h" 12 #include "base/threading/platform_thread.h"
11 #include "base/time/time.h" 13 #include "base/time/time.h"
12 #include "build/build_config.h" 14 #include "build/build_config.h"
13 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
14 16
15 namespace base { 17 namespace base {
16 18
17 TEST(WaitableEventTest, ManualBasics) { 19 TEST(WaitableEventTest, ManualBasics) {
18 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL, 20 WaitableEvent event(WaitableEvent::ResetPolicy::MANUAL,
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 ev[4]->Signal(); 73 ev[4]->Signal();
72 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 4u); 74 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 4u);
73 75
74 ev[0]->Signal(); 76 ev[0]->Signal();
75 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 0u); 77 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 0u);
76 78
77 for (unsigned i = 0; i < 5; ++i) 79 for (unsigned i = 0; i < 5; ++i)
78 delete ev[i]; 80 delete ev[i];
79 } 81 }
80 82
83 TEST(WaitableEventTest, WaitManyLeftToRight) {
84 WaitableEvent* ev[5];
85 for (size_t i = 0; i < 5; ++i) {
86 ev[i] = new WaitableEvent(WaitableEvent::ResetPolicy::AUTOMATIC,
87 WaitableEvent::InitialState::NOT_SIGNALED);
88 }
89
90 // Test for consistent left-to-right return behavior across all permutations
91 // of the input array. This is to verify that only the indices -- and not
92 // the WaitableEvents' addresses -- are relevant in determining who wins when
93 // multiple events are signaled.
94
95 std::sort(ev, ev + 5);
96 do {
97 ev[0]->Signal();
98 ev[1]->Signal();
99 EXPECT_EQ(0u, WaitableEvent::WaitMany(ev, 5));
100
101 ev[2]->Signal();
102 EXPECT_EQ(1u, WaitableEvent::WaitMany(ev, 5));
103 EXPECT_EQ(2u, WaitableEvent::WaitMany(ev, 5));
104
105 ev[3]->Signal();
106 ev[4]->Signal();
107 ev[0]->Signal();
108 EXPECT_EQ(0u, WaitableEvent::WaitMany(ev, 5));
109 EXPECT_EQ(3u, WaitableEvent::WaitMany(ev, 5));
110 ev[2]->Signal();
111 EXPECT_EQ(2u, WaitableEvent::WaitMany(ev, 5));
112 EXPECT_EQ(4u, WaitableEvent::WaitMany(ev, 5));
113 } while (std::next_permutation(ev, ev + 5));
114
115 for (size_t i = 0; i < 5; ++i)
116 delete ev[i];
117 }
118
81 class WaitableEventSignaler : public PlatformThread::Delegate { 119 class WaitableEventSignaler : public PlatformThread::Delegate {
82 public: 120 public:
83 WaitableEventSignaler(TimeDelta delay, WaitableEvent* event) 121 WaitableEventSignaler(TimeDelta delay, WaitableEvent* event)
84 : delay_(delay), 122 : delay_(delay),
85 event_(event) { 123 event_(event) {
86 } 124 }
87 125
88 void ThreadMain() override { 126 void ThreadMain() override {
89 PlatformThread::Sleep(delay_); 127 PlatformThread::Sleep(delay_);
90 event_->Signal(); 128 event_->Signal();
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after
195 start_time = TimeTicks::Now(); 233 start_time = TimeTicks::Now();
196 PlatformThread::Create(0, &signaler, &thread); 234 PlatformThread::Create(0, &signaler, &thread);
197 235
198 EXPECT_TRUE(ev.TimedWaitUntil(TimeTicks::Max())); 236 EXPECT_TRUE(ev.TimedWaitUntil(TimeTicks::Max()));
199 EXPECT_GE(TimeTicks::Now() - start_time, delay); 237 EXPECT_GE(TimeTicks::Now() - start_time, delay);
200 238
201 PlatformThread::Join(thread); 239 PlatformThread::Join(thread);
202 } 240 }
203 241
204 } // namespace base 242 } // namespace base
OLDNEW
« no previous file with comments | « base/synchronization/waitable_event_posix.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698