| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-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/platform_thread.h" | |
| 11 #include "chrome/browser/net/dns_host_info.h" | |
| 12 #include "testing/gtest/include/gtest/gtest.h" | |
| 13 | |
| 14 using base::TimeDelta; | |
| 15 | |
| 16 namespace { | |
| 17 | |
| 18 class DnsHostInfoTest : public testing::Test { | |
| 19 }; | |
| 20 | |
| 21 typedef chrome_browser_net::UrlInfo UrlInfo; | |
| 22 | |
| 23 TEST(DnsHostInfoTest, StateChangeTest) { | |
| 24 UrlInfo info_practice, info; | |
| 25 GURL url1("http://domain1.com:80"), url2("https://domain2.com:443"); | |
| 26 | |
| 27 // First load DLL, so that their load time won't interfere with tests. | |
| 28 // Some tests involve timing function performance, and DLL time can overwhelm | |
| 29 // test durations (which are considering network vs cache response times). | |
| 30 info_practice.SetUrl(url2); | |
| 31 info_practice.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED); | |
| 32 info_practice.SetAssignedState(); | |
| 33 info_practice.SetFoundState(); | |
| 34 PlatformThread::Sleep(500); // Allow time for DLLs to fully load. | |
| 35 | |
| 36 // Complete the construction of real test object. | |
| 37 info.SetUrl(url1); | |
| 38 | |
| 39 EXPECT_TRUE(info.NeedsDnsUpdate()) << "error in construction state"; | |
| 40 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED); | |
| 41 EXPECT_FALSE(info.NeedsDnsUpdate()) | |
| 42 << "update needed after being queued"; | |
| 43 info.SetAssignedState(); | |
| 44 EXPECT_FALSE(info.NeedsDnsUpdate()); | |
| 45 info.SetFoundState(); | |
| 46 EXPECT_FALSE(info.NeedsDnsUpdate()) | |
| 47 << "default expiration time is TOOOOO short"; | |
| 48 | |
| 49 // Note that time from ASSIGNED to FOUND was VERY short (probably 0ms), so the | |
| 50 // object should conclude that no network activity was needed. As a result, | |
| 51 // the required time till expiration will be halved (guessing that we were | |
| 52 // half way through having the cache expire when we did the lookup. | |
| 53 EXPECT_LT(info.resolve_duration().InMilliseconds(), | |
| 54 UrlInfo::kMaxNonNetworkDnsLookupDuration.InMilliseconds()) | |
| 55 << "Non-net time is set too low"; | |
| 56 | |
| 57 info.set_cache_expiration(TimeDelta::FromMilliseconds(300)); | |
| 58 EXPECT_FALSE(info.NeedsDnsUpdate()) << "expiration time not honored"; | |
| 59 PlatformThread::Sleep(80); // Not enough time to pass our 300ms mark. | |
| 60 EXPECT_FALSE(info.NeedsDnsUpdate()) << "expiration time not honored"; | |
| 61 | |
| 62 // That was a nice life when the object was found.... but next time it won't | |
| 63 // be found. We'll sleep for a while, and then come back with not-found. | |
| 64 info.SetQueuedState(UrlInfo::UNIT_TEST_MOTIVATED); | |
| 65 info.SetAssignedState(); | |
| 66 EXPECT_FALSE(info.NeedsDnsUpdate()); | |
| 67 // Greater than minimal expected network latency on DNS lookup. | |
| 68 PlatformThread::Sleep(25); | |
| 69 info.SetNoSuchNameState(); | |
| 70 EXPECT_FALSE(info.NeedsDnsUpdate()) | |
| 71 << "default expiration time is TOOOOO short"; | |
| 72 | |
| 73 // Note that now we'll actually utilize an expiration of 300ms, | |
| 74 // since there was detected network activity time during lookup. | |
| 75 // We're assuming the caching just started with our lookup. | |
| 76 PlatformThread::Sleep(80); // Not enough time to pass our 300ms mark. | |
| 77 EXPECT_FALSE(info.NeedsDnsUpdate()) << "expiration time not honored"; | |
| 78 // Still not past our 300ms mark (only about 4+2ms) | |
| 79 PlatformThread::Sleep(80); | |
| 80 EXPECT_FALSE(info.NeedsDnsUpdate()) << "expiration time not honored"; | |
| 81 PlatformThread::Sleep(150); | |
| 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. | |
| 94 TEST(DnsHostInfoTest, 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_nonexistant()); | |
| 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 | |
| OLD | NEW |