| OLD | NEW |
| (Empty) |
| 1 // Copyright 2008-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 | |
| 16 | |
| 17 #include <stdlib.h> | |
| 18 #include "base/scoped_ptr.h" | |
| 19 #include "omaha/base/event_handler.h" | |
| 20 #include "omaha/base/reactor.h" | |
| 21 #include "omaha/base/scoped_any.h" | |
| 22 #include "omaha/testing/unit_test.h" | |
| 23 | |
| 24 namespace omaha { | |
| 25 | |
| 26 // TODO(omaha): rename EventHandler to EventHandlerInterface. | |
| 27 | |
| 28 // Creates and registers two waitable timers with the reactor. They go off | |
| 29 // randomly until the reactor stops handling events. | |
| 30 class ReactorTest | |
| 31 : public testing::Test, | |
| 32 public EventHandler { | |
| 33 protected: | |
| 34 ReactorTest() : cnt_(0) {} | |
| 35 | |
| 36 virtual void SetUp() { | |
| 37 // Timer handles are with auto reset for simplicity. | |
| 38 reset(timer1_, ::CreateWaitableTimer(NULL, false, NULL)); | |
| 39 reset(timer2_, ::CreateWaitableTimer(NULL, false, NULL)); | |
| 40 | |
| 41 reset(event_done_, ::CreateEvent(NULL, true, false, NULL)); | |
| 42 | |
| 43 ASSERT_TRUE(timer1_); | |
| 44 ASSERT_TRUE(timer2_); | |
| 45 ASSERT_TRUE(event_done_); | |
| 46 | |
| 47 // We only need the thread handle to queue an empty APC to it. | |
| 48 reset(main_thread_, ::OpenThread(THREAD_ALL_ACCESS, | |
| 49 false, | |
| 50 ::GetCurrentThreadId())); | |
| 51 ASSERT_TRUE(main_thread_); | |
| 52 } | |
| 53 | |
| 54 virtual void TearDown() { | |
| 55 } | |
| 56 | |
| 57 // EventHandler. | |
| 58 virtual void HandleEvent(HANDLE h); | |
| 59 | |
| 60 // Empty APC to stop the reactor. | |
| 61 static void _stdcall Stop(ULONG_PTR) {} | |
| 62 | |
| 63 // Returns an integer value in the [0, 10) range. | |
| 64 static int GetSmallInt() { | |
| 65 unsigned int val = 0; | |
| 66 rand_s(&val); | |
| 67 return val % 10; | |
| 68 } | |
| 69 | |
| 70 Reactor reactor_; | |
| 71 | |
| 72 scoped_timer timer1_; | |
| 73 scoped_timer timer2_; | |
| 74 scoped_event event_done_; | |
| 75 | |
| 76 scoped_handle main_thread_; | |
| 77 LONG cnt_; | |
| 78 static const LONG ReactorTest::kMaxCount = 10; | |
| 79 }; | |
| 80 | |
| 81 const LONG ReactorTest::kMaxCount; | |
| 82 | |
| 83 void ReactorTest::HandleEvent(HANDLE h) { | |
| 84 EXPECT_TRUE(h); | |
| 85 if (h == get(event_done_)) { | |
| 86 ASSERT_TRUE(::QueueUserAPC(&ReactorTest::Stop, | |
| 87 get(main_thread_), | |
| 88 0)); | |
| 89 } else if (h == get(timer1_) || h == get(timer2_)) { | |
| 90 // Check the handles auto reset correctly. | |
| 91 EXPECT_EQ(::WaitForSingleObject(h, 0), WAIT_TIMEOUT); | |
| 92 if (::InterlockedIncrement(&cnt_) > kMaxCount) { | |
| 93 ASSERT_TRUE(::SetEvent(get(event_done_))); | |
| 94 } else { | |
| 95 ASSERT_HRESULT_SUCCEEDED(reactor_.RegisterHandle(h)); | |
| 96 | |
| 97 unsigned int val = 0; | |
| 98 ASSERT_EQ(rand_s(&val), 0); | |
| 99 val %= 10; | |
| 100 | |
| 101 // Set the timer to fire; negative values indicate relative time. | |
| 102 LARGE_INTEGER due_time_100ns = {0}; | |
| 103 due_time_100ns.QuadPart = -(static_cast<int>(val) * 10 * 1000); | |
| 104 ASSERT_TRUE(::SetWaitableTimer(h, &due_time_100ns, 0, NULL, NULL, false)); | |
| 105 } | |
| 106 } | |
| 107 } | |
| 108 | |
| 109 // Registers the handles, primes the timers, and handles events. | |
| 110 TEST_F(ReactorTest, HandleEvents) { | |
| 111 ASSERT_HRESULT_SUCCEEDED(reactor_.RegisterHandle(get(event_done_), this, 0)); | |
| 112 ASSERT_HRESULT_SUCCEEDED(reactor_.RegisterHandle(get(timer1_), this, 0)); | |
| 113 ASSERT_HRESULT_SUCCEEDED(reactor_.RegisterHandle(get(timer2_), this, 0)); | |
| 114 | |
| 115 LARGE_INTEGER due_time_100ns = {0}; | |
| 116 ASSERT_TRUE(SetWaitableTimer(get(timer1_), | |
| 117 &due_time_100ns, | |
| 118 0, | |
| 119 NULL, | |
| 120 NULL, | |
| 121 false)); | |
| 122 ASSERT_TRUE(SetWaitableTimer(get(timer2_), | |
| 123 &due_time_100ns, | |
| 124 0, | |
| 125 NULL, | |
| 126 NULL, | |
| 127 false)); | |
| 128 | |
| 129 ASSERT_HRESULT_SUCCEEDED(reactor_.HandleEvents()); | |
| 130 | |
| 131 ASSERT_HRESULT_SUCCEEDED(reactor_.UnregisterHandle(get(timer2_))); | |
| 132 ASSERT_HRESULT_SUCCEEDED(reactor_.UnregisterHandle(get(timer1_))); | |
| 133 ASSERT_HRESULT_SUCCEEDED(reactor_.UnregisterHandle(get(event_done_))); | |
| 134 } | |
| 135 | |
| 136 } // namespace omaha | |
| OLD | NEW |