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

Side by Side Diff: native_client_sdk/src/libraries/nacl_io_test/event_test.cc

Issue 19271009: [NaCl SDK} Add EventListener and EventEmitter to support epoll. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove extra mutex Created 7 years, 5 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 /* Copyright (c) 2013 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.
4 */
Sam Clegg 2013/07/17 17:34:56 C++ comment
Sam Clegg 2013/07/17 17:34:56 C++ comment
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <sys/stat.h>
9 #include <sys/time.h>
10
11 #include "gtest/gtest.h"
12
13 #include "nacl_io/event_emitter.h"
14 #include "nacl_io/event_listener.h"
15
16
17 class EventEmitterTester : public EventEmitter {
18 public:
19 EventEmitterTester() : event_status_(0), event_cnt_(0) {}
20
21 void SetEventStatus(uint32_t bits) { event_status_ = bits; }
22 uint32_t GetEventStatus() { return event_status_; }
23
24 int GetType() { return S_IFSOCK; }
25
26 int NumEvents() { return event_cnt_; }
27
28 public:
29 // Make this function public for testing
30 void RaiseEvent(uint32_t events) {
binji 2013/07/17 22:23:20 just use "using EventEmitter::RaiseEvent;"
noelallen1 2013/07/18 22:18:16 I think the style is a bit ambiguous here. Using
31 EventEmitter::RaiseEvent(events);
32 }
33
34 // Called after registering locally, but while lock is still held.
35 void ChainRegister(const ScopedEventInfo& event) {
36 event_cnt_++;
37 }
38
39 // Called before unregistering locally, but while lock is still held.
40 void ChainUnregister(const ScopedEventInfo& event) {
41 event_cnt_--;
42 }
43
44 protected:
45 uint32_t event_status_;
46 uint32_t event_cnt_;
47 };
48
49
50 const int MAX_EVENTS = 8;
51 const int FD_EMITTER = 5;
52 const int DUP_EMITTER = 6;
53
54 const uint32_t SIG_FILTER = 2;
55 const uint64_t SIG_DATA = 1;
56
57 TEST(EventTest, EmitterBasic) {
58 ScopedRef<EventEmitterTester> emitter(new EventEmitterTester());
59 ScopedRef<EventEmitter> null_emitter;
60 ScopedEventInfo null_info;
61
62 ScopedEventListener listener(new EventListener);
63
64 // Verify construction
65 EXPECT_EQ(0, emitter->NumEvents());
66 EXPECT_EQ(0, emitter->GetEventStatus());
67
68 // Verify status
69 emitter->SetEventStatus(1);
70 EXPECT_EQ(1, emitter->GetEventStatus());
71
72 // Fail to update an FD not in the set
73 EXPECT_EQ(ENOENT, listener->Update(FD_EMITTER, SIG_FILTER, SIG_DATA));
74 EXPECT_EQ(ENOENT, listener->Free(FD_EMITTER));
75
76 // Set the emitter filter and data
77 EXPECT_EQ(0, listener->Track(FD_EMITTER, emitter, SIG_FILTER, SIG_DATA));
78 EXPECT_EQ(1, emitter->NumEvents());
79
80 // Update an existing FD
81 EXPECT_EQ(0, listener->Update(FD_EMITTER, SIG_FILTER, SIG_DATA));
binji 2013/07/17 22:23:20 test that the values change?
noelallen1 2013/07/18 22:18:16 Done.
82
83 // Fail to add the same FD
84 EXPECT_EQ(EEXIST, listener->Track(FD_EMITTER, emitter, SIG_FILTER, SIG_DATA));
85 EXPECT_EQ(1, emitter->NumEvents());
86
87 int event_cnt = 0;
88 EventData ev[MAX_EVENTS];
89
90 // Do not allow a wait with no events
91 EXPECT_EQ(EINVAL, listener->Wait(ev, 0, 0, &event_cnt));
binji 2013/07/17 22:23:20 might be nice to mention that these are all withou
noelallen1 2013/07/18 22:18:16 I've replaced the const values with const variable
92
93 // Do not allow a wait with no events
94 EXPECT_EQ(EINVAL, listener->Wait(ev, -1, 0, &event_cnt));
95
96 // Do not allow a wait with a NULL EventData pointer
97 EXPECT_EQ(EFAULT, listener->Wait(NULL, MAX_EVENTS, 0, &event_cnt));
98
99 // Return unsignaled
100 memset(ev, 0, sizeof(ev));
101 event_cnt = 100;
102 emitter->SetEventStatus(0);
103 EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, 0, &event_cnt));
104 EXPECT_EQ(0, event_cnt);
105
106 // Return with one event signaled
107 event_cnt = 100;
108 emitter->SetEventStatus(3);
binji 2013/07/17 22:23:20 SIG_FILTER? Though maybe an explicit test that ev
109 EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, 0, &event_cnt));
110 EXPECT_EQ(1, event_cnt);
111 EXPECT_EQ(SIG_DATA, ev[0].data);
112 EXPECT_EQ(SIG_FILTER, ev[0].events);
113
114 // Support adding a DUP
115 EXPECT_EQ(0, listener->Track(DUP_EMITTER, emitter, SIG_FILTER, SIG_DATA));
116 EXPECT_EQ(2, emitter->NumEvents());
117
118 // Return unsignaled
119 memset(ev, 0, sizeof(ev));
120 emitter->SetEventStatus(0);
121 event_cnt = 100;
122 EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, 0, &event_cnt));
123 EXPECT_EQ(0, event_cnt);
124
125 // Return with two event signaled
126 emitter->SetEventStatus(3);
127 event_cnt = 100;
128 EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, 0, &event_cnt));
129 EXPECT_EQ(2, event_cnt);
130 EXPECT_EQ(SIG_DATA, ev[0].data);
131 EXPECT_EQ(SIG_FILTER, ev[0].events);
132 EXPECT_EQ(SIG_DATA, ev[1].data);
133 EXPECT_EQ(SIG_FILTER, ev[1].events);
Sam Clegg 2013/07/17 17:34:56 There are rather a lot of assertions in this one t
noelallen1 2013/07/17 21:00:18 I'm verifying the behavior of only a single call:
134 }
135
136 long Duration(struct timeval* start, struct timeval* end) {
137 if (start->tv_usec > end->tv_usec) {
138 end->tv_sec -= 1;
139 end->tv_usec += 1000000;
140 }
141 long cur_time = 1000 * (end->tv_sec - start->tv_sec);
142 cur_time += (end->tv_usec - start->tv_usec) / 1000;
143 return cur_time;
144 }
145
146
147 // Run a timed wait, and return the average of 8 iterations to reduce
148 // chance of false negative on outlier.
149 const int TRIES_TO_AVERAGE = 8;
150 bool TimedListen(ScopedEventListener& listen,
151 EventData* ev,
152 int ev_max,
153 int ev_expect,
154 int ms_wait,
155 long* duration) {
156
157 struct timeval start;
158 struct timeval end;
159 long total_time = 0;
160
161 for (int a=0; a < TRIES_TO_AVERAGE; a++) {
162 gettimeofday(&start, NULL);
163
164 int signaled;
165
166 EXPECT_EQ(0, listen->Wait(ev, ev_max, ms_wait, &signaled));
167 EXPECT_EQ(signaled, ev_expect);
168
169 if (signaled != ev_expect) {
170 return false;
171 }
172
173 gettimeofday(&end, NULL);
174
175 long cur_time = Duration(&start, &end);
176 total_time += cur_time;
177 }
178
179 *duration = total_time / TRIES_TO_AVERAGE;
180 return true;
181 }
182
183
184 // NOTE: These timing tests are potentially flacky, the real test is
binji 2013/07/17 22:23:20 sp: flaky
noelallen1 2013/07/18 22:18:16 Done.
185 // for the zero timeout should be, has the ConditionVariable been waited on?
186 // Once we provide a debuggable SimpleCond and SimpleLock we can actually test
187 // the correct thing.
188
189 // MAX_MS_TIMEOUT
190 const int NO_MS_TIMEOUT = 0;
191
192 // Wait at most .5 seconds
193 const int MAX_MS_TIMEOUT = 500;
194
195 // Wait forever
196 const int NEVER_TIMEOUT = -1;
197
198 // However, normal schedule would expect us to see < 10ms, but we'll
199 // use a much bigger number (yet smaller than the MAX_MS_TIMEOUT).
200 const int MAX_MS_EXPECTED = MAX_MS_TIMEOUT / 2;
201
202 const int EXPECT_ONE_EVENT = 1;
203 const int EXPECT_NO_EVENT = 0;
204
205 TEST(EventTest, EmitterTimeout) {
206 ScopedRef<EventEmitterTester> emitter(new EventEmitterTester());
207 ScopedEventListener listener(new EventListener());
208 long duration;
209
210 EventData ev[MAX_EVENTS];
211 memset(ev, 0, sizeof(ev));
212 EXPECT_EQ(0, listener->Track(FD_EMITTER, emitter, SIG_FILTER, SIG_DATA));
213
214 // Return immediately when emitter is signaled, with no timeout
215 emitter->SetEventStatus(SIG_FILTER);
216 memset(ev, 0, sizeof(ev));
217 EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_ONE_EVENT,
218 NO_MS_TIMEOUT, &duration));
219 EXPECT_EQ(SIG_DATA, ev[0].data);
220 EXPECT_EQ(SIG_FILTER, ev[0].events);
221 EXPECT_EQ(0, duration);
222
223 // Return immediately when emitter is signaled, even with timeout
224 emitter->SetEventStatus(SIG_FILTER);
225 memset(ev, 0, sizeof(ev));
226 EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_ONE_EVENT,
227 MAX_MS_TIMEOUT, &duration));
228 EXPECT_EQ(SIG_DATA, ev[0].data);
229 EXPECT_EQ(SIG_FILTER, ev[0].events);
230 EXPECT_GT(25, duration);
binji 2013/07/17 22:23:20 const for 25 too?
noelallen1 2013/07/18 22:18:16 Done.
231
232 // Return immediately when emitter is signaled, when block forever
233 emitter->SetEventStatus(SIG_FILTER);
234 memset(ev, 0, sizeof(ev));
235 EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_ONE_EVENT,
236 NEVER_TIMEOUT, &duration));
237 EXPECT_EQ(SIG_DATA, ev[0].data);
238 EXPECT_EQ(SIG_FILTER, ev[0].events);
239 EXPECT_GT(25, duration);
240
241 // Timeout immediately when emitter is not signaled
242 emitter->SetEventStatus(0);
243 memset(ev, 0, sizeof(ev));
244 EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_NO_EVENT,
245 NO_MS_TIMEOUT, &duration));
246 EXPECT_EQ(0, ev[0].data);
247 EXPECT_EQ(0, ev[0].events);
248 EXPECT_EQ(0, duration);
249
250 // Return after timeout when emitter not signaled
251 emitter->SetEventStatus(0);
252 memset(ev, 0, sizeof(ev));
253 EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_NO_EVENT,
254 MAX_MS_TIMEOUT, &duration));
255 EXPECT_EQ(0, ev[0].data);
256 EXPECT_EQ(0, ev[0].events);
257 EXPECT_LT(MAX_MS_TIMEOUT - 1, duration);
258 EXPECT_GT(MAX_MS_TIMEOUT + MAX_MS_TIMEOUT, duration);
Sam Clegg 2013/07/17 17:34:56 ditto.
noelallen1 2013/07/17 21:00:18 Done.
259 }
260
261 struct SignalInfo {
262 EventEmitterTester* em;
263 unsigned int ms_wait;
264 uint32_t events;
265 };
266
267 void *SignalEmitter(void *ptr) {
268 SignalInfo* info = (SignalInfo*) ptr;
269 struct timespec ts;
270 ts.tv_sec = 0;
271 ts.tv_nsec = info->ms_wait * 1000000;
272
273 nanosleep(&ts, NULL);
274
275 info->em->RaiseEvent(info->events);
276 return NULL;
277 }
278
279 const int VERY_LONG_WAIT = 1000;
280 TEST(EventTest, EmitterSignalling) {
281 ScopedRef<EventEmitterTester> emitter(new EventEmitterTester());
282 ScopedEventListener listener(new EventListener);
283
284 SignalInfo siginfo;
285 struct timeval start;
286 struct timeval end;
287 long duration;
288
289 EventData ev[MAX_EVENTS];
290 memset(ev, 0, sizeof(ev));
291 EXPECT_EQ(0, listener->Track(FD_EMITTER, emitter, SIG_FILTER, SIG_DATA));
292
293 siginfo.em = emitter.get();
294 siginfo.ms_wait = VERY_LONG_WAIT / 4;
295 siginfo.events = SIG_FILTER;
296
297 pthread_t tid;
298 pthread_create(&tid, NULL, SignalEmitter, &siginfo);
299
300 gettimeofday(&start, NULL);
301 int cnt = 0;
302 EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, VERY_LONG_WAIT, &cnt));
303 EXPECT_EQ(1, cnt);
304 gettimeofday(&end, NULL);
305
306 duration = Duration(&start, &end);
307 EXPECT_GT(VERY_LONG_WAIT, duration);
308 EXPECT_LT((VERY_LONG_WAIT / 4) - 1, duration);
309 EXPECT_EQ(SIG_DATA, ev[0].data);
310 EXPECT_EQ(SIG_FILTER, ev[0].events);
311 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698