OLD | NEW |
(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 */ |
| 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 using namespace nacl_io; |
| 18 using namespace sdk_util; |
| 19 |
| 20 class EventEmitterTester : public EventEmitter { |
| 21 public: |
| 22 EventEmitterTester() : event_status_(0), event_cnt_(0) {} |
| 23 |
| 24 void SetEventStatus(uint32_t bits) { event_status_ = bits; } |
| 25 uint32_t GetEventStatus() { return event_status_; } |
| 26 |
| 27 int GetType() { return S_IFSOCK; } |
| 28 |
| 29 int NumEvents() { return event_cnt_; } |
| 30 |
| 31 public: |
| 32 // Make this function public for testing |
| 33 void RaiseEvent(uint32_t events) { |
| 34 EventEmitter::RaiseEvent(events); |
| 35 } |
| 36 |
| 37 // Called after registering locally, but while lock is still held. |
| 38 void ChainRegisterEventInfo(const ScopedEventInfo& event) { |
| 39 event_cnt_++; |
| 40 } |
| 41 |
| 42 // Called before unregistering locally, but while lock is still held. |
| 43 void ChainUnregisterEventInfo(const ScopedEventInfo& event) { |
| 44 event_cnt_--; |
| 45 } |
| 46 |
| 47 protected: |
| 48 uint32_t event_status_; |
| 49 uint32_t event_cnt_; |
| 50 }; |
| 51 |
| 52 |
| 53 const int MAX_EVENTS = 8; |
| 54 |
| 55 // IDs for Emitters |
| 56 const int ID_EMITTER = 5; |
| 57 const int ID_LISTENER = 6; |
| 58 const int ID_EMITTER_DUP = 7; |
| 59 |
| 60 // Kernel Event values |
| 61 const uint32_t KE_EXPECTED = 4; |
| 62 const uint32_t KE_FILTERED = 2; |
| 63 const uint32_t KE_NONE = 0; |
| 64 |
| 65 // User Data values |
| 66 const uint64_t USER_DATA_A = 1; |
| 67 const uint64_t USER_DATA_B = 5; |
| 68 |
| 69 // Timeout durations |
| 70 const int TIMEOUT_IMMEDIATE = 0; |
| 71 const int TIMEOUT_SHORT= 100; |
| 72 const int TIMEOUT_LONG = 500; |
| 73 const int TIMEOUT_NEVER = -1; |
| 74 const int TIMEOUT_VERY_LONG = 1000; |
| 75 |
| 76 TEST(EventTest, EmitterBasic) { |
| 77 ScopedRef<EventEmitterTester> emitter(new EventEmitterTester()); |
| 78 ScopedRef<EventEmitter> null_emitter; |
| 79 |
| 80 ScopedEventListener listener(new EventListener); |
| 81 |
| 82 // Verify construction |
| 83 EXPECT_EQ(0, emitter->NumEvents()); |
| 84 EXPECT_EQ(0, emitter->GetEventStatus()); |
| 85 |
| 86 // Verify status |
| 87 emitter->SetEventStatus(KE_EXPECTED); |
| 88 EXPECT_EQ(KE_EXPECTED, emitter->GetEventStatus()); |
| 89 |
| 90 // Fail to update or free an ID not in the set |
| 91 EXPECT_EQ(ENOENT, listener->Update(ID_EMITTER, KE_EXPECTED, USER_DATA_A)); |
| 92 EXPECT_EQ(ENOENT, listener->Free(ID_EMITTER)); |
| 93 |
| 94 // Fail to Track self |
| 95 EXPECT_EQ(EINVAL, listener->Track(ID_LISTENER, |
| 96 listener, |
| 97 KE_EXPECTED, |
| 98 USER_DATA_A)); |
| 99 |
| 100 // Set the emitter filter and data |
| 101 EXPECT_EQ(0, listener->Track(ID_EMITTER, emitter, KE_EXPECTED, USER_DATA_A)); |
| 102 EXPECT_EQ(1, emitter->NumEvents()); |
| 103 |
| 104 // Fail to add the same ID |
| 105 EXPECT_EQ(EEXIST, |
| 106 listener->Track(ID_EMITTER, emitter, KE_EXPECTED, USER_DATA_A)); |
| 107 EXPECT_EQ(1, emitter->NumEvents()); |
| 108 |
| 109 int event_cnt = 0; |
| 110 EventData ev[MAX_EVENTS]; |
| 111 |
| 112 // Do not allow a wait with a zero events count. |
| 113 EXPECT_EQ(EINVAL, listener->Wait(ev, 0, TIMEOUT_IMMEDIATE, &event_cnt)); |
| 114 |
| 115 // Do not allow a wait with a negative events count. |
| 116 EXPECT_EQ(EINVAL, listener->Wait(ev, -1, TIMEOUT_IMMEDIATE, &event_cnt)); |
| 117 |
| 118 // Do not allow a wait with a NULL EventData pointer |
| 119 EXPECT_EQ(EFAULT, |
| 120 listener->Wait(NULL, MAX_EVENTS, TIMEOUT_IMMEDIATE, &event_cnt)); |
| 121 |
| 122 // Return with no events if the Emitter has no signals set. |
| 123 memset(ev, 0, sizeof(ev)); |
| 124 event_cnt = 100; |
| 125 emitter->SetEventStatus(KE_NONE); |
| 126 EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, TIMEOUT_IMMEDIATE, &event_cnt)); |
| 127 EXPECT_EQ(0, event_cnt); |
| 128 |
| 129 // Return with no events if the Emitter has a filtered signals set. |
| 130 memset(ev, 0, sizeof(ev)); |
| 131 event_cnt = 100; |
| 132 emitter->SetEventStatus(KE_FILTERED); |
| 133 EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, TIMEOUT_IMMEDIATE, &event_cnt)); |
| 134 EXPECT_EQ(0, event_cnt); |
| 135 |
| 136 // Return with one event if the Emitter has the expected signal set. |
| 137 memset(ev, 0, sizeof(ev)); |
| 138 event_cnt = 100; |
| 139 emitter->SetEventStatus(KE_EXPECTED); |
| 140 EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, TIMEOUT_IMMEDIATE, &event_cnt)); |
| 141 EXPECT_EQ(1, event_cnt); |
| 142 EXPECT_EQ(USER_DATA_A, ev[0].user_data); |
| 143 EXPECT_EQ(KE_EXPECTED, ev[0].events); |
| 144 |
| 145 // Return with one event containing only the expected signal. |
| 146 memset(ev, 0, sizeof(ev)); |
| 147 event_cnt = 100; |
| 148 emitter->SetEventStatus(KE_EXPECTED | KE_FILTERED); |
| 149 EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, TIMEOUT_IMMEDIATE, &event_cnt)); |
| 150 EXPECT_EQ(1, event_cnt); |
| 151 EXPECT_EQ(USER_DATA_A, ev[0].user_data); |
| 152 EXPECT_EQ(KE_EXPECTED, ev[0].events); |
| 153 |
| 154 // Change the USER_DATA on an existing event |
| 155 EXPECT_EQ(0, listener->Update(ID_EMITTER, KE_EXPECTED, USER_DATA_B)); |
| 156 |
| 157 // Return with one event signaled with the alternate USER DATA |
| 158 memset(ev, 0, sizeof(ev)); |
| 159 event_cnt = 100; |
| 160 emitter->SetEventStatus(KE_EXPECTED | KE_FILTERED); |
| 161 EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, 0, &event_cnt)); |
| 162 EXPECT_EQ(1, event_cnt); |
| 163 EXPECT_EQ(USER_DATA_B, ev[0].user_data); |
| 164 EXPECT_EQ(KE_EXPECTED, ev[0].events); |
| 165 |
| 166 // Reset the USER_DATA. |
| 167 EXPECT_EQ(0, listener->Update(ID_EMITTER, KE_EXPECTED, USER_DATA_A)); |
| 168 |
| 169 // Support adding a DUP. |
| 170 EXPECT_EQ(0, listener->Track(ID_EMITTER_DUP, |
| 171 emitter, |
| 172 KE_EXPECTED, |
| 173 USER_DATA_A)); |
| 174 EXPECT_EQ(2, emitter->NumEvents()); |
| 175 |
| 176 // Return unsignaled. |
| 177 memset(ev, 0, sizeof(ev)); |
| 178 emitter->SetEventStatus(KE_NONE); |
| 179 event_cnt = 100; |
| 180 EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, TIMEOUT_IMMEDIATE, &event_cnt)); |
| 181 EXPECT_EQ(0, event_cnt); |
| 182 |
| 183 // Return with two event signaled with expected data. |
| 184 memset(ev, 0, sizeof(ev)); |
| 185 emitter->SetEventStatus(KE_EXPECTED); |
| 186 event_cnt = 100; |
| 187 EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, TIMEOUT_IMMEDIATE, &event_cnt)); |
| 188 EXPECT_EQ(2, event_cnt); |
| 189 EXPECT_EQ(USER_DATA_A, ev[0].user_data); |
| 190 EXPECT_EQ(KE_EXPECTED, ev[0].events); |
| 191 EXPECT_EQ(USER_DATA_A, ev[1].user_data); |
| 192 EXPECT_EQ(KE_EXPECTED, ev[1].events); |
| 193 } |
| 194 |
| 195 long Duration(struct timeval* start, struct timeval* end) { |
| 196 if (start->tv_usec > end->tv_usec) { |
| 197 end->tv_sec -= 1; |
| 198 end->tv_usec += 1000000; |
| 199 } |
| 200 long cur_time = 1000 * (end->tv_sec - start->tv_sec); |
| 201 cur_time += (end->tv_usec - start->tv_usec) / 1000; |
| 202 return cur_time; |
| 203 } |
| 204 |
| 205 |
| 206 // Run a timed wait, and return the average of 8 iterations to reduce |
| 207 // chance of false negative on outlier. |
| 208 const int TRIES_TO_AVERAGE = 8; |
| 209 bool TimedListen(ScopedEventListener& listen, |
| 210 EventData* ev, |
| 211 int ev_max, |
| 212 int ev_expect, |
| 213 int ms_wait, |
| 214 long* duration) { |
| 215 |
| 216 struct timeval start; |
| 217 struct timeval end; |
| 218 long total_time = 0; |
| 219 |
| 220 for (int a=0; a < TRIES_TO_AVERAGE; a++) { |
| 221 gettimeofday(&start, NULL); |
| 222 |
| 223 int signaled; |
| 224 |
| 225 EXPECT_EQ(0, listen->Wait(ev, ev_max, ms_wait, &signaled)); |
| 226 EXPECT_EQ(signaled, ev_expect); |
| 227 |
| 228 if (signaled != ev_expect) { |
| 229 return false; |
| 230 } |
| 231 |
| 232 gettimeofday(&end, NULL); |
| 233 |
| 234 long cur_time = Duration(&start, &end); |
| 235 total_time += cur_time; |
| 236 } |
| 237 |
| 238 *duration = total_time / TRIES_TO_AVERAGE; |
| 239 return true; |
| 240 } |
| 241 |
| 242 |
| 243 // NOTE: These timing tests are potentially flaky, the real test is |
| 244 // for the zero timeout should be, has the ConditionVariable been waited on? |
| 245 // Once we provide a debuggable SimpleCond and SimpleLock we can actually test |
| 246 // the correct thing. |
| 247 |
| 248 // Normal scheduling would expect us to see ~10ms accuracy, but we'll |
| 249 // use a much bigger number (yet smaller than the MAX_MS_TIMEOUT). |
| 250 const int SCHEDULING_GRANULARITY = 100; |
| 251 |
| 252 const int EXPECT_ONE_EVENT = 1; |
| 253 const int EXPECT_NO_EVENT = 0; |
| 254 |
| 255 TEST(EventTest, EmitterTimeout) { |
| 256 ScopedRef<EventEmitterTester> emitter(new EventEmitterTester()); |
| 257 ScopedEventListener listener(new EventListener()); |
| 258 long duration; |
| 259 |
| 260 EventData ev[MAX_EVENTS]; |
| 261 memset(ev, 0, sizeof(ev)); |
| 262 EXPECT_EQ(0, listener->Track(ID_EMITTER, emitter, KE_EXPECTED, USER_DATA_A)); |
| 263 |
| 264 // Return immediately when emitter is signaled, with no timeout |
| 265 emitter->SetEventStatus(KE_EXPECTED); |
| 266 memset(ev, 0, sizeof(ev)); |
| 267 EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_ONE_EVENT, |
| 268 TIMEOUT_IMMEDIATE, &duration)); |
| 269 EXPECT_EQ(USER_DATA_A, ev[0].user_data); |
| 270 EXPECT_EQ(KE_EXPECTED, ev[0].events); |
| 271 EXPECT_EQ(0, duration); |
| 272 |
| 273 // Return immediately when emitter is signaled, even with timeout |
| 274 emitter->SetEventStatus(KE_EXPECTED); |
| 275 memset(ev, 0, sizeof(ev)); |
| 276 EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_ONE_EVENT, |
| 277 TIMEOUT_LONG, &duration)); |
| 278 EXPECT_EQ(USER_DATA_A, ev[0].user_data); |
| 279 EXPECT_EQ(KE_EXPECTED, ev[0].events); |
| 280 EXPECT_GT(SCHEDULING_GRANULARITY, duration); |
| 281 |
| 282 // Return immediately if Emiiter is already signaled when blocking forever. |
| 283 emitter->SetEventStatus(KE_EXPECTED); |
| 284 memset(ev, 0, sizeof(ev)); |
| 285 EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_ONE_EVENT, |
| 286 TIMEOUT_NEVER, &duration)); |
| 287 EXPECT_EQ(USER_DATA_A, ev[0].user_data); |
| 288 EXPECT_EQ(KE_EXPECTED, ev[0].events); |
| 289 EXPECT_GT(SCHEDULING_GRANULARITY, duration); |
| 290 |
| 291 // Return immediately if Emitter is no signaled when not blocking. |
| 292 emitter->SetEventStatus(KE_NONE); |
| 293 memset(ev, 0, sizeof(ev)); |
| 294 EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_NO_EVENT, |
| 295 TIMEOUT_IMMEDIATE, &duration)); |
| 296 EXPECT_EQ(0, duration); |
| 297 |
| 298 // Wait TIMEOUT_LONG if the emitter is not in a signaled state. |
| 299 emitter->SetEventStatus(KE_NONE); |
| 300 memset(ev, 0, sizeof(ev)); |
| 301 EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_NO_EVENT, |
| 302 TIMEOUT_LONG, &duration)); |
| 303 EXPECT_LT(TIMEOUT_LONG - 1, duration); |
| 304 EXPECT_GT(TIMEOUT_LONG + SCHEDULING_GRANULARITY, duration); |
| 305 } |
| 306 |
| 307 struct SignalInfo { |
| 308 EventEmitterTester* em; |
| 309 unsigned int ms_wait; |
| 310 uint32_t events; |
| 311 }; |
| 312 |
| 313 void *SignalEmitter(void *ptr) { |
| 314 SignalInfo* info = (SignalInfo*) ptr; |
| 315 struct timespec ts; |
| 316 ts.tv_sec = 0; |
| 317 ts.tv_nsec = info->ms_wait * 1000000; |
| 318 |
| 319 nanosleep(&ts, NULL); |
| 320 |
| 321 info->em->RaiseEvent(info->events); |
| 322 return NULL; |
| 323 } |
| 324 |
| 325 TEST(EventTest, EmitterSignalling) { |
| 326 ScopedRef<EventEmitterTester> emitter(new EventEmitterTester()); |
| 327 ScopedEventListener listener(new EventListener); |
| 328 |
| 329 SignalInfo siginfo; |
| 330 struct timeval start; |
| 331 struct timeval end; |
| 332 long duration; |
| 333 |
| 334 EventData ev[MAX_EVENTS]; |
| 335 memset(ev, 0, sizeof(ev)); |
| 336 EXPECT_EQ(0, listener->Track(ID_EMITTER, emitter, KE_EXPECTED, USER_DATA_A)); |
| 337 |
| 338 // Setup another thread to wait 1/4 of the max time, and signal both |
| 339 // an expected, and unexpected value. |
| 340 siginfo.em = emitter.get(); |
| 341 siginfo.ms_wait = TIMEOUT_SHORT; |
| 342 siginfo.events = KE_EXPECTED | KE_FILTERED; |
| 343 pthread_t tid; |
| 344 pthread_create(&tid, NULL, SignalEmitter, &siginfo); |
| 345 |
| 346 // Wait for the signal from the other thread and time it. |
| 347 gettimeofday(&start, NULL); |
| 348 int cnt = 0; |
| 349 EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, TIMEOUT_VERY_LONG, &cnt)); |
| 350 EXPECT_EQ(1, cnt); |
| 351 gettimeofday(&end, NULL); |
| 352 |
| 353 // Verify the wait duration, and that we only recieved the expected signal. |
| 354 duration = Duration(&start, &end); |
| 355 EXPECT_GT(TIMEOUT_SHORT + SCHEDULING_GRANULARITY, duration); |
| 356 EXPECT_LT(TIMEOUT_SHORT - 1, duration); |
| 357 EXPECT_EQ(USER_DATA_A, ev[0].user_data); |
| 358 EXPECT_EQ(KE_EXPECTED, ev[0].events); |
| 359 } |
OLD | NEW |