Index: native_client_sdk/src/libraries/nacl_io_test/event_test.cc |
diff --git a/native_client_sdk/src/libraries/nacl_io_test/event_test.cc b/native_client_sdk/src/libraries/nacl_io_test/event_test.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..b54614ada49f8320cee07ba44ae08dcb8dbcc5cc |
--- /dev/null |
+++ b/native_client_sdk/src/libraries/nacl_io_test/event_test.cc |
@@ -0,0 +1,311 @@ |
+/* Copyright (c) 2013 The Chromium Authors. All rights reserved. |
+ * Use of this source code is governed by a BSD-style license that can be |
+ * found in the LICENSE file. |
+ */ |
Sam Clegg
2013/07/17 17:34:56
C++ comment
Sam Clegg
2013/07/17 17:34:56
C++ comment
|
+ |
+#include <errno.h> |
+#include <fcntl.h> |
+#include <sys/stat.h> |
+#include <sys/time.h> |
+ |
+#include "gtest/gtest.h" |
+ |
+#include "nacl_io/event_emitter.h" |
+#include "nacl_io/event_listener.h" |
+ |
+ |
+class EventEmitterTester : public EventEmitter { |
+ public: |
+ EventEmitterTester() : event_status_(0), event_cnt_(0) {} |
+ |
+ void SetEventStatus(uint32_t bits) { event_status_ = bits; } |
+ uint32_t GetEventStatus() { return event_status_; } |
+ |
+ int GetType() { return S_IFSOCK; } |
+ |
+ int NumEvents() { return event_cnt_; } |
+ |
+ public: |
+ // Make this function public for testing |
+ 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
|
+ EventEmitter::RaiseEvent(events); |
+ } |
+ |
+ // Called after registering locally, but while lock is still held. |
+ void ChainRegister(const ScopedEventInfo& event) { |
+ event_cnt_++; |
+ } |
+ |
+ // Called before unregistering locally, but while lock is still held. |
+ void ChainUnregister(const ScopedEventInfo& event) { |
+ event_cnt_--; |
+ } |
+ |
+ protected: |
+ uint32_t event_status_; |
+ uint32_t event_cnt_; |
+}; |
+ |
+ |
+const int MAX_EVENTS = 8; |
+const int FD_EMITTER = 5; |
+const int DUP_EMITTER = 6; |
+ |
+const uint32_t SIG_FILTER = 2; |
+const uint64_t SIG_DATA = 1; |
+ |
+TEST(EventTest, EmitterBasic) { |
+ ScopedRef<EventEmitterTester> emitter(new EventEmitterTester()); |
+ ScopedRef<EventEmitter> null_emitter; |
+ ScopedEventInfo null_info; |
+ |
+ ScopedEventListener listener(new EventListener); |
+ |
+ // Verify construction |
+ EXPECT_EQ(0, emitter->NumEvents()); |
+ EXPECT_EQ(0, emitter->GetEventStatus()); |
+ |
+ // Verify status |
+ emitter->SetEventStatus(1); |
+ EXPECT_EQ(1, emitter->GetEventStatus()); |
+ |
+ // Fail to update an FD not in the set |
+ EXPECT_EQ(ENOENT, listener->Update(FD_EMITTER, SIG_FILTER, SIG_DATA)); |
+ EXPECT_EQ(ENOENT, listener->Free(FD_EMITTER)); |
+ |
+ // Set the emitter filter and data |
+ EXPECT_EQ(0, listener->Track(FD_EMITTER, emitter, SIG_FILTER, SIG_DATA)); |
+ EXPECT_EQ(1, emitter->NumEvents()); |
+ |
+ // Update an existing FD |
+ 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.
|
+ |
+ // Fail to add the same FD |
+ EXPECT_EQ(EEXIST, listener->Track(FD_EMITTER, emitter, SIG_FILTER, SIG_DATA)); |
+ EXPECT_EQ(1, emitter->NumEvents()); |
+ |
+ int event_cnt = 0; |
+ EventData ev[MAX_EVENTS]; |
+ |
+ // Do not allow a wait with no events |
+ 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
|
+ |
+ // Do not allow a wait with no events |
+ EXPECT_EQ(EINVAL, listener->Wait(ev, -1, 0, &event_cnt)); |
+ |
+ // Do not allow a wait with a NULL EventData pointer |
+ EXPECT_EQ(EFAULT, listener->Wait(NULL, MAX_EVENTS, 0, &event_cnt)); |
+ |
+ // Return unsignaled |
+ memset(ev, 0, sizeof(ev)); |
+ event_cnt = 100; |
+ emitter->SetEventStatus(0); |
+ EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, 0, &event_cnt)); |
+ EXPECT_EQ(0, event_cnt); |
+ |
+ // Return with one event signaled |
+ event_cnt = 100; |
+ emitter->SetEventStatus(3); |
binji
2013/07/17 22:23:20
SIG_FILTER?
Though maybe an explicit test that ev
|
+ EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, 0, &event_cnt)); |
+ EXPECT_EQ(1, event_cnt); |
+ EXPECT_EQ(SIG_DATA, ev[0].data); |
+ EXPECT_EQ(SIG_FILTER, ev[0].events); |
+ |
+ // Support adding a DUP |
+ EXPECT_EQ(0, listener->Track(DUP_EMITTER, emitter, SIG_FILTER, SIG_DATA)); |
+ EXPECT_EQ(2, emitter->NumEvents()); |
+ |
+ // Return unsignaled |
+ memset(ev, 0, sizeof(ev)); |
+ emitter->SetEventStatus(0); |
+ event_cnt = 100; |
+ EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, 0, &event_cnt)); |
+ EXPECT_EQ(0, event_cnt); |
+ |
+ // Return with two event signaled |
+ emitter->SetEventStatus(3); |
+ event_cnt = 100; |
+ EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, 0, &event_cnt)); |
+ EXPECT_EQ(2, event_cnt); |
+ EXPECT_EQ(SIG_DATA, ev[0].data); |
+ EXPECT_EQ(SIG_FILTER, ev[0].events); |
+ EXPECT_EQ(SIG_DATA, ev[1].data); |
+ 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:
|
+} |
+ |
+long Duration(struct timeval* start, struct timeval* end) { |
+ if (start->tv_usec > end->tv_usec) { |
+ end->tv_sec -= 1; |
+ end->tv_usec += 1000000; |
+ } |
+ long cur_time = 1000 * (end->tv_sec - start->tv_sec); |
+ cur_time += (end->tv_usec - start->tv_usec) / 1000; |
+ return cur_time; |
+} |
+ |
+ |
+// Run a timed wait, and return the average of 8 iterations to reduce |
+// chance of false negative on outlier. |
+const int TRIES_TO_AVERAGE = 8; |
+bool TimedListen(ScopedEventListener& listen, |
+ EventData* ev, |
+ int ev_max, |
+ int ev_expect, |
+ int ms_wait, |
+ long* duration) { |
+ |
+ struct timeval start; |
+ struct timeval end; |
+ long total_time = 0; |
+ |
+ for (int a=0; a < TRIES_TO_AVERAGE; a++) { |
+ gettimeofday(&start, NULL); |
+ |
+ int signaled; |
+ |
+ EXPECT_EQ(0, listen->Wait(ev, ev_max, ms_wait, &signaled)); |
+ EXPECT_EQ(signaled, ev_expect); |
+ |
+ if (signaled != ev_expect) { |
+ return false; |
+ } |
+ |
+ gettimeofday(&end, NULL); |
+ |
+ long cur_time = Duration(&start, &end); |
+ total_time += cur_time; |
+ } |
+ |
+ *duration = total_time / TRIES_TO_AVERAGE; |
+ return true; |
+} |
+ |
+ |
+// 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.
|
+// for the zero timeout should be, has the ConditionVariable been waited on? |
+// Once we provide a debuggable SimpleCond and SimpleLock we can actually test |
+// the correct thing. |
+ |
+// MAX_MS_TIMEOUT |
+const int NO_MS_TIMEOUT = 0; |
+ |
+// Wait at most .5 seconds |
+const int MAX_MS_TIMEOUT = 500; |
+ |
+// Wait forever |
+const int NEVER_TIMEOUT = -1; |
+ |
+// However, normal schedule would expect us to see < 10ms, but we'll |
+// use a much bigger number (yet smaller than the MAX_MS_TIMEOUT). |
+const int MAX_MS_EXPECTED = MAX_MS_TIMEOUT / 2; |
+ |
+const int EXPECT_ONE_EVENT = 1; |
+const int EXPECT_NO_EVENT = 0; |
+ |
+TEST(EventTest, EmitterTimeout) { |
+ ScopedRef<EventEmitterTester> emitter(new EventEmitterTester()); |
+ ScopedEventListener listener(new EventListener()); |
+ long duration; |
+ |
+ EventData ev[MAX_EVENTS]; |
+ memset(ev, 0, sizeof(ev)); |
+ EXPECT_EQ(0, listener->Track(FD_EMITTER, emitter, SIG_FILTER, SIG_DATA)); |
+ |
+ // Return immediately when emitter is signaled, with no timeout |
+ emitter->SetEventStatus(SIG_FILTER); |
+ memset(ev, 0, sizeof(ev)); |
+ EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_ONE_EVENT, |
+ NO_MS_TIMEOUT, &duration)); |
+ EXPECT_EQ(SIG_DATA, ev[0].data); |
+ EXPECT_EQ(SIG_FILTER, ev[0].events); |
+ EXPECT_EQ(0, duration); |
+ |
+ // Return immediately when emitter is signaled, even with timeout |
+ emitter->SetEventStatus(SIG_FILTER); |
+ memset(ev, 0, sizeof(ev)); |
+ EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_ONE_EVENT, |
+ MAX_MS_TIMEOUT, &duration)); |
+ EXPECT_EQ(SIG_DATA, ev[0].data); |
+ EXPECT_EQ(SIG_FILTER, ev[0].events); |
+ EXPECT_GT(25, duration); |
binji
2013/07/17 22:23:20
const for 25 too?
noelallen1
2013/07/18 22:18:16
Done.
|
+ |
+ // Return immediately when emitter is signaled, when block forever |
+ emitter->SetEventStatus(SIG_FILTER); |
+ memset(ev, 0, sizeof(ev)); |
+ EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_ONE_EVENT, |
+ NEVER_TIMEOUT, &duration)); |
+ EXPECT_EQ(SIG_DATA, ev[0].data); |
+ EXPECT_EQ(SIG_FILTER, ev[0].events); |
+ EXPECT_GT(25, duration); |
+ |
+ // Timeout immediately when emitter is not signaled |
+ emitter->SetEventStatus(0); |
+ memset(ev, 0, sizeof(ev)); |
+ EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_NO_EVENT, |
+ NO_MS_TIMEOUT, &duration)); |
+ EXPECT_EQ(0, ev[0].data); |
+ EXPECT_EQ(0, ev[0].events); |
+ EXPECT_EQ(0, duration); |
+ |
+ // Return after timeout when emitter not signaled |
+ emitter->SetEventStatus(0); |
+ memset(ev, 0, sizeof(ev)); |
+ EXPECT_TRUE(TimedListen(listener, ev, MAX_EVENTS, EXPECT_NO_EVENT, |
+ MAX_MS_TIMEOUT, &duration)); |
+ EXPECT_EQ(0, ev[0].data); |
+ EXPECT_EQ(0, ev[0].events); |
+ EXPECT_LT(MAX_MS_TIMEOUT - 1, duration); |
+ 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.
|
+} |
+ |
+struct SignalInfo { |
+ EventEmitterTester* em; |
+ unsigned int ms_wait; |
+ uint32_t events; |
+}; |
+ |
+void *SignalEmitter(void *ptr) { |
+ SignalInfo* info = (SignalInfo*) ptr; |
+ struct timespec ts; |
+ ts.tv_sec = 0; |
+ ts.tv_nsec = info->ms_wait * 1000000; |
+ |
+ nanosleep(&ts, NULL); |
+ |
+ info->em->RaiseEvent(info->events); |
+ return NULL; |
+} |
+ |
+const int VERY_LONG_WAIT = 1000; |
+TEST(EventTest, EmitterSignalling) { |
+ ScopedRef<EventEmitterTester> emitter(new EventEmitterTester()); |
+ ScopedEventListener listener(new EventListener); |
+ |
+ SignalInfo siginfo; |
+ struct timeval start; |
+ struct timeval end; |
+ long duration; |
+ |
+ EventData ev[MAX_EVENTS]; |
+ memset(ev, 0, sizeof(ev)); |
+ EXPECT_EQ(0, listener->Track(FD_EMITTER, emitter, SIG_FILTER, SIG_DATA)); |
+ |
+ siginfo.em = emitter.get(); |
+ siginfo.ms_wait = VERY_LONG_WAIT / 4; |
+ siginfo.events = SIG_FILTER; |
+ |
+ pthread_t tid; |
+ pthread_create(&tid, NULL, SignalEmitter, &siginfo); |
+ |
+ gettimeofday(&start, NULL); |
+ int cnt = 0; |
+ EXPECT_EQ(0, listener->Wait(ev, MAX_EVENTS, VERY_LONG_WAIT, &cnt)); |
+ EXPECT_EQ(1, cnt); |
+ gettimeofday(&end, NULL); |
+ |
+ duration = Duration(&start, &end); |
+ EXPECT_GT(VERY_LONG_WAIT, duration); |
+ EXPECT_LT((VERY_LONG_WAIT / 4) - 1, duration); |
+ EXPECT_EQ(SIG_DATA, ev[0].data); |
+ EXPECT_EQ(SIG_FILTER, ev[0].events); |
+} |