summaryrefslogtreecommitdiffstats
path: root/net/base/load_log_unittest.cc
blob: 81ccac94fe3be6619f2d31943db8e4cfad2f0d99 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright (c) 2009 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.

#include "net/base/load_log_unittest.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace net {

namespace {

TEST(LoadLogTest, Nullable) {
  // Make sure that the static methods can be called with NULL (no-op).
  // (Should not crash).
  LoadLog::BeginEvent(NULL, LoadLog::TYPE_HOST_RESOLVER_IMPL);
  LoadLog::AddEvent(NULL, LoadLog::TYPE_HOST_RESOLVER_IMPL);
  LoadLog::EndEvent(NULL, LoadLog::TYPE_HOST_RESOLVER_IMPL);
}

TEST(LoadLogTest, Basic) {
  scoped_refptr<LoadLog> log(new LoadLog);

  // Logs start off empty.
  EXPECT_EQ(0u, log->events().size());

  // Add 3 entries.

  log->Add(MakeTime(0), LoadLog::TYPE_HOST_RESOLVER_IMPL, LoadLog::PHASE_BEGIN);
  log->Add(MakeTime(2), LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE);
  log->Add(MakeTime(11), LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART,
           LoadLog::PHASE_END);

  EXPECT_EQ(3u, log->events().size());

  ExpectLogContains(log, 0, MakeTime(0), LoadLog::TYPE_HOST_RESOLVER_IMPL,
                    LoadLog::PHASE_BEGIN);

  ExpectLogContains(log, 1, MakeTime(2), LoadLog::TYPE_CANCELLED,
                    LoadLog::PHASE_NONE);

  ExpectLogContains(log, 2, MakeTime(11),
                    LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART,
                    LoadLog::PHASE_END);
}

// Test that the log's size is strictly bounded.
TEST(LoadLogTest, Truncation) {
  scoped_refptr<LoadLog> log(new LoadLog);

  // Almost max it out.
  for (size_t i = 0; i < LoadLog::kMaxNumEntries - 1; ++i) {
    log->Add(MakeTime(0), LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE);
  }
  EXPECT_EQ(LoadLog::kMaxNumEntries - 1,
            static_cast<int>(log->events().size()));
  EXPECT_EQ(LoadLog::TYPE_CANCELLED,
            log->events()[LoadLog::kMaxNumEntries - 2].type);

  // Max it out (none of these get appended to the log).
  log->Add(MakeTime(0), LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE);
  log->Add(MakeTime(0), LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE);
  log->Add(MakeTime(0), LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE);

  EXPECT_EQ(LoadLog::kMaxNumEntries, log->events().size());

  // We terminated with a "truncation" event.
  ExpectLogContains(log, LoadLog::kMaxNumEntries - 1, MakeTime(0),
                    LoadLog::TYPE_LOG_TRUNCATED, LoadLog::PHASE_NONE);
}

TEST(LoadLogTest, Append) {
  scoped_refptr<LoadLog> log1(new LoadLog);
  scoped_refptr<LoadLog> log2(new LoadLog);

  log1->Add(MakeTime(0), LoadLog::TYPE_HOST_RESOLVER_IMPL,
            LoadLog::PHASE_BEGIN);

  log2->Add(MakeTime(3), LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE);
  log2->Add(MakeTime(9), LoadLog::TYPE_HOST_RESOLVER_IMPL, LoadLog::PHASE_END);

  log1->Append(log2);

  // Add something else to log2 (should NOT be reflected in log1).
  log2->Add(MakeTime(19), LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE);

  EXPECT_EQ(3u, log1->events().size());

  ExpectLogContains(log1, 0, MakeTime(0), LoadLog::TYPE_HOST_RESOLVER_IMPL,
                    LoadLog::PHASE_BEGIN);

  ExpectLogContains(log1, 1, MakeTime(3), LoadLog::TYPE_CANCELLED,
                    LoadLog::PHASE_NONE);

  ExpectLogContains(log1, 2, MakeTime(9), LoadLog::TYPE_HOST_RESOLVER_IMPL,
                    LoadLog::PHASE_END);
}

TEST(LoadLogTest, AppendWithTruncation) {
  // Append() should also respect the maximum number of entries bound.
  // (This is basically the same test as LoadLogTest.Truncation).

  // Create two load logs, which are 2/3 capcity.
  scoped_refptr<LoadLog> log1(new LoadLog);
  scoped_refptr<LoadLog> log2(new LoadLog);
  for (size_t i = 0; i < 2 * LoadLog::kMaxNumEntries / 3; ++i) {
    log1->Add(MakeTime(1), LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE);
    log2->Add(MakeTime(2), LoadLog::TYPE_CANCELLED, LoadLog::PHASE_NONE);
  }

  // Append log2 to log1.
  log1->Append(log2);

  EXPECT_EQ(LoadLog::kMaxNumEntries, log1->events().size());

  // We terminated with a "truncation" event.
  ExpectLogContains(log1, LoadLog::kMaxNumEntries - 1, MakeTime(2),
                    LoadLog::TYPE_LOG_TRUNCATED, LoadLog::PHASE_NONE);
}

TEST(LoadLogTest, EventTypeToString) {
  EXPECT_STREQ("HOST_RESOLVER_IMPL",
               LoadLog::EventTypeToString(LoadLog::TYPE_HOST_RESOLVER_IMPL));
  EXPECT_STREQ("HOST_RESOLVER_IMPL_OBSERVER_ONSTART",
               LoadLog::EventTypeToString(
                  LoadLog::TYPE_HOST_RESOLVER_IMPL_OBSERVER_ONSTART));
}

}  // namespace
}  // namespace net