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
3 // found in the LICENSE file.
6 #include "base/waitable_event.h"
7 #include "base/platform_thread.h"
8 #include "testing/gtest/include/gtest/gtest.h"
10 using base::TimeDelta
;
11 using base::WaitableEvent
;
14 typedef testing::Test WaitableEventTest
;
17 TEST(WaitableEventTest
, ManualBasics
) {
18 WaitableEvent
event(true, false);
20 EXPECT_FALSE(event
.IsSignaled());
23 EXPECT_TRUE(event
.IsSignaled());
24 EXPECT_TRUE(event
.IsSignaled());
27 EXPECT_FALSE(event
.IsSignaled());
28 EXPECT_FALSE(event
.TimedWait(TimeDelta::FromMilliseconds(10)));
31 EXPECT_TRUE(event
.Wait());
32 EXPECT_TRUE(event
.TimedWait(TimeDelta::FromMilliseconds(10)));
35 TEST(WaitableEventTest
, AutoBasics
) {
36 WaitableEvent
event(false, false);
38 EXPECT_FALSE(event
.IsSignaled());
41 EXPECT_TRUE(event
.IsSignaled());
42 EXPECT_FALSE(event
.IsSignaled());
45 EXPECT_FALSE(event
.IsSignaled());
46 EXPECT_FALSE(event
.TimedWait(TimeDelta::FromMilliseconds(10)));
49 EXPECT_TRUE(event
.Wait());
50 EXPECT_FALSE(event
.TimedWait(TimeDelta::FromMilliseconds(10)));
53 EXPECT_TRUE(event
.TimedWait(TimeDelta::FromMilliseconds(10)));
56 TEST(WaitableEventTest
, WaitManyShortcut
) {
58 for (unsigned i
= 0; i
< 5; ++i
)
59 ev
[i
] = new WaitableEvent(false, false);
62 EXPECT_EQ(WaitableEvent::WaitMany(ev
, 5), 3u);
65 EXPECT_EQ(WaitableEvent::WaitMany(ev
, 5), 3u);
68 EXPECT_EQ(WaitableEvent::WaitMany(ev
, 5), 4u);
71 EXPECT_EQ(WaitableEvent::WaitMany(ev
, 5), 0u);
73 for (unsigned i
= 0; i
< 5; ++i
)
77 class WaitableEventSignaler
: public PlatformThread::Delegate
{
79 WaitableEventSignaler(double seconds
, WaitableEvent
* ev
)
85 PlatformThread::Sleep(static_cast<int>(seconds_
* 1000));
90 const double seconds_
;
91 WaitableEvent
*const ev_
;
94 TEST(WaitableEventTest
, WaitMany
) {
96 for (unsigned i
= 0; i
< 5; ++i
)
97 ev
[i
] = new WaitableEvent(false, false);
99 WaitableEventSignaler
signaler(0.1, ev
[2]);
100 PlatformThreadHandle thread
;
101 PlatformThread::Create(0, &signaler
, &thread
);
103 EXPECT_EQ(WaitableEvent::WaitMany(ev
, 5), 2u);
105 PlatformThread::Join(thread
);
107 for (unsigned i
= 0; i
< 5; ++i
)