[email protected] | 8d02de1 | 2012-01-04 18:01:13 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 5 | // Single threaded tests of UrlInfo functionality. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | #include <time.h> |
| 8 | #include <string> |
| 9 | |
[email protected] | f214f879 | 2011-01-01 02:17:08 | [diff] [blame] | 10 | #include "base/threading/platform_thread.h" |
[email protected] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 11 | #include "base/time.h" |
[email protected] | 3530cd9 | 2010-06-27 06:22:01 | [diff] [blame] | 12 | #include "chrome/browser/net/url_info.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 13 | #include "testing/gtest/include/gtest/gtest.h" |
| 14 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 15 | using base::TimeDelta; |
[email protected] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 16 | using base::TimeTicks; |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 17 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | namespace { |
| 19 | |
[email protected] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 20 | class UrlHostInfoTest : public testing::Test { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | }; |
| 22 | |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 23 | typedef chrome_browser_net::UrlInfo UrlInfo; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | |
[email protected] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 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. |
| 28 | TEST(UrlHostInfoTest, StateChangeTest) { |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 29 | UrlInfo info_practice, info; |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 30 | GURL url1("http://domain1.com:80"), url2("https://domain2.com:443"); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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). |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 35 | info_practice.SetUrl(url2); |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 36 | info_practice.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 37 | info_practice.SetAssignedState(); |
| 38 | info_practice.SetFoundState(); |
[email protected] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 39 | |
| 40 | // Start test with actual (long/default) expiration time intact. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | |
| 42 | // Complete the construction of real test object. |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 43 | info.SetUrl(url1); |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 44 | EXPECT_TRUE(info.NeedsDnsUpdate()) << "error in construction state"; |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 45 | info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED); |
[email protected] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 46 | EXPECT_FALSE(info.NeedsDnsUpdate()) << "update needed after being queued"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 47 | info.SetAssignedState(); |
[email protected] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 48 | EXPECT_FALSE(info.NeedsDnsUpdate()) << "update needed during resolution"; |
| 49 | base::TimeTicks before_resolution_complete = TimeTicks::Now(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | info.SetFoundState(); |
[email protected] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 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 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 58 | |
[email protected] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 59 | // Run similar test with a shortened expiration, so we can trigger it. |
[email protected] | 8d02de1 | 2012-01-04 18:01:13 | [diff] [blame] | 60 | const TimeDelta kMockExpirationTime = TimeDelta::FromMilliseconds(300); |
| 61 | info.set_cache_expiration(kMockExpirationTime); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 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. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 65 | info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED); |
[email protected] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 66 | EXPECT_FALSE(info.NeedsDnsUpdate()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 67 | info.SetAssignedState(); |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 68 | EXPECT_FALSE(info.NeedsDnsUpdate()); |
[email protected] | 954bc8a5 | 2008-09-17 18:16:01 | [diff] [blame] | 69 | // Greater than minimal expected network latency on DNS lookup. |
[email protected] | 8d02de1 | 2012-01-04 18:01:13 | [diff] [blame] | 70 | base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(25)); |
[email protected] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 71 | before_resolution_complete = TimeTicks::Now(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 72 | info.SetNoSuchNameState(); |
[email protected] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 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. |
[email protected] | 8d02de1 | 2012-01-04 18:01:13 | [diff] [blame] | 76 | EXPECT_GT((TimeTicks::Now() - before_resolution_complete), |
[email protected] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 77 | kMockExpirationTime); |
| 78 | return; |
| 79 | } |
| 80 | // Wait over 300ms, so it should definately be considered out of cache. |
[email protected] | 8d02de1 | 2012-01-04 18:01:13 | [diff] [blame] | 81 | base::PlatformThread::Sleep(kMockExpirationTime + |
| 82 | TimeDelta::FromMilliseconds(20)); |
[email protected] | 760d970a | 2010-05-18 00:39:18 | [diff] [blame] | 83 | EXPECT_TRUE(info.NeedsDnsUpdate()) << "expiration time not honored"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | } |
| 85 | |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 86 | // 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] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 88 | // situation, we divert the sequence of states a UrlInfo instance moves |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 89 | // 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] | c2bfb817 | 2010-09-29 17:37:54 | [diff] [blame] | 95 | TEST(UrlHostInfoTest, CongestionResetStateTest) { |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 96 | UrlInfo info; |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 97 | GURL url("http://domain1.com:80"); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 98 | |
[email protected] | c5629c3 | 2010-06-23 01:22:43 | [diff] [blame] | 99 | info.SetUrl(url); |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 100 | info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 101 | 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] | e7afe245 | 2010-08-22 16:19:13 | [diff] [blame] | 110 | EXPECT_FALSE(info.was_nonexistent()); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 111 | |
| 112 | // Make sure we're completely re-usable, by going throug a normal flow. |
[email protected] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 113 | info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 114 | 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] | 74be069e8 | 2010-06-25 00:12:49 | [diff] [blame] | 119 | info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED); |
[email protected] | a20bc09 | 2009-06-05 01:34:20 | [diff] [blame] | 120 | 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.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | // TODO(jar): Add death test for illegal state changes, and also for setting |
| 128 | // hostname when already set. |
| 129 | |
[email protected] | 954bc8a5 | 2008-09-17 18:16:01 | [diff] [blame] | 130 | } // namespace |