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

Side by Side Diff: base/waitable_event_unittest.cc

Issue 16554: WaitableEvent (Closed)
Patch Set: Addresssing darin's comments (round 2) Created 11 years, 11 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/waitable_event_posix.cc ('k') | base/waitable_event_watcher.h » ('j') | 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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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/time.h" 5 #include "base/time.h"
6 #include "base/waitable_event.h" 6 #include "base/waitable_event.h"
7 #include "base/platform_thread.h"
7 #include "testing/gtest/include/gtest/gtest.h" 8 #include "testing/gtest/include/gtest/gtest.h"
8 9
9 using base::TimeDelta; 10 using base::TimeDelta;
10 using base::WaitableEvent; 11 using base::WaitableEvent;
11 12
12 namespace { 13 namespace {
13 typedef testing::Test WaitableEventTest; 14 typedef testing::Test WaitableEventTest;
14 } 15 }
15 16
16 TEST(WaitableEventTest, ManualBasics) { 17 TEST(WaitableEventTest, ManualBasics) {
(...skipping 28 matching lines...) Expand all
45 EXPECT_FALSE(event.TimedWait(TimeDelta::FromMilliseconds(10))); 46 EXPECT_FALSE(event.TimedWait(TimeDelta::FromMilliseconds(10)));
46 47
47 event.Signal(); 48 event.Signal();
48 EXPECT_TRUE(event.Wait()); 49 EXPECT_TRUE(event.Wait());
49 EXPECT_FALSE(event.TimedWait(TimeDelta::FromMilliseconds(10))); 50 EXPECT_FALSE(event.TimedWait(TimeDelta::FromMilliseconds(10)));
50 51
51 event.Signal(); 52 event.Signal();
52 EXPECT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(10))); 53 EXPECT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(10)));
53 } 54 }
54 55
56 TEST(WaitableEventTest, WaitManyShortcut) {
57 WaitableEvent* ev[5];
58 for (unsigned i = 0; i < 5; ++i)
59 ev[i] = new WaitableEvent(false, false);
60
61 ev[3]->Signal();
62 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 3u);
63
64 ev[3]->Signal();
65 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 3u);
66
67 ev[4]->Signal();
68 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 4u);
69
70 ev[0]->Signal();
71 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 0u);
72
73 for (unsigned i = 0; i < 5; ++i)
74 delete ev[i];
75 }
76
77 class WaitableEventSignaler : public PlatformThread::Delegate {
78 public:
79 WaitableEventSignaler(double seconds, WaitableEvent* ev)
80 : seconds_(seconds),
81 ev_(ev) {
82 }
83
84 void ThreadMain() {
85 PlatformThread::Sleep(static_cast<int>(seconds_ * 1000));
86 ev_->Signal();
87 }
88
89 private:
90 const double seconds_;
91 WaitableEvent *const ev_;
92 };
93
94 TEST(WaitableEventTest, WaitMany) {
95 WaitableEvent* ev[5];
96 for (unsigned i = 0; i < 5; ++i)
97 ev[i] = new WaitableEvent(false, false);
98
99 WaitableEventSignaler signaler(0.1, ev[2]);
100 PlatformThreadHandle thread;
101 PlatformThread::Create(0, &signaler, &thread);
102
103 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 2u);
104
105 PlatformThread::Join(thread);
106
107 for (unsigned i = 0; i < 5; ++i)
108 delete ev[i];
109 }
OLDNEW
« no previous file with comments | « base/waitable_event_posix.cc ('k') | base/waitable_event_watcher.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698