blob: 64e847d94f0c36c6320195e0aa104e4741e2b311 [file] [log] [blame]
rspangler@google.com49fdf182009-10-10 00:57:34 +00001// 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 Deymoaab50e32014-11-10 19:55:35 -08005#include "update_engine/action_processor.h"
6
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -08007#include <string>
rspangler@google.com49fdf182009-10-10 00:57:34 +00008#include <gtest/gtest.h>
9#include "update_engine/action.h"
rspangler@google.com49fdf182009-10-10 00:57:34 +000010
Andrew de los Reyes4fe15d02009-12-10 19:01:36 -080011using std::string;
12
rspangler@google.com49fdf182009-10-10 00:57:34 +000013namespace chromeos_update_engine {
14
15using chromeos_update_engine::ActionPipe;
16
17class ActionProcessorTestAction;
18
19template<>
20class ActionTraits<ActionProcessorTestAction> {
21 public:
22 typedef string OutputObjectType;
23 typedef string InputObjectType;
24};
25
26// This is a simple Action class for testing.
Yunlian Jianga178e5e2013-04-05 14:41:56 -070027class ActionProcessorTestAction : public Action<ActionProcessorTestAction> {
28 public:
rspangler@google.com49fdf182009-10-10 00:57:34 +000029 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 Arnoldd1c4d2d2014-06-05 14:07:53 -070037 processor()->ActionComplete(this, ErrorCode::kSuccess);
rspangler@google.com49fdf182009-10-10 00:57:34 +000038 }
39 string Type() const { return "ActionProcessorTestAction"; }
40};
41
42class 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.
46TEST(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
62namespace {
63class MyActionProcessorDelegate : public ActionProcessorDelegate {
64 public:
65 explicit MyActionProcessorDelegate(const ActionProcessor* processor)
Andrew de los Reyes3270f742010-07-15 22:28:14 -070066 : processor_(processor),
67 processing_done_called_(false),
68 processing_stopped_called_(false),
69 action_completed_called_(false),
Gilad Arnoldd1c4d2d2014-06-05 14:07:53 -070070 action_exit_code_(ErrorCode::kError) {}
rspangler@google.com49fdf182009-10-10 00:57:34 +000071
Darin Petkovc1a8b422010-07-19 11:34:49 -070072 virtual void ProcessingDone(const ActionProcessor* processor,
David Zeuthena99981f2013-04-29 13:42:47 -070073 ErrorCode code) {
rspangler@google.com49fdf182009-10-10 00:57:34 +000074 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.comc98a7ed2009-12-04 18:54:03 +000083 virtual void ActionCompleted(ActionProcessor* processor,
84 AbstractAction* action,
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(action_completed_called_);
88 action_completed_called_ = true;
Darin Petkovc1a8b422010-07-19 11:34:49 -070089 action_exit_code_ = code;
rspangler@google.com49fdf182009-10-10 00:57:34 +000090 }
91
92 const ActionProcessor* processor_;
93 bool processing_done_called_;
94 bool processing_stopped_called_;
95 bool action_completed_called_;
David Zeuthena99981f2013-04-29 13:42:47 -070096 ErrorCode action_exit_code_;
rspangler@google.com49fdf182009-10-10 00:57:34 +000097};
Alex Vakulenkod2779df2014-06-16 13:19:00 -070098} // namespace
rspangler@google.com49fdf182009-10-10 00:57:34 +000099
100TEST(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 Vakulenko88b591f2014-08-28 16:48:57 -0700109 action_processor.set_delegate(nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000110 EXPECT_TRUE(delegate.processing_done_called_);
111 EXPECT_TRUE(delegate.action_completed_called_);
112}
113
114TEST(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 Vakulenko88b591f2014-08-28 16:48:57 -0700123 action_processor.set_delegate(nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000124 EXPECT_TRUE(delegate.processing_stopped_called_);
125 EXPECT_FALSE(delegate.action_completed_called_);
126 EXPECT_FALSE(action_processor.IsRunning());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700127 EXPECT_EQ(nullptr, action_processor.current_action());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000128}
129
130TEST(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 Vakulenko88b591f2014-08-28 16:48:57 -0700142 EXPECT_EQ(nullptr, action_processor.current_action());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000143 EXPECT_FALSE(action_processor.IsRunning());
144}
145
146TEST(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 Vakulenko88b591f2014-08-28 16:48:57 -0700154 EXPECT_EQ(nullptr, action1.processor());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000155 EXPECT_FALSE(action1.IsRunning());
Alex Vakulenko88b591f2014-08-28 16:48:57 -0700156 EXPECT_EQ(nullptr, action2.processor());
rspangler@google.com49fdf182009-10-10 00:57:34 +0000157 EXPECT_FALSE(action2.IsRunning());
158}
159
160TEST(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 Vakulenko88b591f2014-08-28 16:48:57 -0700175 action_processor.set_delegate(nullptr);
rspangler@google.com49fdf182009-10-10 00:57:34 +0000176}
177
178} // namespace chromeos_update_engine