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

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

Issue 999753005: Make sure that WaitableEvent::TimedWait works fine with large timeouts. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 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
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 "base/compiler_specific.h" 7 #include "base/compiler_specific.h"
8 #include "base/threading/platform_thread.h" 8 #include "base/threading/platform_thread.h"
9 #include "base/time/time.h" 9 #include "base/time/time.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 66
67 ev[0]->Signal(); 67 ev[0]->Signal();
68 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 0u); 68 EXPECT_EQ(WaitableEvent::WaitMany(ev, 5), 0u);
69 69
70 for (unsigned i = 0; i < 5; ++i) 70 for (unsigned i = 0; i < 5; ++i)
71 delete ev[i]; 71 delete ev[i];
72 } 72 }
73 73
74 class WaitableEventSignaler : public PlatformThread::Delegate { 74 class WaitableEventSignaler : public PlatformThread::Delegate {
75 public: 75 public:
76 WaitableEventSignaler(double seconds, WaitableEvent* ev) 76 WaitableEventSignaler(TimeDelta delay, WaitableEvent* event)
77 : seconds_(seconds), 77 : delay_(delay),
78 ev_(ev) { 78 event_(event) {
79 } 79 }
80 80
81 void ThreadMain() override { 81 void ThreadMain() override {
82 PlatformThread::Sleep(TimeDelta::FromSecondsD(seconds_)); 82 PlatformThread::Sleep(delay_);
83 ev_->Signal(); 83 event_->Signal();
84 } 84 }
85 85
86 private: 86 private:
87 const double seconds_; 87 TimeDelta delay_;
Lei Zhang 2015/03/25 01:36:27 nit: can this stay const since it doesn't change?
rvargas (doing something else) 2015/03/26 19:37:28 Done.
88 WaitableEvent *const ev_; 88 WaitableEvent* event_;
89 }; 89 };
90 90
91 // Tests that a WaitableEvent can be safely deleted when |Wait| is done without
92 // additional synchrnization.
Lei Zhang 2015/03/25 01:36:27 nit: typo (cut+pasted), ditto below.
rvargas (doing something else) 2015/03/26 19:37:28 Done.
91 TEST(WaitableEventTest, WaitAndDelete) { 93 TEST(WaitableEventTest, WaitAndDelete) {
92 // This test tests that if a WaitableEvent can be safely deleted
93 // when |Wait| is done without additional synchrnization.
94 // If this test crashes, it is a bug.
95
96 WaitableEvent* ev = new WaitableEvent(false, false); 94 WaitableEvent* ev = new WaitableEvent(false, false);
97 95
98 WaitableEventSignaler signaler(0.01, ev); 96 WaitableEventSignaler signaler(TimeDelta::FromMilliseconds(10), ev);
99 PlatformThreadHandle thread; 97 PlatformThreadHandle thread;
100 PlatformThread::Create(0, &signaler, &thread); 98 PlatformThread::Create(0, &signaler, &thread);
101 99
102 ev->Wait(); 100 ev->Wait();
103 delete ev; 101 delete ev;
104 102
105 PlatformThread::Join(thread); 103 PlatformThread::Join(thread);
106 } 104 }
107 105
106 // Tests that a WaitableEvent can be safely deleted when |WaitMany| is done
107 // without additional synchrnization.
108 TEST(WaitableEventTest, WaitMany) { 108 TEST(WaitableEventTest, WaitMany) {
109 // This test tests that if a WaitableEvent can be safely deleted
110 // when |WaitMany| is done without additional synchrnization.
111 // If this test crashes, it is a bug.
112
113 WaitableEvent* ev[5]; 109 WaitableEvent* ev[5];
114 for (unsigned i = 0; i < 5; ++i) 110 for (unsigned i = 0; i < 5; ++i)
115 ev[i] = new WaitableEvent(false, false); 111 ev[i] = new WaitableEvent(false, false);
116 112
117 WaitableEventSignaler signaler(0.01, ev[2]); 113 WaitableEventSignaler signaler(TimeDelta::FromMilliseconds(10), ev[2]);
118 PlatformThreadHandle thread; 114 PlatformThreadHandle thread;
119 PlatformThread::Create(0, &signaler, &thread); 115 PlatformThread::Create(0, &signaler, &thread);
120 116
121 size_t index = WaitableEvent::WaitMany(ev, 5); 117 size_t index = WaitableEvent::WaitMany(ev, 5);
122 118
123 for (unsigned i = 0; i < 5; ++i) 119 for (unsigned i = 0; i < 5; ++i)
124 delete ev[i]; 120 delete ev[i];
125 121
126 PlatformThread::Join(thread); 122 PlatformThread::Join(thread);
127 EXPECT_EQ(2u, index); 123 EXPECT_EQ(2u, index);
128 } 124 }
129 125
126 // Tests that using TimeDelta::Max() on TimedWait() is not the same as passing
127 // a timeout of 0. (crbug.com/465948)
128 TEST(WaitableEventTest, TimedWait) {
129 WaitableEvent* ev = new WaitableEvent(false, false);
130
131 TimeDelta thread_delay = TimeDelta::FromMilliseconds(10);
132 WaitableEventSignaler signaler(thread_delay, ev);
133 PlatformThreadHandle thread;
134 TimeTicks start = TimeTicks::Now();
135 PlatformThread::Create(0, &signaler, &thread);
136
137 ev->TimedWait(TimeDelta::Max());
138 EXPECT_GE(TimeTicks::Now() - start, thread_delay);
139 delete ev;
140
141 PlatformThread::Join(thread);
142
143 TimeTicks end_time(TimeTicks::Now() + TimeDelta::Max());
Lei Zhang 2015/03/25 01:36:27 I can't tell what part this plays in the test. Sho
rvargas (doing something else) 2015/03/26 19:37:28 Sorry, this was a quick test I did weeks ago befor
144 EXPECT_LT(TimeTicks(), end_time);
145 }
146
130 } // namespace base 147 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698