Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium OS 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 | #include "shill/hook_table.h" |
| 6 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 7 | #include <string> |
| 8 | |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 9 | #include <base/bind.h> |
| 10 | #include <base/callback.h> |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 11 | #include <base/message_loop.h> |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 12 | #include <gmock/gmock.h> |
| 13 | #include <gtest/gtest.h> |
| 14 | |
| 15 | #include "shill/error.h" |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 16 | #include "shill/event_dispatcher.h" |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 17 | |
| 18 | using base::Callback; |
| 19 | using base::Closure; |
| 20 | using base::Bind; |
| 21 | using base::Unretained; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 22 | using std::string; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 23 | using ::testing::_; |
| 24 | using ::testing::InSequence; |
| 25 | using ::testing::Return; |
| 26 | using ::testing::SaveArg; |
| 27 | |
| 28 | namespace shill { |
| 29 | |
| 30 | class HookTableTest : public testing::Test { |
| 31 | public: |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 32 | static const char kName[]; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 33 | MOCK_METHOD0(StartAction, void()); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 34 | MOCK_METHOD0(StartAction2, void()); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 35 | MOCK_METHOD1(DoneAction, void(const Error &)); |
| 36 | |
| 37 | protected: |
| 38 | HookTableTest() |
| 39 | : hook_table_(&event_dispatcher_) {} |
| 40 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 41 | EventDispatcher event_dispatcher_; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 42 | HookTable hook_table_; |
| 43 | }; |
| 44 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 45 | const char HookTableTest::kName[] = "test"; |
| 46 | |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 47 | MATCHER(IsSuccess, "") { |
| 48 | return arg.IsSuccess(); |
| 49 | } |
| 50 | |
| 51 | MATCHER(IsFailure, "") { |
| 52 | return arg.IsFailure(); |
| 53 | } |
| 54 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 55 | TEST_F(HookTableTest, ActionCompletes) { |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 56 | EXPECT_CALL(*this, StartAction()); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 57 | EXPECT_CALL(*this, DoneAction(IsSuccess())); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 58 | Closure start_cb = Bind(&HookTableTest::StartAction, Unretained(this)); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 59 | Callback<void(const Error &)> done_cb = Bind(&HookTableTest::DoneAction, |
| 60 | Unretained(this)); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 61 | hook_table_.Add(kName, start_cb); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 62 | hook_table_.Run(0, done_cb); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 63 | hook_table_.ActionComplete(kName); |
| 64 | |
| 65 | // Ensure that the timeout callback got cancelled. If it did not get |
| 66 | // cancelled, done_cb will be run twice and make this test fail. |
| 67 | event_dispatcher_.DispatchPendingEvents(); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 70 | ACTION_P2(CompleteAction, hook_table, name) { |
| 71 | hook_table->ActionComplete(name); |
| 72 | } |
| 73 | |
| 74 | TEST_F(HookTableTest, ActionCompletesInline) { |
| 75 | // StartAction completes immediately before HookTable::Run() returns. |
| 76 | EXPECT_CALL(*this, StartAction()) |
| 77 | .WillOnce(CompleteAction(&hook_table_, kName)); |
| 78 | EXPECT_CALL(*this, DoneAction(IsSuccess())); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 79 | Closure start_cb = Bind(&HookTableTest::StartAction, Unretained(this)); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 80 | Callback<void(const Error &)> done_cb = Bind(&HookTableTest::DoneAction, |
| 81 | Unretained(this)); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 82 | hook_table_.Add(kName, start_cb); |
| 83 | hook_table_.Run(0, done_cb); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 84 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 85 | // Ensure that the timeout callback got cancelled. If it did not get |
| 86 | // cancelled, done_cb will be run twice and make this test fail. |
| 87 | event_dispatcher_.DispatchPendingEvents(); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | TEST_F(HookTableTest, ActionTimesOut) { |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 91 | const int kTimeout = 1; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 92 | EXPECT_CALL(*this, StartAction()); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 93 | EXPECT_CALL(*this, DoneAction(IsFailure())); |
| 94 | |
| 95 | Closure start_cb = Bind(&HookTableTest::StartAction, Unretained(this)); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 96 | Callback<void(const Error &)> done_cb = Bind(&HookTableTest::DoneAction, |
| 97 | Unretained(this)); |
| 98 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 99 | hook_table_.Add(kName, start_cb); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 100 | hook_table_.Run(kTimeout, done_cb); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 101 | |
| 102 | // Cause the event dispatcher to exit after kTimeout + 1 ms. |
| 103 | event_dispatcher_.PostDelayedTask(MessageLoop::QuitClosure(), |
| 104 | kTimeout * + 1); |
| 105 | event_dispatcher_.DispatchForever(); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | TEST_F(HookTableTest, MultipleActionsAllSucceed) { |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 109 | const string kName1 = "test1"; |
| 110 | const string kName2 = "test2"; |
| 111 | const string kName3 = "test3"; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 112 | Closure pending_cb; |
| 113 | const int kTimeout = 10; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 114 | EXPECT_CALL(*this, StartAction()).Times(2); |
| 115 | |
| 116 | // StartAction2 completes immediately before HookTable::Run() returns. |
| 117 | EXPECT_CALL(*this, StartAction2()) |
| 118 | .WillOnce(CompleteAction(&hook_table_, kName1)); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 119 | EXPECT_CALL(*this, DoneAction(IsSuccess())); |
| 120 | |
| 121 | Closure start_cb = Bind(&HookTableTest::StartAction, Unretained(this)); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 122 | Closure start2_cb = Bind(&HookTableTest::StartAction2, Unretained(this)); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 123 | Callback<void(const Error &)> done_cb = Bind(&HookTableTest::DoneAction, |
| 124 | Unretained(this)); |
| 125 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 126 | hook_table_.Add(kName1, start2_cb); |
| 127 | hook_table_.Add(kName2, start_cb); |
| 128 | hook_table_.Add(kName3, start_cb); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 129 | hook_table_.Run(kTimeout, done_cb); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 130 | hook_table_.ActionComplete(kName2); |
| 131 | hook_table_.ActionComplete(kName3); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | TEST_F(HookTableTest, MultipleActionsAndOneTimesOut) { |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 135 | const string kName1 = "test1"; |
| 136 | const string kName2 = "test2"; |
| 137 | const string kName3 = "test3"; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 138 | Closure pending_cb; |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 139 | const int kTimeout = 1; |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 140 | EXPECT_CALL(*this, StartAction()).Times(3); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 141 | EXPECT_CALL(*this, DoneAction(IsFailure())); |
| 142 | |
| 143 | Closure start_cb = Bind(&HookTableTest::StartAction, Unretained(this)); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 144 | Callback<void(const Error &)> done_cb = Bind(&HookTableTest::DoneAction, |
| 145 | Unretained(this)); |
| 146 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 147 | hook_table_.Add(kName1, start_cb); |
| 148 | hook_table_.Add(kName2, start_cb); |
| 149 | hook_table_.Add(kName3, start_cb); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 150 | hook_table_.Run(kTimeout, done_cb); |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 151 | hook_table_.ActionComplete(kName1); |
| 152 | hook_table_.ActionComplete(kName3); |
| 153 | // Cause the event dispatcher to exit after kTimeout + 1 ms. |
| 154 | event_dispatcher_.PostDelayedTask(MessageLoop::QuitClosure(), |
| 155 | kTimeout + 1); |
| 156 | event_dispatcher_.DispatchForever(); |
| 157 | } |
| 158 | |
| 159 | TEST_F(HookTableTest, AddActionsWithSameName) { |
| 160 | EXPECT_CALL(*this, StartAction()).Times(0); |
| 161 | EXPECT_CALL(*this, StartAction2()); |
| 162 | EXPECT_CALL(*this, DoneAction(IsSuccess())); |
| 163 | Closure start_cb = Bind(&HookTableTest::StartAction, Unretained(this)); |
| 164 | Closure start2_cb = Bind(&HookTableTest::StartAction2, Unretained(this)); |
| 165 | Callback<void(const Error &)> done_cb = Bind(&HookTableTest::DoneAction, |
| 166 | Unretained(this)); |
| 167 | hook_table_.Add(kName, start_cb); |
| 168 | |
| 169 | // Adding an action with the same name kName. New callbacks should replace |
| 170 | // old ones. |
| 171 | hook_table_.Add(kName, start2_cb); |
| 172 | hook_table_.Run(0, done_cb); |
| 173 | hook_table_.ActionComplete(kName); |
| 174 | |
| 175 | // Ensure that the timeout callback got cancelled. If it did not get |
| 176 | // cancelled, done_cb will be run twice and make this test fail. |
| 177 | event_dispatcher_.DispatchPendingEvents(); |
| 178 | } |
| 179 | |
| 180 | TEST_F(HookTableTest, RemoveAction) { |
| 181 | EXPECT_CALL(*this, StartAction()).Times(0); |
| 182 | EXPECT_CALL(*this, DoneAction(IsSuccess())); |
| 183 | Closure start_cb = Bind(&HookTableTest::StartAction, Unretained(this)); |
| 184 | Callback<void(const Error &)> done_cb = Bind(&HookTableTest::DoneAction, |
| 185 | Unretained(this)); |
| 186 | hook_table_.Add(kName, start_cb); |
| 187 | hook_table_.Remove(kName); |
| 188 | hook_table_.Run(0, done_cb); |
| 189 | } |
| 190 | |
| 191 | TEST_F(HookTableTest, ActionCompleteFollowedByRemove) { |
| 192 | EXPECT_CALL(*this, StartAction()).Times(0); |
| 193 | Closure start_cb = Bind(&HookTableTest::StartAction, Unretained(this)); |
| 194 | hook_table_.Add(kName, start_cb); |
| 195 | hook_table_.ActionComplete(kName); |
| 196 | hook_table_.Remove(kName); |
| 197 | } |
| 198 | |
| 199 | class SomeClass : public base::RefCounted<SomeClass> { |
| 200 | public: |
| 201 | SomeClass() {} |
| 202 | void StartAction() {} |
| 203 | |
| 204 | private: |
| 205 | DISALLOW_COPY_AND_ASSIGN(SomeClass); |
| 206 | }; |
| 207 | |
| 208 | // This test verifies that a class that removes itself from a hook table upon |
| 209 | // destruction does not crash if the hook table is destroyed first. |
| 210 | TEST_F(HookTableTest, RefcountedObject) { |
| 211 | scoped_ptr<HookTable> ht(new HookTable(&event_dispatcher_)); |
| 212 | { |
| 213 | scoped_refptr<SomeClass> ref_counted_object = new SomeClass(); |
| 214 | Closure start_cb = Bind(&SomeClass::StartAction, ref_counted_object); |
| 215 | ht->Add(kName, start_cb); |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 216 | } |
| 217 | } |
| 218 | |
Gary Morain | a9fb325 | 2012-05-31 12:05:31 -0700 | [diff] [blame^] | 219 | TEST_F(HookTableTest, ActionAddedBeforePreviousActionCompletes) { |
| 220 | EXPECT_CALL(*this, StartAction()); |
| 221 | EXPECT_CALL(*this, StartAction2()).Times(0); |
| 222 | EXPECT_CALL(*this, DoneAction(IsSuccess())); |
| 223 | Closure start_cb = Bind(&HookTableTest::StartAction, Unretained(this)); |
| 224 | Closure start2_cb = Bind(&HookTableTest::StartAction2, Unretained(this)); |
| 225 | Callback<void(const Error &)> done_cb = Bind(&HookTableTest::DoneAction, |
| 226 | Unretained(this)); |
| 227 | hook_table_.Add(kName, start_cb); |
| 228 | hook_table_.Run(0, done_cb); |
| 229 | |
| 230 | // An action with the same name is added before the previous actions complete. |
| 231 | // It should not be run. |
| 232 | hook_table_.Add(kName, start2_cb); |
| 233 | hook_table_.ActionComplete(kName); |
| 234 | } |
| 235 | |
| 236 | |
Gary Morain | f80ef06 | 2012-05-16 14:57:04 -0700 | [diff] [blame] | 237 | } // namespace shill |