1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
// Copyright 2008, Google Inc.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Single threaded tests of DnsHostInfo functionality.
#include <time.h>
#include <string>
#include "chrome/browser/net/dns_host_info.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class DnsHostInfoTest : public testing::Test {
};
typedef chrome_browser_net::DnsHostInfo DnsHostInfo;
TEST(DnsHostInfoTest, StateChangeTest) {
DnsHostInfo info_practice, info;
std::string hostname1("domain1.com"), hostname2("domain2.com");
// First load DLL, so that their load time won't interfere with tests.
// Some tests involve timing function performance, and DLL time can overwhelm
// test durations (which are considering network vs cache response times).
info_practice.SetHostname(hostname2);
info_practice.SetQueuedState();
info_practice.SetAssignedState();
info_practice.SetFoundState();
Sleep(500); // Allow time for DLLs to fully load.
// Complete the construction of real test object.
info.SetHostname(hostname1);
EXPECT_TRUE(info.NeedsDnsUpdate(hostname1)) << "error in construction state";
info.SetQueuedState();
EXPECT_FALSE(info.NeedsDnsUpdate(hostname1))
<< "update needed after being queued";
info.SetAssignedState();
EXPECT_FALSE(info.NeedsDnsUpdate(hostname1))
<< "update needed while assigned to slave";
info.SetFoundState();
EXPECT_FALSE(info.NeedsDnsUpdate(hostname1))
<< "default expiration time is TOOOOO short";
// Note that time from ASSIGNED to FOUND was VERY short (probably 0ms), so the
// object should conclude that no network activity was needed. As a result,
// the required time till expiration will be halved (guessing that we were
// half way through having the cache expire when we did the lookup.
EXPECT_LT(info.resolve_duration().InMilliseconds(),
DnsHostInfo::kMaxNonNetworkDnsLookupDuration.InMilliseconds())
<< "Non-net time is set too low";
info.set_cache_expiration(TimeDelta::FromMilliseconds(300));
EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored";
// Note that we'll actually get an expiration (effectively) of
// 150ms, since there was no detected network activity time during lookup.
Sleep(80); // Not enough time to pass our 150ms mark.
EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored";
Sleep(100); // Be sure we sleep (80+100) enough to pass that 150ms mark.
EXPECT_TRUE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored";
// That was a nice life when the object was found.... but next time it won't
// be found. We'll sleep for a while, and then come back with not-found.
info.SetQueuedState();
info.SetAssignedState();
EXPECT_FALSE(info.NeedsDnsUpdate(hostname1))
<< "update needed while assigned to slave";
Sleep(25); // Greater than minimal expected network latency on DNS lookup.
info.SetNoSuchNameState();
EXPECT_FALSE(info.NeedsDnsUpdate(hostname1))
<< "default expiration time is TOOOOO short";
// Note that now we'll actually utilize an expiration of 300ms,
// since there was detected network activity time during lookup.
// We're assuming the caching just started with our lookup.
Sleep(80); // Not enough time to pass our 300ms mark.
EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored";
Sleep(80); // Still not past our 300ms mark (only about 4+2ms)
EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored";
Sleep(150);
EXPECT_TRUE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored";
}
// TODO(jar): Add death test for illegal state changes, and also for setting
// hostname when already set.
} // namespace anonymous
|