Add ICU message format support
[chromium-blink-merge.git] / base / synchronization / waitable_event_unittest.cc
blobbe56cf171a14798184e8f4e4a29b5830264fe87a
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
3 // found in the LICENSE file.
5 #include "base/synchronization/waitable_event.h"
7 #include "base/compiler_specific.h"
8 #include "base/threading/platform_thread.h"
9 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h"
12 namespace base {
14 TEST(WaitableEventTest, ManualBasics) {
15 WaitableEvent event(true, false);
17 EXPECT_FALSE(event.IsSignaled());
19 event.Signal();
20 EXPECT_TRUE(event.IsSignaled());
21 EXPECT_TRUE(event.IsSignaled());
23 event.Reset();
24 EXPECT_FALSE(event.IsSignaled());
25 EXPECT_FALSE(event.TimedWait(TimeDelta::FromMilliseconds(10)));
27 event.Signal();
28 event.Wait();
29 EXPECT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(10)));
32 TEST(WaitableEventTest, AutoBasics) {
33 WaitableEvent event(false, false);
35 EXPECT_FALSE(event.IsSignaled());
37 event.Signal();
38 EXPECT_TRUE(event.IsSignaled());
39 EXPECT_FALSE(event.IsSignaled());
41 event.Reset();
42 EXPECT_FALSE(event.IsSignaled());
43 EXPECT_FALSE(event.TimedWait(TimeDelta::FromMilliseconds(10)));
45 event.Signal();
46 event.Wait();
47 EXPECT_FALSE(event.TimedWait(TimeDelta::FromMilliseconds(10)));
49 event.Signal();
50 EXPECT_TRUE(event.TimedWait(TimeDelta::FromMilliseconds(10)));
53 TEST(WaitableEventTest, WaitManyShortcut) {
54 WaitableEvent* ev[5];
55 for (unsigned i = 0; i < 5; ++i)
56 ev[i] = new WaitableEvent(false, false);
58 ev[3]->Signal();
59 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 3u);
61 ev[3]->Signal();
62 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 3u);
64 ev[4]->Signal();
65 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 4u);
67 ev[0]->Signal();
68 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 0u);
70 for (unsigned i = 0; i < 5; ++i)
71 delete ev[i];
74 class WaitableEventSignaler : public PlatformThread::Delegate {
75 public:
76 WaitableEventSignaler(TimeDelta delay, WaitableEvent* event)
77 : delay_(delay),
78 event_(event) {
81 void ThreadMain() override {
82 PlatformThread::Sleep(delay_);
83 event_->Signal();
86 private:
87 const TimeDelta delay_;
88 WaitableEvent* event_;
91 // Tests that a WaitableEvent can be safely deleted when |Wait| is done without
92 // additional synchronization.
93 TEST(WaitableEventTest, WaitAndDelete) {
94 WaitableEvent* ev = new WaitableEvent(false, false);
96 WaitableEventSignaler signaler(TimeDelta::FromMilliseconds(10), ev);
97 PlatformThreadHandle thread;
98 PlatformThread::Create(0, &signaler, &thread);
100 ev->Wait();
101 delete ev;
103 PlatformThread::Join(thread);
106 // Tests that a WaitableEvent can be safely deleted when |WaitMany| is done
107 // without additional synchronization.
108 TEST(WaitableEventTest, WaitMany) {
109 WaitableEvent* ev[5];
110 for (unsigned i = 0; i < 5; ++i)
111 ev[i] = new WaitableEvent(false, false);
113 WaitableEventSignaler signaler(TimeDelta::FromMilliseconds(10), ev[2]);
114 PlatformThreadHandle thread;
115 PlatformThread::Create(0, &signaler, &thread);
117 size_t index = WaitableEvent::WaitMany(ev, 5);
119 for (unsigned i = 0; i < 5; ++i)
120 delete ev[i];
122 PlatformThread::Join(thread);
123 EXPECT_EQ(2u, index);
126 // Tests that using TimeDelta::Max() on TimedWait() is not the same as passing
127 // a timeout of 0. (crbug.com/465948)
128 #if defined(OS_POSIX)
129 // crbug.com/465948 not fixed yet.
130 #define MAYBE_TimedWait DISABLED_TimedWait
131 #else
132 #define MAYBE_TimedWait TimedWait
133 #endif
134 TEST(WaitableEventTest, MAYBE_TimedWait) {
135 WaitableEvent* ev = new WaitableEvent(false, false);
137 TimeDelta thread_delay = TimeDelta::FromMilliseconds(10);
138 WaitableEventSignaler signaler(thread_delay, ev);
139 PlatformThreadHandle thread;
140 TimeTicks start = TimeTicks::Now();
141 PlatformThread::Create(0, &signaler, &thread);
143 ev->TimedWait(TimeDelta::Max());
144 EXPECT_GE(TimeTicks::Now() - start, thread_delay);
145 delete ev;
147 PlatformThread::Join(thread);
150 } // namespace base