blob: 1ae6a89bf38d120cd8d033ff3396b2117d70b5b7 [file] [log] [blame]
[email protected]8d02de12012-01-04 18:01:131// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]74be069e82010-06-25 00:12:495// Single threaded tests of UrlInfo functionality.
initial.commit09911bf2008-07-26 23:55:296
7#include <time.h>
8#include <string>
9
[email protected]f214f8792011-01-01 02:17:0810#include "base/threading/platform_thread.h"
[email protected]c2bfb8172010-09-29 17:37:5411#include "base/time.h"
[email protected]3530cd92010-06-27 06:22:0112#include "chrome/browser/net/url_info.h"
initial.commit09911bf2008-07-26 23:55:2913#include "testing/gtest/include/gtest/gtest.h"
14
[email protected]e1acf6f2008-10-27 20:43:3315using base::TimeDelta;
[email protected]c2bfb8172010-09-29 17:37:5416using base::TimeTicks;
[email protected]e1acf6f2008-10-27 20:43:3317
initial.commit09911bf2008-07-26 23:55:2918namespace {
19
[email protected]c2bfb8172010-09-29 17:37:5420class UrlHostInfoTest : public testing::Test {
initial.commit09911bf2008-07-26 23:55:2921};
22
[email protected]74be069e82010-06-25 00:12:4923typedef chrome_browser_net::UrlInfo UrlInfo;
initial.commit09911bf2008-07-26 23:55:2924
[email protected]c2bfb8172010-09-29 17:37:5425// Cycle throught the states held by a UrlInfo instance, and check to see that
26// states look reasonable as time ticks away. If the test bots are too slow,
27// we'll just give up on this test and exit from it.
28TEST(UrlHostInfoTest, StateChangeTest) {
[email protected]74be069e82010-06-25 00:12:4929 UrlInfo info_practice, info;
[email protected]c5629c32010-06-23 01:22:4330 GURL url1("http://domain1.com:80"), url2("https://domain2.com:443");
initial.commit09911bf2008-07-26 23:55:2931
32 // First load DLL, so that their load time won't interfere with tests.
33 // Some tests involve timing function performance, and DLL time can overwhelm
34 // test durations (which are considering network vs cache response times).
[email protected]c5629c32010-06-23 01:22:4335 info_practice.SetUrl(url2);
[email protected]74be069e82010-06-25 00:12:4936 info_practice.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
initial.commit09911bf2008-07-26 23:55:2937 info_practice.SetAssignedState();
38 info_practice.SetFoundState();
[email protected]c2bfb8172010-09-29 17:37:5439
40 // Start test with actual (long/default) expiration time intact.
initial.commit09911bf2008-07-26 23:55:2941
42 // Complete the construction of real test object.
[email protected]c5629c32010-06-23 01:22:4343 info.SetUrl(url1);
[email protected]760d970a2010-05-18 00:39:1844 EXPECT_TRUE(info.NeedsDnsUpdate()) << "error in construction state";
[email protected]74be069e82010-06-25 00:12:4945 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
[email protected]c2bfb8172010-09-29 17:37:5446 EXPECT_FALSE(info.NeedsDnsUpdate()) << "update needed after being queued";
initial.commit09911bf2008-07-26 23:55:2947 info.SetAssignedState();
[email protected]c2bfb8172010-09-29 17:37:5448 EXPECT_FALSE(info.NeedsDnsUpdate()) << "update needed during resolution";
49 base::TimeTicks before_resolution_complete = TimeTicks::Now();
initial.commit09911bf2008-07-26 23:55:2950 info.SetFoundState();
[email protected]c2bfb8172010-09-29 17:37:5451 // "Immediately" check to see if we need an update yet (we shouldn't).
52 if (info.NeedsDnsUpdate()) {
53 // The test bot must be really slow, so we can verify that.
54 EXPECT_GT((TimeTicks::Now() - before_resolution_complete).InMilliseconds(),
55 UrlInfo::get_cache_expiration().InMilliseconds());
56 return; // Lets punt here, the test bot is too slow.
57 }
initial.commit09911bf2008-07-26 23:55:2958
[email protected]c2bfb8172010-09-29 17:37:5459 // Run similar test with a shortened expiration, so we can trigger it.
[email protected]8d02de12012-01-04 18:01:1360 const TimeDelta kMockExpirationTime = TimeDelta::FromMilliseconds(300);
61 info.set_cache_expiration(kMockExpirationTime);
initial.commit09911bf2008-07-26 23:55:2962
63 // That was a nice life when the object was found.... but next time it won't
64 // be found. We'll sleep for a while, and then come back with not-found.
[email protected]74be069e82010-06-25 00:12:4965 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
[email protected]c2bfb8172010-09-29 17:37:5466 EXPECT_FALSE(info.NeedsDnsUpdate());
initial.commit09911bf2008-07-26 23:55:2967 info.SetAssignedState();
[email protected]760d970a2010-05-18 00:39:1868 EXPECT_FALSE(info.NeedsDnsUpdate());
[email protected]954bc8a52008-09-17 18:16:0169 // Greater than minimal expected network latency on DNS lookup.
[email protected]8d02de12012-01-04 18:01:1370 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(25));
[email protected]c2bfb8172010-09-29 17:37:5471 before_resolution_complete = TimeTicks::Now();
initial.commit09911bf2008-07-26 23:55:2972 info.SetNoSuchNameState();
[email protected]c2bfb8172010-09-29 17:37:5473 // "Immediately" check to see if we need an update yet (we shouldn't).
74 if (info.NeedsDnsUpdate()) {
75 // The test bot must be really slow, so we can verify that.
[email protected]8d02de12012-01-04 18:01:1376 EXPECT_GT((TimeTicks::Now() - before_resolution_complete),
[email protected]c2bfb8172010-09-29 17:37:5477 kMockExpirationTime);
78 return;
79 }
80 // Wait over 300ms, so it should definately be considered out of cache.
[email protected]8d02de12012-01-04 18:01:1381 base::PlatformThread::Sleep(kMockExpirationTime +
82 TimeDelta::FromMilliseconds(20));
[email protected]760d970a2010-05-18 00:39:1883 EXPECT_TRUE(info.NeedsDnsUpdate()) << "expiration time not honored";
initial.commit09911bf2008-07-26 23:55:2984}
85
[email protected]a20bc092009-06-05 01:34:2086// When a system gets "congested" relative to DNS, it means it is doing too many
87// DNS resolutions, and bogging down the system. When we detect such a
[email protected]74be069e82010-06-25 00:12:4988// situation, we divert the sequence of states a UrlInfo instance moves
[email protected]a20bc092009-06-05 01:34:2089// through. Rather than proceeding from QUEUED (waiting in a name queue for a
90// worker thread that can resolve the name) to ASSIGNED (where a worker thread
91// actively resolves the name), we enter the ASSIGNED state (without actually
92// getting sent to a resolver thread) and reset our state to what it was before
93// the corresponding name was put in the work_queue_. This test drives through
94// the state transitions used in such congestion handling.
[email protected]c2bfb8172010-09-29 17:37:5495TEST(UrlHostInfoTest, CongestionResetStateTest) {
[email protected]74be069e82010-06-25 00:12:4996 UrlInfo info;
[email protected]c5629c32010-06-23 01:22:4397 GURL url("http://domain1.com:80");
[email protected]a20bc092009-06-05 01:34:2098
[email protected]c5629c32010-06-23 01:22:4399 info.SetUrl(url);
[email protected]74be069e82010-06-25 00:12:49100 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
[email protected]a20bc092009-06-05 01:34:20101 info.SetAssignedState();
102 EXPECT_TRUE(info.is_assigned());
103
104 info.RemoveFromQueue(); // Do the reset.
105 EXPECT_FALSE(info.is_assigned());
106
107 // Since this was a new info instance, and it never got resolved, we land back
108 // in a PENDING state rather than FOUND or NO_SUCH_NAME.
109 EXPECT_FALSE(info.was_found());
[email protected]e7afe2452010-08-22 16:19:13110 EXPECT_FALSE(info.was_nonexistent());
[email protected]a20bc092009-06-05 01:34:20111
112 // Make sure we're completely re-usable, by going throug a normal flow.
[email protected]74be069e82010-06-25 00:12:49113 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
[email protected]a20bc092009-06-05 01:34:20114 info.SetAssignedState();
115 info.SetFoundState();
116 EXPECT_TRUE(info.was_found());
117
118 // Use the congestion flow, and check that we end up in the found state.
[email protected]74be069e82010-06-25 00:12:49119 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
[email protected]a20bc092009-06-05 01:34:20120 info.SetAssignedState();
121 info.RemoveFromQueue(); // Do the reset.
122 EXPECT_FALSE(info.is_assigned());
123 EXPECT_TRUE(info.was_found()); // Back to what it was before being queued.
124}
125
126
initial.commit09911bf2008-07-26 23:55:29127// TODO(jar): Add death test for illegal state changes, and also for setting
128// hostname when already set.
129
[email protected]954bc8a52008-09-17 18:16:01130} // namespace