Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(270)

Side by Side Diff: base/tracked_objects_unittest.cc

Issue 2488073002: Reuse ThreadData instances associated with terminated named threads. (Closed)
Patch Set: CR danakj #55 (nits) Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Test of classes in the tracked_objects.h classes. 5 // Test of classes in the tracked_objects.h classes.
6 6
7 #include "base/tracked_objects.h" 7 #include "base/tracked_objects.h"
8 8
9 #include <stddef.h> 9 #include <stddef.h>
10 #include <stdint.h> 10 #include <stdint.h>
11 11
12 #include <memory> 12 #include <memory>
13 13
14 #include "base/macros.h"
14 #include "base/process/process_handle.h" 15 #include "base/process/process_handle.h"
16 #include "base/strings/stringprintf.h"
17 #include "base/threading/thread.h"
15 #include "base/time/time.h" 18 #include "base/time/time.h"
16 #include "base/tracking_info.h" 19 #include "base/tracking_info.h"
17 #include "testing/gtest/include/gtest/gtest.h" 20 #include "testing/gtest/include/gtest/gtest.h"
18 21
19 const int kLineNumber = 1776; 22 const int kLineNumber = 1776;
20 const char kFile[] = "FixedUnitTestFileName"; 23 const char kFile[] = "FixedUnitTestFileName";
21 const char kWorkerThreadName[] = "WorkerThread-1"; 24 const char kWorkerThreadName[] = "WorkerThread-*";
22 const char kMainThreadName[] = "SomeMainThreadName"; 25 const char kMainThreadName[] = "SomeMainThreadName";
23 const char kStillAlive[] = "Still_Alive"; 26 const char kStillAlive[] = "Still_Alive";
24 27
25 namespace tracked_objects { 28 namespace tracked_objects {
26 29
27 class TrackedObjectsTest : public testing::Test { 30 class TrackedObjectsTest : public testing::Test {
28 protected: 31 protected:
29 TrackedObjectsTest() { 32 TrackedObjectsTest() {
30 // On entry, leak any database structures in case they are still in use by 33 // On entry, leak any database structures in case they are still in use by
31 // prior threads. 34 // prior threads.
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
101 process_data_phase.tasks[0].death_data.queue_duration_sample); 104 process_data_phase.tasks[0].death_data.queue_duration_sample);
102 105
103 EXPECT_EQ(death_thread, process_data_phase.tasks[0].death_thread_name); 106 EXPECT_EQ(death_thread, process_data_phase.tasks[0].death_thread_name);
104 107
105 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); 108 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
106 } 109 }
107 110
108 // Sets time that will be returned by ThreadData::Now(). 111 // Sets time that will be returned by ThreadData::Now().
109 static void SetTestTime(unsigned int test_time) { test_time_ = test_time; } 112 static void SetTestTime(unsigned int test_time) { test_time_ = test_time; }
110 113
114 int GetNumThreadData() {
Nico 2016/11/24 01:42:04 doesn't this have to take the list lock?
fdoray 2016/11/29 16:10:33 No. ThreadData::first() takes a lock. The list can
115 int num_thread_data = 0;
116 ThreadData* current = ThreadData::first();
117 while (current) {
118 ++num_thread_data;
119 current = current->next();
120 }
121 return num_thread_data;
122 }
123
111 private: 124 private:
112 // Returns test time in milliseconds. 125 // Returns test time in milliseconds.
113 static unsigned int GetTestTime() { return test_time_; } 126 static unsigned int GetTestTime() { return test_time_; }
114 127
115 // Test time in milliseconds. 128 // Test time in milliseconds.
116 static unsigned int test_time_; 129 static unsigned int test_time_;
117 }; 130 };
118 131
119 // static 132 // static
120 unsigned int TrackedObjectsTest::test_time_; 133 unsigned int TrackedObjectsTest::test_time_;
(...skipping 1056 matching lines...) Expand 10 before | Expand all | Expand 10 after
1177 EXPECT_EQ(2, process_data_phase.tasks[t1].death_data.run_duration_sum); 1190 EXPECT_EQ(2, process_data_phase.tasks[t1].death_data.run_duration_sum);
1178 EXPECT_EQ(2, process_data_phase.tasks[t1].death_data.run_duration_max); 1191 EXPECT_EQ(2, process_data_phase.tasks[t1].death_data.run_duration_max);
1179 EXPECT_EQ(2, process_data_phase.tasks[t1].death_data.run_duration_sample); 1192 EXPECT_EQ(2, process_data_phase.tasks[t1].death_data.run_duration_sample);
1180 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.queue_duration_sum); 1193 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.queue_duration_sum);
1181 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.queue_duration_max); 1194 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.queue_duration_max);
1182 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.queue_duration_sample); 1195 EXPECT_EQ(1, process_data_phase.tasks[t1].death_data.queue_duration_sample);
1183 EXPECT_EQ(kMainThreadName, process_data_phase.tasks[t1].death_thread_name); 1196 EXPECT_EQ(kMainThreadName, process_data_phase.tasks[t1].death_thread_name);
1184 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id); 1197 EXPECT_EQ(base::GetCurrentProcId(), process_data.process_id);
1185 } 1198 }
1186 1199
1200 // Repetitively create and stop named threads. Verify that the number of
1201 // instantiated ThreadData instance is equal to the number of different
1202 // sanitized thread names used in the test.
1203 TEST_F(TrackedObjectsTest, ReuseRetiredThreadData) {
1204 const char* const kThreadNames[] = {"Foo%d", "Bar%d", "123Dummy%d",
1205 "456Dummy%d"};
Nico 2016/11/24 01:42:04 include a thread that has only digits in the name
fdoray 2016/11/29 16:10:33 Done.
1206 constexpr int kNumIterations = 10;
1207 EXPECT_EQ(0, GetNumThreadData());
1208
1209 for (int i = 0; i < kNumIterations; ++i) {
1210 for (const char* thread_name : kThreadNames) {
1211 base::Thread thread(base::StringPrintf(thread_name, i));
1212 EXPECT_TRUE(thread.Start());
1213 }
1214 }
1215
1216 // Expect one ThreadData instance for each element in |kThreadNames| and one
1217 // ThreadData instance for the main thread.
1218 EXPECT_EQ(static_cast<int>(arraysize(kThreadNames) + 1), GetNumThreadData());
1219 }
1220
1187 } // namespace tracked_objects 1221 } // namespace tracked_objects
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698