blob: 05ae7fbe38a114152df1528a85f84a7c0386e146 [file] [log] [blame]
Alex Deymoaea4c1c2015-08-19 20:24:43 -07001//
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.com49fdf182009-10-10 00:57:34 +000016
Alex Deymoaab50e32014-11-10 19:55:35 -080017#include "update_engine/action_processor.h"
18
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080019#include <string>
rspangler@google.com49fdf182009-10-10 00:57:34 +000020#include <gtest/gtest.h>
21#include "update_engine/action.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000022
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080023using std::string;
24
rspangler@google.com49fdf182009-10-10 00:57:34 +000025namespace chromeos_update_engine {
26
27using chromeos_update_engine::ActionPipe;
28
29class ActionProcessorTestAction;
30
31template<>
32class ActionTraits<ActionProcessorTestAction> {
33 public:
34 typedef string OutputObjectType;
35 typedef string InputObjectType;
36};
37
38// This is a simple Action class for testing.
Yunlian Jianga178e5e2013-04-05 14:41:56 -070039class ActionProcessorTestAction : public Action<ActionProcessorTestAction> {
40 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +000041 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 Arnoldd1c4d2d2014-06-05 14:07:53 -070049 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +000050 }
51 string Type() const { return "ActionProcessorTestAction"; }
52};
53
54class 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.
58TEST(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
74namespace {
75class MyActionProcessorDelegate : public ActionProcessorDelegate {
76 public:
77 explicit MyActionProcessorDelegate(const ActionProcessor* processor)
Andrew de los Reyes3270f742010-07-15 22:28:14 -070078 : processor_(processor),
79 processing_done_called_(false),
80 processing_stopped_called_(false),
81 action_completed_called_(false),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070082 action_exit_code_(ErrorCode::kError) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000083
Darin Petkovc1a8b422010-07-19 11:34:49 -070084 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070085 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000086 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.comc98a7ed2009-12-04 18:54:03 +000095 virtual void ActionCompleted(ActionProcessor* processor,
96 AbstractAction* action,
David Zeuthena99981f2013-04-29 13:42:47 -070097 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000098 EXPECT_EQ(processor_, processor);
99 EXPECT_FALSE(action_completed_called_);
100 action_completed_called_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -0700101 action_exit_code_ = code;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000102 }
103
104 const ActionProcessor* processor_;
105 bool processing_done_called_;
106 bool processing_stopped_called_;
107 bool action_completed_called_;
David Zeuthena99981f2013-04-29 13:42:47 -0700108 ErrorCode action_exit_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +0000109};
Alex Vakulenkod2779df2014-06-16 13:19:00 -0700110} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +0000111
112TEST(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 Vakulenko88b591f2014-08-28 16:48:57 -0700121 action_processor.set_delegate(nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000122 EXPECT_TRUE(delegate.processing_done_called_);
123 EXPECT_TRUE(delegate.action_completed_called_);
124}
125
126TEST(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 Vakulenko88b591f2014-08-28 16:48:57 -0700135 action_processor.set_delegate(nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000136 EXPECT_TRUE(delegate.processing_stopped_called_);
137 EXPECT_FALSE(delegate.action_completed_called_);
138 EXPECT_FALSE(action_processor.IsRunning());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700139 EXPECT_EQ(nullptr, action_processor.current_action());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000140}
141
142TEST(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 Vakulenko88b591f2014-08-28 16:48:57 -0700154 EXPECT_EQ(nullptr, action_processor.current_action());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000155 EXPECT_FALSE(action_processor.IsRunning());
156}
157
158TEST(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 Vakulenko88b591f2014-08-28 16:48:57 -0700166 EXPECT_EQ(nullptr, action1.processor());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000167 EXPECT_FALSE(action1.IsRunning());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700168 EXPECT_EQ(nullptr, action2.processor());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000169 EXPECT_FALSE(action2.IsRunning());
170}
171
172TEST(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 Vakulenko88b591f2014-08-28 16:48:57 -0700187 action_processor.set_delegate(nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000188}
189
190} // namespace chromeos_update_engine