| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Single threaded tests of DnsHostInfo functionality. | 5 // Single threaded tests of DnsHostInfo functionality. |
| 6 | 6 |
| 7 #include <time.h> | 7 #include <time.h> |
| 8 #include <string> | 8 #include <string> |
| 9 | 9 |
| 10 #include "base/platform_thread.h" |
| 10 #include "chrome/browser/net/dns_host_info.h" | 11 #include "chrome/browser/net/dns_host_info.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 12 | 13 |
| 13 namespace { | 14 namespace { |
| 14 | 15 |
| 15 class DnsHostInfoTest : public testing::Test { | 16 class DnsHostInfoTest : public testing::Test { |
| 16 }; | 17 }; |
| 17 | 18 |
| 18 typedef chrome_browser_net::DnsHostInfo DnsHostInfo; | 19 typedef chrome_browser_net::DnsHostInfo DnsHostInfo; |
| 19 | 20 |
| 20 TEST(DnsHostInfoTest, StateChangeTest) { | 21 TEST(DnsHostInfoTest, StateChangeTest) { |
| 21 DnsHostInfo info_practice, info; | 22 DnsHostInfo info_practice, info; |
| 22 std::string hostname1("domain1.com"), hostname2("domain2.com"); | 23 std::string hostname1("domain1.com"), hostname2("domain2.com"); |
| 23 | 24 |
| 24 // First load DLL, so that their load time won't interfere with tests. | 25 // First load DLL, so that their load time won't interfere with tests. |
| 25 // Some tests involve timing function performance, and DLL time can overwhelm | 26 // Some tests involve timing function performance, and DLL time can overwhelm |
| 26 // test durations (which are considering network vs cache response times). | 27 // test durations (which are considering network vs cache response times). |
| 27 info_practice.SetHostname(hostname2); | 28 info_practice.SetHostname(hostname2); |
| 28 info_practice.SetQueuedState(); | 29 info_practice.SetQueuedState(); |
| 29 info_practice.SetAssignedState(); | 30 info_practice.SetAssignedState(); |
| 30 info_practice.SetFoundState(); | 31 info_practice.SetFoundState(); |
| 31 Sleep(500); // Allow time for DLLs to fully load. | 32 PlatformThread::Sleep(500); // Allow time for DLLs to fully load. |
| 32 | 33 |
| 33 // Complete the construction of real test object. | 34 // Complete the construction of real test object. |
| 34 info.SetHostname(hostname1); | 35 info.SetHostname(hostname1); |
| 35 | 36 |
| 36 EXPECT_TRUE(info.NeedsDnsUpdate(hostname1)) << "error in construction state"; | 37 EXPECT_TRUE(info.NeedsDnsUpdate(hostname1)) << "error in construction state"; |
| 37 info.SetQueuedState(); | 38 info.SetQueuedState(); |
| 38 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) | 39 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) |
| 39 << "update needed after being queued"; | 40 << "update needed after being queued"; |
| 40 info.SetAssignedState(); | 41 info.SetAssignedState(); |
| 41 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) | 42 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) |
| 42 << "update needed while assigned to slave"; | 43 << "update needed while assigned to slave"; |
| 43 info.SetFoundState(); | 44 info.SetFoundState(); |
| 44 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) | 45 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) |
| 45 << "default expiration time is TOOOOO short"; | 46 << "default expiration time is TOOOOO short"; |
| 46 | 47 |
| 47 // Note that time from ASSIGNED to FOUND was VERY short (probably 0ms), so the | 48 // Note that time from ASSIGNED to FOUND was VERY short (probably 0ms), so the |
| 48 // object should conclude that no network activity was needed. As a result, | 49 // object should conclude that no network activity was needed. As a result, |
| 49 // the required time till expiration will be halved (guessing that we were | 50 // the required time till expiration will be halved (guessing that we were |
| 50 // half way through having the cache expire when we did the lookup. | 51 // half way through having the cache expire when we did the lookup. |
| 51 EXPECT_LT(info.resolve_duration().InMilliseconds(), | 52 EXPECT_LT(info.resolve_duration().InMilliseconds(), |
| 52 DnsHostInfo::kMaxNonNetworkDnsLookupDuration.InMilliseconds()) | 53 DnsHostInfo::kMaxNonNetworkDnsLookupDuration.InMilliseconds()) |
| 53 << "Non-net time is set too low"; | 54 << "Non-net time is set too low"; |
| 54 | 55 |
| 55 info.set_cache_expiration(TimeDelta::FromMilliseconds(300)); | 56 info.set_cache_expiration(TimeDelta::FromMilliseconds(300)); |
| 56 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored"; | 57 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored"; |
| 57 // Note that we'll actually get an expiration (effectively) of | 58 // Note that we'll actually get an expiration (effectively) of |
| 58 // 150ms, since there was no detected network activity time during lookup. | 59 // 150ms, since there was no detected network activity time during lookup. |
| 59 Sleep(80); // Not enough time to pass our 150ms mark. | 60 PlatformThread::Sleep(80); // Not enough time to pass our 150ms mark. |
| 60 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored"; | 61 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored"; |
| 61 Sleep(100); // Be sure we sleep (80+100) enough to pass that 150ms mark. | 62 // Be sure we sleep (80+100) enough to pass that 150ms mark. |
| 63 PlatformThread::Sleep(100); |
| 62 EXPECT_TRUE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored"; | 64 EXPECT_TRUE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored"; |
| 63 | 65 |
| 64 // That was a nice life when the object was found.... but next time it won't | 66 // That was a nice life when the object was found.... but next time it won't |
| 65 // be found. We'll sleep for a while, and then come back with not-found. | 67 // be found. We'll sleep for a while, and then come back with not-found. |
| 66 info.SetQueuedState(); | 68 info.SetQueuedState(); |
| 67 info.SetAssignedState(); | 69 info.SetAssignedState(); |
| 68 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) | 70 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) |
| 69 << "update needed while assigned to slave"; | 71 << "update needed while assigned to slave"; |
| 70 Sleep(25); // Greater than minimal expected network latency on DNS lookup. | 72 // Greater than minimal expected network latency on DNS lookup. |
| 73 PlatformThread::Sleep(25); |
| 71 info.SetNoSuchNameState(); | 74 info.SetNoSuchNameState(); |
| 72 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) | 75 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) |
| 73 << "default expiration time is TOOOOO short"; | 76 << "default expiration time is TOOOOO short"; |
| 74 | 77 |
| 75 // Note that now we'll actually utilize an expiration of 300ms, | 78 // Note that now we'll actually utilize an expiration of 300ms, |
| 76 // since there was detected network activity time during lookup. | 79 // since there was detected network activity time during lookup. |
| 77 // We're assuming the caching just started with our lookup. | 80 // We're assuming the caching just started with our lookup. |
| 78 Sleep(80); // Not enough time to pass our 300ms mark. | 81 PlatformThread::Sleep(80); // Not enough time to pass our 300ms mark. |
| 79 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored"; | 82 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored"; |
| 80 Sleep(80); // Still not past our 300ms mark (only about 4+2ms) | 83 // Still not past our 300ms mark (only about 4+2ms) |
| 84 PlatformThread::Sleep(80); |
| 81 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored"; | 85 EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored"; |
| 82 Sleep(150); | 86 PlatformThread::Sleep(150); |
| 83 EXPECT_TRUE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored"; | 87 EXPECT_TRUE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored"; |
| 84 } | 88 } |
| 85 | 89 |
| 86 // TODO(jar): Add death test for illegal state changes, and also for setting | 90 // TODO(jar): Add death test for illegal state changes, and also for setting |
| 87 // hostname when already set. | 91 // hostname when already set. |
| 88 | 92 |
| 89 } // namespace anonymous | 93 } // namespace |
| 90 | 94 |
| OLD | NEW |