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