Alex Deymo | aea4c1c | 2015-08-19 20:24:43 -0700 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2009 The Android Open Source Project |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | // |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 16 | |
Alex Deymo | aab50e3 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 17 | #include "update_engine/action_processor.h" |
| 18 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 19 | #include <string> |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 20 | #include <gtest/gtest.h> |
| 21 | #include "update_engine/action.h" |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 22 | |
Andrew de los Reyes | 4fe15d0 | 2009-12-10 19:01:36 -0800 | [diff] [blame] | 23 | using std::string; |
| 24 | |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 25 | namespace chromeos_update_engine { |
| 26 | |
| 27 | using chromeos_update_engine::ActionPipe; |
| 28 | |
| 29 | class ActionProcessorTestAction; |
| 30 | |
| 31 | template<> |
| 32 | class ActionTraits<ActionProcessorTestAction> { |
| 33 | public: |
| 34 | typedef string OutputObjectType; |
| 35 | typedef string InputObjectType; |
| 36 | }; |
| 37 | |
| 38 | // This is a simple Action class for testing. |
Yunlian Jiang | a178e5e | 2013-04-05 14:41:56 -0700 | [diff] [blame] | 39 | class ActionProcessorTestAction : public Action<ActionProcessorTestAction> { |
| 40 | public: |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 41 | typedef string InputObjectType; |
| 42 | typedef string OutputObjectType; |
| 43 | ActionPipe<string>* in_pipe() { return in_pipe_.get(); } |
| 44 | ActionPipe<string>* out_pipe() { return out_pipe_.get(); } |
| 45 | ActionProcessor* processor() { return processor_; } |
| 46 | void PerformAction() {} |
| 47 | void CompleteAction() { |
| 48 | ASSERT_TRUE(processor()); |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 49 | processor()->ActionComplete(this, ErrorCode::kSuccess); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 50 | } |
| 51 | string Type() const { return "ActionProcessorTestAction"; } |
| 52 | }; |
| 53 | |
| 54 | class ActionProcessorTest : public ::testing::Test { }; |
| 55 | |
| 56 | // This test creates two simple Actions and sends a message via an ActionPipe |
| 57 | // from one to the other. |
| 58 | TEST(ActionProcessorTest, SimpleTest) { |
| 59 | ActionProcessorTestAction action; |
| 60 | ActionProcessor action_processor; |
| 61 | EXPECT_FALSE(action_processor.IsRunning()); |
| 62 | action_processor.EnqueueAction(&action); |
| 63 | EXPECT_FALSE(action_processor.IsRunning()); |
| 64 | EXPECT_FALSE(action.IsRunning()); |
| 65 | action_processor.StartProcessing(); |
| 66 | EXPECT_TRUE(action_processor.IsRunning()); |
| 67 | EXPECT_TRUE(action.IsRunning()); |
| 68 | EXPECT_EQ(action_processor.current_action(), &action); |
| 69 | action.CompleteAction(); |
| 70 | EXPECT_FALSE(action_processor.IsRunning()); |
| 71 | EXPECT_FALSE(action.IsRunning()); |
| 72 | } |
| 73 | |
| 74 | namespace { |
| 75 | class MyActionProcessorDelegate : public ActionProcessorDelegate { |
| 76 | public: |
| 77 | explicit MyActionProcessorDelegate(const ActionProcessor* processor) |
Andrew de los Reyes | 3270f74 | 2010-07-15 22:28:14 -0700 | [diff] [blame] | 78 | : processor_(processor), |
| 79 | processing_done_called_(false), |
| 80 | processing_stopped_called_(false), |
| 81 | action_completed_called_(false), |
Gilad Arnold | d1c4d2d | 2014-06-05 14:07:53 -0700 | [diff] [blame] | 82 | action_exit_code_(ErrorCode::kError) {} |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 83 | |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 84 | virtual void ProcessingDone(const ActionProcessor* processor, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 85 | ErrorCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 86 | EXPECT_EQ(processor_, processor); |
| 87 | EXPECT_FALSE(processing_done_called_); |
| 88 | processing_done_called_ = true; |
| 89 | } |
| 90 | virtual void ProcessingStopped(const ActionProcessor* processor) { |
| 91 | EXPECT_EQ(processor_, processor); |
| 92 | EXPECT_FALSE(processing_stopped_called_); |
| 93 | processing_stopped_called_ = true; |
| 94 | } |
adlr@google.com | c98a7ed | 2009-12-04 18:54:03 +0000 | [diff] [blame] | 95 | virtual void ActionCompleted(ActionProcessor* processor, |
| 96 | AbstractAction* action, |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 97 | ErrorCode code) { |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 98 | EXPECT_EQ(processor_, processor); |
| 99 | EXPECT_FALSE(action_completed_called_); |
| 100 | action_completed_called_ = true; |
Darin Petkov | c1a8b42 | 2010-07-19 11:34:49 -0700 | [diff] [blame] | 101 | action_exit_code_ = code; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | const ActionProcessor* processor_; |
| 105 | bool processing_done_called_; |
| 106 | bool processing_stopped_called_; |
| 107 | bool action_completed_called_; |
David Zeuthen | a99981f | 2013-04-29 13:42:47 -0700 | [diff] [blame] | 108 | ErrorCode action_exit_code_; |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 109 | }; |
Alex Vakulenko | d2779df | 2014-06-16 13:19:00 -0700 | [diff] [blame] | 110 | } // namespace |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 111 | |
| 112 | TEST(ActionProcessorTest, DelegateTest) { |
| 113 | ActionProcessorTestAction action; |
| 114 | ActionProcessor action_processor; |
| 115 | MyActionProcessorDelegate delegate(&action_processor); |
| 116 | action_processor.set_delegate(&delegate); |
| 117 | |
| 118 | action_processor.EnqueueAction(&action); |
| 119 | action_processor.StartProcessing(); |
| 120 | action.CompleteAction(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 121 | action_processor.set_delegate(nullptr); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 122 | EXPECT_TRUE(delegate.processing_done_called_); |
| 123 | EXPECT_TRUE(delegate.action_completed_called_); |
| 124 | } |
| 125 | |
| 126 | TEST(ActionProcessorTest, StopProcessingTest) { |
| 127 | ActionProcessorTestAction action; |
| 128 | ActionProcessor action_processor; |
| 129 | MyActionProcessorDelegate delegate(&action_processor); |
| 130 | action_processor.set_delegate(&delegate); |
| 131 | |
| 132 | action_processor.EnqueueAction(&action); |
| 133 | action_processor.StartProcessing(); |
| 134 | action_processor.StopProcessing(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 135 | action_processor.set_delegate(nullptr); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 136 | EXPECT_TRUE(delegate.processing_stopped_called_); |
| 137 | EXPECT_FALSE(delegate.action_completed_called_); |
| 138 | EXPECT_FALSE(action_processor.IsRunning()); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 139 | EXPECT_EQ(nullptr, action_processor.current_action()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | TEST(ActionProcessorTest, ChainActionsTest) { |
| 143 | ActionProcessorTestAction action1, action2; |
| 144 | ActionProcessor action_processor; |
| 145 | action_processor.EnqueueAction(&action1); |
| 146 | action_processor.EnqueueAction(&action2); |
| 147 | action_processor.StartProcessing(); |
| 148 | EXPECT_EQ(&action1, action_processor.current_action()); |
| 149 | EXPECT_TRUE(action_processor.IsRunning()); |
| 150 | action1.CompleteAction(); |
| 151 | EXPECT_EQ(&action2, action_processor.current_action()); |
| 152 | EXPECT_TRUE(action_processor.IsRunning()); |
| 153 | action2.CompleteAction(); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 154 | EXPECT_EQ(nullptr, action_processor.current_action()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 155 | EXPECT_FALSE(action_processor.IsRunning()); |
| 156 | } |
| 157 | |
| 158 | TEST(ActionProcessorTest, DtorTest) { |
| 159 | ActionProcessorTestAction action1, action2; |
| 160 | { |
| 161 | ActionProcessor action_processor; |
| 162 | action_processor.EnqueueAction(&action1); |
| 163 | action_processor.EnqueueAction(&action2); |
| 164 | action_processor.StartProcessing(); |
| 165 | } |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 166 | EXPECT_EQ(nullptr, action1.processor()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 167 | EXPECT_FALSE(action1.IsRunning()); |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 168 | EXPECT_EQ(nullptr, action2.processor()); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 169 | EXPECT_FALSE(action2.IsRunning()); |
| 170 | } |
| 171 | |
| 172 | TEST(ActionProcessorTest, DefaultDelegateTest) { |
| 173 | // Just make sure it doesn't crash |
| 174 | ActionProcessorTestAction action; |
| 175 | ActionProcessor action_processor; |
| 176 | ActionProcessorDelegate delegate; |
| 177 | action_processor.set_delegate(&delegate); |
| 178 | |
| 179 | action_processor.EnqueueAction(&action); |
| 180 | action_processor.StartProcessing(); |
| 181 | action.CompleteAction(); |
| 182 | |
| 183 | action_processor.EnqueueAction(&action); |
| 184 | action_processor.StartProcessing(); |
| 185 | action_processor.StopProcessing(); |
| 186 | |
Alex Vakulenko | 88b591f | 2014-08-28 16:48:57 -0700 | [diff] [blame] | 187 | action_processor.set_delegate(nullptr); |
rspangler@google.com | 49fdf18 | 2009-10-10 00:57:34 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | } // namespace chromeos_update_engine |