Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium 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 | |
| 5 #include "components/rappor/log_uploader.h" | |
| 6 | |
| 7 #include "base/message_loop/message_loop_proxy.h" | |
| 8 #include "net/url_request/test_url_fetcher_factory.h" | |
| 9 #include "net/url_request/url_request_test_util.h" | |
| 10 #include "testing/gtest/include/gtest/gtest.h" | |
| 11 | |
| 12 namespace rappor { | |
| 13 | |
| 14 namespace { | |
| 15 | |
| 16 const char* kTestServerURL = "http://a.com/"; | |
| 17 const char* kTestMimeType = "text/plain"; | |
|
Ilya Sherman
2014/02/13 23:23:08
nit: "const char* foo" -> "const char foo[]"
Steven Holte
2014/02/14 02:53:29
Done.
| |
| 18 | |
| 19 class LogUploaderTest : public testing::Test { | |
| 20 public: | |
| 21 LogUploaderTest() | |
| 22 : request_context_(new net::TestURLRequestContextGetter( | |
| 23 base::MessageLoopProxy::current())), | |
| 24 factory_(NULL) {} | |
| 25 | |
| 26 protected: | |
| 27 // Required for base::MessageLoopProxy::current(). | |
| 28 base::MessageLoopForUI loop_; | |
| 29 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | |
| 30 net::FakeURLFetcherFactory factory_; | |
| 31 | |
| 32 DISALLOW_COPY_AND_ASSIGN(LogUploaderTest); | |
| 33 }; | |
|
Ilya Sherman
2014/02/13 23:23:08
nit: The test harness itself should be declared ou
Steven Holte
2014/02/14 02:53:29
Done.
| |
| 34 | |
| 35 class TestLogUploader : public LogUploader { | |
| 36 public: | |
| 37 TestLogUploader(net::URLRequestContextGetter* request_context) : | |
| 38 LogUploader(GURL(kTestServerURL), kTestMimeType, request_context) { | |
| 39 } | |
| 40 | |
| 41 base::TimeDelta last_interval_set() const { return last_interval_set_; }; | |
| 42 | |
| 43 void StartUpload() { | |
| 44 last_interval_set_ = base::TimeDelta(); | |
| 45 StartScheduledUpload(); | |
| 46 } | |
| 47 | |
| 48 static base::TimeDelta BackOff(base::TimeDelta t) { | |
| 49 return LogUploader::BackOffUploadInterval(t); | |
| 50 } | |
| 51 | |
| 52 protected: | |
| 53 virtual bool IsUploadScheduled() const OVERRIDE { | |
| 54 return last_interval_set() != base::TimeDelta(); | |
| 55 } | |
| 56 | |
| 57 // Schedules a future call to StartScheduledUpload if one isn't already | |
| 58 // pending. | |
| 59 virtual void ScheduleNextUpload(base::TimeDelta interval) OVERRIDE { | |
| 60 EXPECT_EQ(last_interval_set(), base::TimeDelta()); | |
| 61 last_interval_set_ = interval; | |
| 62 } | |
| 63 | |
| 64 base::TimeDelta last_interval_set_; | |
| 65 | |
| 66 DISALLOW_COPY_AND_ASSIGN(TestLogUploader); | |
| 67 }; | |
| 68 | |
| 69 } // namespace | |
| 70 | |
| 71 TEST_F(LogUploaderTest, Success) { | |
| 72 TestLogUploader uploader(request_context_); | |
| 73 | |
| 74 factory_.SetFakeResponse(GURL(kTestServerURL), | |
| 75 std::string(), | |
| 76 net::HTTP_OK, | |
| 77 net::URLRequestStatus::SUCCESS); | |
| 78 | |
| 79 uploader.QueueLog("log1"); | |
| 80 base::MessageLoop::current()->RunUntilIdle(); | |
| 81 // Log should be discarded instead of retransmitted. | |
| 82 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta()); | |
| 83 } | |
| 84 | |
| 85 TEST_F(LogUploaderTest, Rejection) { | |
| 86 TestLogUploader uploader(request_context_); | |
| 87 | |
| 88 factory_.SetFakeResponse(GURL(kTestServerURL), | |
| 89 std::string(), | |
| 90 net::HTTP_BAD_REQUEST, | |
| 91 net::URLRequestStatus::SUCCESS); | |
| 92 | |
| 93 uploader.QueueLog("log1"); | |
| 94 base::MessageLoop::current()->RunUntilIdle(); | |
| 95 // Log should be discarded instead of retransmitted. | |
| 96 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta()); | |
| 97 } | |
| 98 | |
| 99 TEST_F(LogUploaderTest, Failure) { | |
| 100 TestLogUploader uploader(request_context_); | |
| 101 | |
| 102 factory_.SetFakeResponse(GURL(kTestServerURL), | |
| 103 std::string(), | |
| 104 net::HTTP_INTERNAL_SERVER_ERROR, | |
| 105 net::URLRequestStatus::SUCCESS); | |
| 106 | |
| 107 uploader.QueueLog("log1"); | |
| 108 base::MessageLoop::current()->RunUntilIdle(); | |
| 109 // Log should be scheduled for retransmission. | |
| 110 base::TimeDelta error_interval = uploader.last_interval_set(); | |
| 111 EXPECT_GT(error_interval, base::TimeDelta()); | |
| 112 | |
| 113 for (int i = 0; i < 10; i++) { | |
| 114 uploader.QueueLog("logX"); | |
| 115 } | |
| 116 | |
| 117 // A second failure should lead to a longer interval, and the log should | |
| 118 // be discarded due to full queue. | |
| 119 uploader.StartUpload(); | |
| 120 base::MessageLoop::current()->RunUntilIdle(); | |
| 121 EXPECT_GT(uploader.last_interval_set(), error_interval); | |
| 122 | |
| 123 factory_.SetFakeResponse(GURL(kTestServerURL), | |
| 124 std::string(), | |
| 125 net::HTTP_OK, | |
| 126 net::URLRequestStatus::SUCCESS); | |
| 127 | |
| 128 // A success should revert to base interval while queue is not empty. | |
| 129 for (int i = 0; i < 9; i++) { | |
| 130 uploader.StartUpload(); | |
| 131 base::MessageLoop::current()->RunUntilIdle(); | |
| 132 EXPECT_LT(uploader.last_interval_set(), error_interval); | |
| 133 } | |
| 134 | |
| 135 // Queue should be empty. | |
| 136 uploader.StartUpload(); | |
| 137 base::MessageLoop::current()->RunUntilIdle(); | |
| 138 EXPECT_EQ(uploader.last_interval_set(), base::TimeDelta()); | |
| 139 } | |
| 140 | |
| 141 TEST_F(LogUploaderTest, Backoff) { | |
| 142 base::TimeDelta current = base::TimeDelta(); | |
| 143 base::TimeDelta next = base::TimeDelta::FromSeconds(1); | |
| 144 // Backoff until the maximum is reached. | |
| 145 while (next > current) { | |
| 146 current = next; | |
| 147 next = TestLogUploader::BackOff(current); | |
| 148 } | |
| 149 // Maximum backoff should have been reached. | |
| 150 EXPECT_EQ(next, current); | |
| 151 } | |
| 152 | |
| 153 } // namespace rappor | |
| OLD | NEW |