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