OLD | NEW |
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 #include "base/threading/thread_id_name_manager.h" | 5 #include "base/threading/thread_id_name_manager.h" |
6 | 6 |
| 7 #include "base/synchronization/waitable_event.h" |
7 #include "base/threading/platform_thread.h" | 8 #include "base/threading/platform_thread.h" |
8 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
9 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
10 #include "testing/platform_test.h" | 11 #include "testing/platform_test.h" |
11 | 12 |
12 typedef PlatformTest ThreadIdNameManagerTest; | 13 typedef PlatformTest ThreadIdNameManagerTest; |
13 | 14 |
14 namespace { | 15 namespace { |
15 | 16 |
16 const char kAThread[] = "a thread"; | 17 const char kAThread[] = "a thread"; |
17 const char kBThread[] = "b thread"; | 18 const char kBThread[] = "b thread"; |
18 | 19 |
19 TEST_F(ThreadIdNameManagerTest, AddThreads) { | 20 TEST_F(ThreadIdNameManagerTest, AddThreads) { |
20 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); | 21 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); |
21 base::Thread thread_a(kAThread); | 22 base::Thread thread_a(kAThread); |
22 base::Thread thread_b(kBThread); | 23 base::Thread thread_b(kBThread); |
23 | 24 |
24 thread_a.Start(); | 25 thread_a.StartAndWait(); |
25 thread_b.Start(); | 26 thread_b.StartAndWait(); |
26 | 27 |
27 EXPECT_STREQ(kAThread, manager->GetName(thread_a.thread_id())); | 28 EXPECT_STREQ(kAThread, manager->GetName(thread_a.thread_id())); |
28 EXPECT_STREQ(kBThread, manager->GetName(thread_b.thread_id())); | 29 EXPECT_STREQ(kBThread, manager->GetName(thread_b.thread_id())); |
29 | 30 |
30 thread_b.Stop(); | 31 thread_b.Stop(); |
31 thread_a.Stop(); | 32 thread_a.Stop(); |
32 } | 33 } |
33 | 34 |
34 TEST_F(ThreadIdNameManagerTest, RemoveThreads) { | 35 TEST_F(ThreadIdNameManagerTest, RemoveThreads) { |
35 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); | 36 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); |
36 base::Thread thread_a(kAThread); | 37 base::Thread thread_a(kAThread); |
37 | 38 |
38 thread_a.Start(); | 39 thread_a.StartAndWait(); |
39 { | 40 { |
40 base::Thread thread_b(kBThread); | 41 base::Thread thread_b(kBThread); |
41 thread_b.Start(); | 42 thread_b.StartAndWait(); |
42 thread_b.Stop(); | 43 thread_b.Stop(); |
43 } | 44 } |
44 EXPECT_STREQ(kAThread, manager->GetName(thread_a.thread_id())); | 45 EXPECT_STREQ(kAThread, manager->GetName(thread_a.thread_id())); |
45 | 46 |
46 thread_a.Stop(); | 47 thread_a.Stop(); |
47 EXPECT_STREQ("", manager->GetName(thread_a.thread_id())); | 48 EXPECT_STREQ("", manager->GetName(thread_a.thread_id())); |
48 } | 49 } |
49 | 50 |
50 TEST_F(ThreadIdNameManagerTest, RestartThread) { | 51 TEST_F(ThreadIdNameManagerTest, RestartThread) { |
51 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); | 52 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); |
52 base::Thread thread_a(kAThread); | 53 base::Thread thread_a(kAThread); |
53 | 54 |
54 thread_a.Start(); | 55 thread_a.StartAndWait(); |
55 base::PlatformThreadId a_id = thread_a.thread_id(); | 56 base::PlatformThreadId a_id = thread_a.thread_id(); |
56 EXPECT_STREQ(kAThread, manager->GetName(a_id)); | 57 EXPECT_STREQ(kAThread, manager->GetName(a_id)); |
57 thread_a.Stop(); | 58 thread_a.Stop(); |
58 | 59 |
59 thread_a.Start(); | 60 thread_a.StartAndWait(); |
60 EXPECT_STREQ("", manager->GetName(a_id)); | 61 EXPECT_STREQ("", manager->GetName(a_id)); |
61 EXPECT_STREQ(kAThread, manager->GetName(thread_a.thread_id())); | 62 EXPECT_STREQ(kAThread, manager->GetName(thread_a.thread_id())); |
62 thread_a.Stop(); | 63 thread_a.Stop(); |
63 } | 64 } |
64 | 65 |
65 TEST_F(ThreadIdNameManagerTest, ThreadNameInterning) { | 66 TEST_F(ThreadIdNameManagerTest, ThreadNameInterning) { |
66 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); | 67 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); |
67 | 68 |
68 base::PlatformThreadId a_id = base::PlatformThread::CurrentId(); | 69 base::PlatformThreadId a_id = base::PlatformThread::CurrentId(); |
69 base::PlatformThread::SetName("First Name"); | 70 base::PlatformThread::SetName("First Name"); |
(...skipping 14 matching lines...) Expand all Loading... |
84 base::PlatformThread::SetName("New name"); | 85 base::PlatformThread::SetName("New name"); |
85 EXPECT_NE(version, manager->GetName(a_id)); | 86 EXPECT_NE(version, manager->GetName(a_id)); |
86 | 87 |
87 base::PlatformThread::SetName("Test Name"); | 88 base::PlatformThread::SetName("Test Name"); |
88 EXPECT_EQ(version, manager->GetName(a_id)); | 89 EXPECT_EQ(version, manager->GetName(a_id)); |
89 | 90 |
90 base::PlatformThread::SetName(""); | 91 base::PlatformThread::SetName(""); |
91 } | 92 } |
92 | 93 |
93 } // namespace | 94 } // namespace |
OLD | NEW |