AU: OmahaRequestAction: allow to be skipped.
This CL changes OmahaRequestAction to take a request to skip its
action when it's run. This will be useful in a future CL, where we'll
want to schedule an OmahaRequestAction to run, but then in some cases
prevent it from actually doing so.
This also changes MockHttpFetcher to be able, if properly configured,
to fail it it's used. This is used in the test to make sure that a
skipped OmahaRequestAction does no HTTP traffic.
BUG=chromium-os:13813
TEST=unittests
Review URL: http://codereview.chromium.org/6677146
Change-Id: Ic3e4099d221c4d7d0bca65b1a0064c33dca4edb5
diff --git a/mock_http_fetcher.cc b/mock_http_fetcher.cc
index 1a88b94..5452d5d 100644
--- a/mock_http_fetcher.cc
+++ b/mock_http_fetcher.cc
@@ -3,10 +3,14 @@
// found in the LICENSE file.
#include "update_engine/mock_http_fetcher.h"
-#include <algorithm>
-#include "base/logging.h"
-// This is a mac implementation of HttpFetcher which is useful for testing.
+#include <algorithm>
+
+#include <base/logging.h>
+#include <gtest/gtest.h>
+
+
+// This is a mock implementation of HttpFetcher which is useful for testing.
using std::min;
@@ -17,6 +21,7 @@
}
void MockHttpFetcher::BeginTransfer(const std::string& url) {
+ EXPECT_FALSE(never_use_);
if (fail_transfer_ || data_.empty()) {
// No data to send, just notify of completion..
SignalTransferComplete();
diff --git a/mock_http_fetcher.h b/mock_http_fetcher.h
index b46e023..6d72b92 100644
--- a/mock_http_fetcher.h
+++ b/mock_http_fetcher.h
@@ -36,7 +36,8 @@
timeout_source_(NULL),
timout_tag_(0),
paused_(false),
- fail_transfer_(false) {
+ fail_transfer_(false),
+ never_use_(false) {
data_.insert(data_.end(), data, data + size);
}
@@ -66,6 +67,9 @@
// Fail the transfer. This simulates a network failure.
void FailTransfer(int http_response_code);
+ // If set to true, this will EXPECT fail on BeginTransfer
+ void set_never_use(bool never_use) { never_use_ = never_use; }
+
const std::vector<char>& post_data() const {
return post_data_;
}
@@ -108,6 +112,9 @@
// Set to true if the transfer should fail.
bool fail_transfer_;
+ // Set to true if BeginTransfer should EXPECT fail.
+ bool never_use_;
+
DISALLOW_COPY_AND_ASSIGN(MockHttpFetcher);
};
diff --git a/omaha_request_action.cc b/omaha_request_action.cc
index 4c77ce0..a02387e 100644
--- a/omaha_request_action.cc
+++ b/omaha_request_action.cc
@@ -179,7 +179,8 @@
event_(event),
http_fetcher_(http_fetcher),
ping_active_days_(0),
- ping_roll_call_days_(0) {}
+ ping_roll_call_days_(0),
+ should_skip_(false) {}
OmahaRequestAction::~OmahaRequestAction() {}
@@ -216,6 +217,10 @@
}
void OmahaRequestAction::PerformAction() {
+ if (should_skip_) {
+ processor_->ActionComplete(this, kActionCodeSuccess);
+ return;
+ }
http_fetcher_->set_delegate(this);
InitPingDays();
string request_post(FormatRequest(event_.get(),
diff --git a/omaha_request_action.h b/omaha_request_action.h
index 948bcc7..8749546 100644
--- a/omaha_request_action.h
+++ b/omaha_request_action.h
@@ -150,6 +150,8 @@
// Returns true if this is an Event request, false if it's an UpdateCheck.
bool IsEvent() const { return event_.get() != NULL; }
+ void set_should_skip(bool should_skip) { should_skip_ = should_skip; }
+
private:
// If this is an update check request, initializes
// |ping_active_days_| and |ping_roll_call_days_| to values that may
@@ -181,6 +183,9 @@
int ping_active_days_;
int ping_roll_call_days_;
+ // If true, this action should be a noop.
+ bool should_skip_;
+
DISALLOW_COPY_AND_ASSIGN(OmahaRequestAction);
};
diff --git a/omaha_request_action_unittest.cc b/omaha_request_action_unittest.cc
index ece5a7d..409e947 100755
--- a/omaha_request_action_unittest.cc
+++ b/omaha_request_action_unittest.cc
@@ -283,6 +283,32 @@
EXPECT_FALSE(processor.IsRunning());
}
+TEST(OmahaRequestActionTest, SkipTest) {
+ const string http_response("invalid xml>");
+
+ GMainLoop *loop = g_main_loop_new(g_main_context_default(), FALSE);
+
+ NiceMock<PrefsMock> prefs;
+ MockHttpFetcher* fetcher = new MockHttpFetcher(http_response.data(),
+ http_response.size(),
+ NULL);
+ fetcher->set_never_use(true);
+ OmahaRequestAction action(&prefs, kDefaultTestParams,
+ new OmahaEvent(OmahaEvent::kTypeUpdateComplete),
+ fetcher); // Passes fetcher ownership
+ action.set_should_skip(true);
+ OmahaRequestActionTestProcessorDelegate delegate;
+ delegate.loop_ = loop;
+ ActionProcessor processor;
+ processor.set_delegate(&delegate);
+ processor.EnqueueAction(&action);
+
+ g_timeout_add(0, &StartProcessorInRunLoop, &processor);
+ g_main_loop_run(loop);
+ g_main_loop_unref(loop);
+ EXPECT_FALSE(processor.IsRunning());
+}
+
TEST(OmahaRequestActionTest, InvalidXmlTest) {
OmahaResponse response;
ASSERT_FALSE(