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