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