summaryrefslogtreecommitdiffstats
path: root/net/base/load_log_unittest.cc
diff options
context:
space:
mode:
authorericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-14 03:01:09 +0000
committerericroman@google.com <ericroman@google.com@0039d316-1c4b-4281-b951-d872f2087c98>2009-08-14 03:01:09 +0000
commit54e1377ea309c5d8ab318ee9691b46249d7441b6 (patch)
tree8471e44056027dff255ff5b22ba083a9482b41c3 /net/base/load_log_unittest.cc
parent672eca0fd191acd39c28430d1ed1f327594faff8 (diff)
downloadchromium_src-54e1377ea309c5d8ab318ee9691b46249d7441b6.zip
chromium_src-54e1377ea309c5d8ab318ee9691b46249d7441b6.tar.gz
chromium_src-54e1377ea309c5d8ab318ee9691b46249d7441b6.tar.bz2
Implement LoadLog, and hook up HostResolverImpl to LoadLog.
The functionality in load_log_util.h is currently unused, but since it motivates the implementation of LoadLog, figured it should be included with this review. Note that I decided against defining the loggable event types as LoadStates, since I wanted more flexibility in adding crazy stuff, and they really seem too implementation specific to be in LoadStates. BUG=http://crbug.com/14478 TEST=net_unittests Review URL: http://codereview.chromium.org/165404 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@23412 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net/base/load_log_unittest.cc')
-rw-r--r--net/base/load_log_unittest.cc105
1 files changed, 105 insertions, 0 deletions
diff --git a/net/base/load_log_unittest.cc b/net/base/load_log_unittest.cc
new file mode 100644
index 0000000..9634dd9
--- /dev/null
+++ b/net/base/load_log_unittest.cc
@@ -0,0 +1,105 @@
+// 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 {
+
+base::TimeTicks MakeTime(int t) {
+ base::TimeTicks ticks; // initialized to 0.
+ ticks += base::TimeDelta::FromMilliseconds(t);
+ return ticks;
+}
+
+void ExpectLogContains(const LoadLog* log,
+ size_t i,
+ LoadLog::EventType expected_event,
+ LoadLog::EventPhase expected_phase) {
+ ASSERT_LT(i, log->events().size());
+ EXPECT_EQ(expected_event, log->events()[i].type);
+ EXPECT_EQ(expected_phase, log->events()[i].phase);
+}
+
+void ExpectLogContains(const LoadLog* log,
+ size_t i,
+ base::TimeTicks expected_time,
+ LoadLog::EventType expected_event,
+ LoadLog::EventPhase expected_phase) {
+ ASSERT_LT(i, log->events().size());
+ EXPECT_TRUE(expected_time == log->events()[i].time);
+ EXPECT_EQ(expected_event, log->events()[i].type);
+ EXPECT_EQ(expected_phase, log->events()[i].phase);
+}
+
+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, 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