summaryrefslogtreecommitdiffstats
path: root/chrome/browser/net/dns_host_info_unittest.cc
blob: 3ab5db797388395e8a2dae37d3d6b212f0633998 (plain)
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
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Single threaded tests of DnsHostInfo functionality.

#include <time.h>
#include <string>

#include "base/platform_thread.h"
#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();
  PlatformThread::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.
  PlatformThread::Sleep(80);  // Not enough time to pass our 150ms mark.
  EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored";
  // Be sure we sleep (80+100) enough to pass that 150ms mark.
  PlatformThread::Sleep(100);
  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";
  // Greater than minimal expected network latency on DNS lookup.
  PlatformThread::Sleep(25);
  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.
  PlatformThread::Sleep(80);  // Not enough time to pass our 300ms mark.
  EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored";
  // Still not past our 300ms mark (only about 4+2ms)
  PlatformThread::Sleep(80);
  EXPECT_FALSE(info.NeedsDnsUpdate(hostname1)) << "expiration time not honored";
  PlatformThread::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