1 // Copyright (c) 2010 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 // Single threaded tests of UrlInfo functionality.
6
7 #include <time.h>
8 #include <string>
9
10 #include "base/threading/platform_thread.h"
11 #include "base/time.h"
12 #include "chrome/browser/net/url_info.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 using base::TimeDelta;
16 using base::TimeTicks;
17
18 namespace {
19
20 class UrlHostInfoTest : public testing::Test {
21 };
22
23 typedef chrome_browser_net::UrlInfo UrlInfo;
24
25 // 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.
TEST(UrlHostInfoTest,StateChangeTest)28 TEST(UrlHostInfoTest, StateChangeTest) {
29 UrlInfo info_practice, info;
30 GURL url1("http://domain1.com:80"), url2("https://domain2.com:443");
31
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).
35 info_practice.SetUrl(url2);
36 info_practice.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
37 info_practice.SetAssignedState();
38 info_practice.SetFoundState();
39
40 // Start test with actual (long/default) expiration time intact.
41
42 // Complete the construction of real test object.
43 info.SetUrl(url1);
44 EXPECT_TRUE(info.NeedsDnsUpdate()) << "error in construction state";
45 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
46 EXPECT_FALSE(info.NeedsDnsUpdate()) << "update needed after being queued";
47 info.SetAssignedState();
48 EXPECT_FALSE(info.NeedsDnsUpdate()) << "update needed during resolution";
49 base::TimeTicks before_resolution_complete = TimeTicks::Now();
50 info.SetFoundState();
51 // "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 }
58
59 // Run similar test with a shortened expiration, so we can trigger it.
60 const int kMockExpirationTime(300); // Vastly reduced expiration time.
61 info.set_cache_expiration(TimeDelta::FromMilliseconds(kMockExpirationTime));
62
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.
65 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
66 EXPECT_FALSE(info.NeedsDnsUpdate());
67 info.SetAssignedState();
68 EXPECT_FALSE(info.NeedsDnsUpdate());
69 // Greater than minimal expected network latency on DNS lookup.
70 base::PlatformThread::Sleep(25);
71 before_resolution_complete = TimeTicks::Now();
72 info.SetNoSuchNameState();
73 // "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.
76 EXPECT_GT((TimeTicks::Now() - before_resolution_complete).InMilliseconds(),
77 kMockExpirationTime);
78 return;
79 }
80 // Wait over 300ms, so it should definately be considered out of cache.
81 base::PlatformThread::Sleep(kMockExpirationTime + 20);
82 EXPECT_TRUE(info.NeedsDnsUpdate()) << "expiration time not honored";
83 }
84
85 // When a system gets "congested" relative to DNS, it means it is doing too many
86 // DNS resolutions, and bogging down the system. When we detect such a
87 // situation, we divert the sequence of states a UrlInfo instance moves
88 // through. Rather than proceeding from QUEUED (waiting in a name queue for a
89 // worker thread that can resolve the name) to ASSIGNED (where a worker thread
90 // actively resolves the name), we enter the ASSIGNED state (without actually
91 // getting sent to a resolver thread) and reset our state to what it was before
92 // the corresponding name was put in the work_queue_. This test drives through
93 // the state transitions used in such congestion handling.
TEST(UrlHostInfoTest,CongestionResetStateTest)94 TEST(UrlHostInfoTest, CongestionResetStateTest) {
95 UrlInfo info;
96 GURL url("http://domain1.com:80");
97
98 info.SetUrl(url);
99 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
100 info.SetAssignedState();
101 EXPECT_TRUE(info.is_assigned());
102
103 info.RemoveFromQueue(); // Do the reset.
104 EXPECT_FALSE(info.is_assigned());
105
106 // Since this was a new info instance, and it never got resolved, we land back
107 // in a PENDING state rather than FOUND or NO_SUCH_NAME.
108 EXPECT_FALSE(info.was_found());
109 EXPECT_FALSE(info.was_nonexistent());
110
111 // Make sure we're completely re-usable, by going throug a normal flow.
112 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
113 info.SetAssignedState();
114 info.SetFoundState();
115 EXPECT_TRUE(info.was_found());
116
117 // Use the congestion flow, and check that we end up in the found state.
118 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED);
119 info.SetAssignedState();
120 info.RemoveFromQueue(); // Do the reset.
121 EXPECT_FALSE(info.is_assigned());
122 EXPECT_TRUE(info.was_found()); // Back to what it was before being queued.
123 }
124
125
126 // TODO(jar): Add death test for illegal state changes, and also for setting
127 // hostname when already set.
128
129 } // namespace
130