Index: base/debug/activity_analyzer_unittest.cc |
diff --git a/base/debug/activity_analyzer_unittest.cc b/base/debug/activity_analyzer_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..ac67b8c4940c69fecb1ded1472f29d45921dba1b |
--- /dev/null |
+++ b/base/debug/activity_analyzer_unittest.cc |
@@ -0,0 +1,100 @@ |
+// Copyright 2016 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 "base/debug/activity_analyzer.h" |
+ |
+#include <memory> |
+ |
+#include "base/bind.h" |
+#include "base/files/file.h" |
+#include "base/files/file_util.h" |
+#include "base/files/memory_mapped_file.h" |
+#include "base/files/scoped_temp_dir.h" |
+#include "base/memory/ptr_util.h" |
+#include "base/pending_task.h" |
+#include "base/process/process.h" |
+#include "base/synchronization/condition_variable.h" |
+#include "base/synchronization/lock.h" |
+#include "base/synchronization/spin_wait.h" |
+#include "base/threading/platform_thread.h" |
+#include "base/threading/simple_thread.h" |
+#include "base/time/time.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace base { |
+namespace debug { |
+ |
+namespace { |
+ |
+class TestActivityTracker : public ThreadActivityTracker { |
+ public: |
+ TestActivityTracker(std::unique_ptr<char[]> memory, size_t mem_size) |
+ : ThreadActivityTracker(memset(memory.get(), 0, mem_size), mem_size), |
+ mem_segment_(std::move(memory)) {} |
+ |
+ ~TestActivityTracker() override {} |
+ |
+ private: |
+ std::unique_ptr<char[]> mem_segment_; |
+}; |
+ |
+} // namespace |
+ |
+ |
+class ActivityAnalyzerTest : public testing::Test { |
+ public: |
+ const int kMemorySize = 1 << 10; // 1KiB |
Sigurður Ásgeirsson
2016/06/14 15:28:13
static?
bcwhite
2016/06/14 19:48:45
In general it doesn't matter since the compiler wi
|
+ const int kStackSize = 256; |
+ |
+ using Activity = ThreadActivityAnalyzer::Activity; |
+ |
+ ActivityAnalyzerTest() {} |
+ |
+ ~ActivityAnalyzerTest() override { |
+ GlobalActivityTracker* global_tracker = GlobalActivityTracker::Get(); |
+ if (global_tracker) { |
+ global_tracker->ReleaseTrackerForCurrentThreadForTesting(); |
+ delete global_tracker; |
+ } |
+ } |
+ |
+ std::unique_ptr<ThreadActivityTracker> CreateActivityTracker() { |
+ std::unique_ptr<char[]> memory(new char[kStackSize]); |
+ return WrapUnique(new TestActivityTracker(std::move(memory), kStackSize)); |
+ } |
+ |
+ static void DoNothing() {} |
+}; |
+ |
+TEST_F(ActivityAnalyzerTest, ThreadAnalyzerConstruction) { |
+ std::unique_ptr<ThreadActivityTracker> tracker = CreateActivityTracker(); |
+ { |
+ ThreadActivityAnalyzer analyzer(tracker.get()); |
+ EXPECT_TRUE(analyzer.IsValid()); |
+ EXPECT_EQ(PlatformThread::GetName(), analyzer.GetThreadName()); |
+ } |
+ |
+ // TODO(bcwhite): More tests. |
Sigurður Ásgeirsson
2016/06/14 15:28:13
Are there already cross-process tests?
bcwhite
2016/06/14 19:48:45
No but in practice it doesn't matter since there a
|
+} |
+ |
+TEST_F(ActivityAnalyzerTest, GlobalAnalyzerConstruction) { |
+ GlobalActivityTracker::CreateWithLocalMemory(kMemorySize, 0, "", 3); |
+ // ThreadActivityTracker* tracker = |
+ // GlobalActivityTracker::Get()->GetOrCreateTrackerForCurrentThread(); |
+ |
+ PersistentMemoryAllocator* allocator = |
+ GlobalActivityTracker::Get()->allocator(); |
+ GlobalActivityAnalyzer analyzer(WrapUnique( |
+ new PersistentMemoryAllocator(const_cast<void*>(allocator->data()), |
+ allocator->size(), 0, 0, "", true))); |
+ |
+ // The only thread is the test thread. |
+ ThreadActivityAnalyzer* ta1 = analyzer.GetFirstAnalyzer(); |
Sigurður Ásgeirsson
2016/06/14 15:28:13
I'm not sure this is a safe assumption, given the
bcwhite
2016/06/14 19:48:45
I'll look into it.
|
+ ASSERT_TRUE(ta1); |
+ EXPECT_FALSE(analyzer.GetNextAnalyzer()); |
+ EXPECT_EQ(ta1, analyzer.GetAnalyzerForThread(ta1->GetThreadKey())); |
+} |
+ |
+} // namespace debug |
+} // namespace base |