summaryrefslogtreecommitdiffstats
path: root/net/url_request/url_request_throttler_unittest.cc
diff options
context:
space:
mode:
authorjoi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-17 15:35:07 +0000
committerjoi@chromium.org <joi@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2011-03-17 15:35:07 +0000
commitcade5686b5ec6a116543bbc29dc4f6a1068d5c17 (patch)
treed0f59d8c3218ca749c6378fcab26431df24e723d /net/url_request/url_request_throttler_unittest.cc
parent47f1c5806ddbdf336db68b4274a7d9f3fba5b3ba (diff)
downloadchromium_src-cade5686b5ec6a116543bbc29dc4f6a1068d5c17.zip
chromium_src-cade5686b5ec6a116543bbc29dc4f6a1068d5c17.tar.gz
chromium_src-cade5686b5ec6a116543bbc29dc4f6a1068d5c17.tar.bz2
Extracting core back-off logic into a separate class,
BackoffEntry. Simplifying the logic slightly while I'm there, removing special support for "after the fact" malformed bodies (the error count doesn't need to be 100% accurate) and removing a constant value added to back-off times (it was only being added once anyway, had close to zero effect). Modifying URLRequestThrottlerEntry and related tests to use the new class instead of co-mingling sliding window logic and exponential back-off logic. Removing now-unnecessary StressTest and associated ugly wart "SetEntryLifetimeMsForTest" method on the URLRequestThrottlerEntryInterface class. Fixing up a few minor things e.g. #pragma once while I'm in there. BUG=none TEST=net_unittests.exe, unit_tests.exe Review URL: http://codereview.chromium.org/6697001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@78548 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/url_request/url_request_throttler_unittest.cc')
-rw-r--r--net/url_request/url_request_throttler_unittest.cc109
1 files changed, 48 insertions, 61 deletions
diff --git a/net/url_request/url_request_throttler_unittest.cc b/net/url_request/url_request_throttler_unittest.cc
index 7d4e739..4aed38c 100644
--- a/net/url_request/url_request_throttler_unittest.cc
+++ b/net/url_request/url_request_throttler_unittest.cc
@@ -19,23 +19,61 @@ using base::TimeTicks;
namespace {
class MockURLRequestThrottlerManager;
+class MockBackoffEntry : public net::BackoffEntry {
+ public:
+ explicit MockBackoffEntry(const net::BackoffEntry::Policy* const policy)
+ : net::BackoffEntry(policy), fake_now_(TimeTicks()) {
+ }
+
+ virtual ~MockBackoffEntry() {}
+
+ TimeTicks GetTimeNow() const {
+ return fake_now_;
+ }
+
+ void SetFakeNow(TimeTicks now) {
+ fake_now_ = now;
+ }
+
+ private:
+ TimeTicks fake_now_;
+};
+
class MockURLRequestThrottlerEntry : public net::URLRequestThrottlerEntry {
public :
- MockURLRequestThrottlerEntry() {}
+ MockURLRequestThrottlerEntry() : mock_backoff_entry_(&backoff_policy_) {
+ // Some tests become flaky if we have jitter.
+ backoff_policy_.jitter_factor = 0.0;
+ }
MockURLRequestThrottlerEntry(
const TimeTicks& exponential_backoff_release_time,
const TimeTicks& sliding_window_release_time,
const TimeTicks& fake_now)
- : fake_time_now_(fake_now) {
+ : fake_time_now_(fake_now),
+ mock_backoff_entry_(&backoff_policy_) {
+ // Some tests become flaky if we have jitter.
+ backoff_policy_.jitter_factor = 0.0;
+
+ mock_backoff_entry_.SetFakeNow(fake_now);
set_exponential_backoff_release_time(exponential_backoff_release_time);
set_sliding_window_release_time(sliding_window_release_time);
}
virtual ~MockURLRequestThrottlerEntry() {}
+ const net::BackoffEntry* GetBackoffEntry() const {
+ return &mock_backoff_entry_;
+ }
+
+ net::BackoffEntry* GetBackoffEntry() {
+ return &mock_backoff_entry_;
+ }
+
void ResetToBlank(const TimeTicks& time_now) {
fake_time_now_ = time_now;
- set_exponential_backoff_release_time(time_now);
- set_failure_count(0);
+ mock_backoff_entry_.SetFakeNow(time_now);
+
+ GetBackoffEntry()->InformOfRequest(true); // Sets failure count to 0.
+ GetBackoffEntry()->SetCustomReleaseTime(time_now);
set_sliding_window_release_time(time_now);
}
@@ -44,8 +82,7 @@ class MockURLRequestThrottlerEntry : public net::URLRequestThrottlerEntry {
void set_exponential_backoff_release_time(
const base::TimeTicks& release_time) {
- net::URLRequestThrottlerEntry::set_exponential_backoff_release_time(
- release_time);
+ GetBackoffEntry()->SetCustomReleaseTime(release_time);
}
base::TimeTicks sliding_window_release_time() const {
@@ -59,6 +96,7 @@ class MockURLRequestThrottlerEntry : public net::URLRequestThrottlerEntry {
}
TimeTicks fake_time_now_;
+ MockBackoffEntry mock_backoff_entry_;
};
class MockURLRequestThrottlerHeaderAdapter
@@ -226,7 +264,8 @@ TEST_F(URLRequestThrottlerEntryTest, IsEntryReallyOutdated) {
TimeAndBool(now_, false, __LINE__),
TimeAndBool(now_ - kFiveMs, false, __LINE__),
TimeAndBool(now_ + kFiveMs, false, __LINE__),
- TimeAndBool(now_ - lifetime, false, __LINE__),
+ TimeAndBool(now_ - (lifetime - kFiveMs), false, __LINE__),
+ TimeAndBool(now_ - lifetime, true, __LINE__),
TimeAndBool(now_ - (lifetime + kFiveMs), true, __LINE__)};
for (unsigned int i = 0; i < arraysize(test_values); ++i) {
@@ -352,59 +391,7 @@ TEST(URLRequestThrottlerManager, IsHostBeingRegistered) {
EXPECT_EQ(3, manager.GetNumberOfEntries());
}
-class DummyResponseHeaders : public net::URLRequestThrottlerHeaderInterface {
- public:
- DummyResponseHeaders(int response_code) : response_code_(response_code) {}
-
- std::string GetNormalizedValue(const std::string& key) const {
- return "";
- }
-
- // Returns the HTTP response code associated with the request.
- int GetResponseCode() const {
- return response_code_;
- }
-
- private:
- int response_code_;
-};
-
-TEST(URLRequestThrottlerManager, StressTest) {
- MockURLRequestThrottlerManager manager;
-
- for (int i = 0; i < 10000; ++i) {
- // Make some entries duplicates or triplicates.
- int entry_number = i + 2;
- if (i % 17 == 0) {
- entry_number = i - 1;
- } else if ((i - 1) % 17 == 0) {
- entry_number = i - 2;
- } else if (i % 13 == 0) {
- entry_number = i - 1;
- }
-
- scoped_refptr<net::URLRequestThrottlerEntryInterface> entry =
- manager.RegisterRequestUrl(GURL(StringPrintf(
- "http://bingourl.com/%d", entry_number)));
- entry->IsDuringExponentialBackoff();
- entry->GetExponentialBackoffReleaseTime();
-
- DummyResponseHeaders headers(i % 7 == 0 ? 500 : 200);
- entry->UpdateWithResponse(&headers);
- entry->SetEntryLifetimeMsForTest(1);
-
- entry->IsDuringExponentialBackoff();
- entry->GetExponentialBackoffReleaseTime();
- entry->ReserveSendingTimeForNextRequest(base::TimeTicks::Now());
-
- if (i % 11 == 0) {
- manager.DoGarbageCollectEntries();
- }
- }
-}
-
-// TODO(joi): Remove the debug-only condition after M11 branch point.
-#if defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
+#if defined(GTEST_HAS_DEATH_TEST)
TEST(URLRequestThrottlerManager, NullHandlingTest) {
MockURLRequestThrottlerManager manager;
manager.OverrideEntryForTests(GURL("http://www.foo.com/"), NULL);
@@ -412,4 +399,4 @@ TEST(URLRequestThrottlerManager, NullHandlingTest) {
manager.DoGarbageCollectEntries();
}, "");
}
-#endif // defined(GTEST_HAS_DEATH_TEST) && !defined(NDEBUG)
+#endif // defined(GTEST_HAS_DEATH_TEST)