OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "base/threading/thread_id_name_manager.h" |
| 6 |
| 7 #include "base/threading/platform_thread.h" |
| 8 #include "base/threading/thread.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "testing/platform_test.h" |
| 11 |
| 12 typedef PlatformTest ThreadIdNameManagerTest; |
| 13 |
| 14 namespace { |
| 15 |
| 16 static const char* kAThread = "a thread"; |
| 17 static const char* kBThread = "b thread"; |
| 18 |
| 19 TEST_F(ThreadIdNameManagerTest, AddThreads) { |
| 20 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); |
| 21 base::Thread thread_a(kAThread); |
| 22 base::Thread thread_b(kBThread); |
| 23 |
| 24 thread_a.Start(); |
| 25 thread_b.Start(); |
| 26 |
| 27 EXPECT_NE(manager->GetInternedName(thread_a.thread_id()), |
| 28 manager->GetInternedName(thread_b.thread_id())); |
| 29 |
| 30 EXPECT_STREQ(kAThread, manager->GetNameForId(thread_a.thread_id())); |
| 31 EXPECT_STREQ(kBThread, manager->GetNameForId(thread_b.thread_id())); |
| 32 |
| 33 thread_b.Stop(); |
| 34 thread_a.Stop(); |
| 35 } |
| 36 |
| 37 TEST_F(ThreadIdNameManagerTest, RemoveThreads) { |
| 38 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); |
| 39 base::Thread thread_a(kAThread); |
| 40 |
| 41 thread_a.Start(); |
| 42 base::PlatformThreadId b_id; |
| 43 { |
| 44 base::Thread thread_b(kBThread); |
| 45 thread_b.Start(); |
| 46 b_id = thread_b.thread_id(); |
| 47 thread_b.Stop(); |
| 48 } |
| 49 thread_a.Stop(); |
| 50 |
| 51 EXPECT_STREQ(kAThread, manager->GetNameForId(thread_a.thread_id())); |
| 52 EXPECT_EQ(NULL, manager->GetNameForId(b_id)); |
| 53 } |
| 54 |
| 55 TEST_F(ThreadIdNameManagerTest, RestartThread) { |
| 56 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); |
| 57 base::Thread thread_a(kAThread); |
| 58 |
| 59 thread_a.Start(); |
| 60 base::PlatformThreadId a_id = thread_a.thread_id(); |
| 61 thread_a.Stop(); |
| 62 EXPECT_STREQ(kAThread, manager->GetNameForId(a_id)); |
| 63 |
| 64 thread_a.Start(); |
| 65 thread_a.Stop(); |
| 66 EXPECT_STREQ(kAThread, manager->GetNameForId(a_id)); |
| 67 } |
| 68 |
| 69 TEST_F(ThreadIdNameManagerTest, ThreadNameInterning) { |
| 70 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); |
| 71 |
| 72 base::PlatformThreadId a_id = base::PlatformThread::CurrentId(); |
| 73 base::InternedString version = manager->GetInternedName(a_id); |
| 74 |
| 75 base::PlatformThread::SetName("New name"); |
| 76 EXPECT_NE(version, manager->GetInternedName(a_id)); |
| 77 base::PlatformThread::SetName(""); |
| 78 } |
| 79 |
| 80 TEST_F(ThreadIdNameManagerTest, ResettingNameKeepsCorrectInternedValue) { |
| 81 base::ThreadIdNameManager* manager = base::ThreadIdNameManager::GetInstance(); |
| 82 |
| 83 base::PlatformThreadId a_id = base::PlatformThread::CurrentId(); |
| 84 base::PlatformThread::SetName("Test Name"); |
| 85 base::InternedString version = manager->GetInternedName(a_id); |
| 86 |
| 87 base::PlatformThread::SetName("New name"); |
| 88 EXPECT_NE(version, manager->GetInternedName(a_id)); |
| 89 |
| 90 base::PlatformThread::SetName("Test Name"); |
| 91 EXPECT_EQ(version, manager->GetInternedName(a_id)); |
| 92 |
| 93 base::PlatformThread::SetName(""); |
| 94 } |
| 95 |
| 96 } // namespace |
OLD | NEW |