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.
5 #include "base/threading/thread_id_name_manager.h"
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"
12 typedef PlatformTest ThreadIdNameManagerTest
;
16 const char kAThread
[] = "a thread";
17 const char kBThread
[] = "b thread";
19 TEST_F(ThreadIdNameManagerTest
, AddThreads
) {
20 base::ThreadIdNameManager
* manager
= base::ThreadIdNameManager::GetInstance();
21 base::Thread
thread_a(kAThread
);
22 base::Thread
thread_b(kBThread
);
24 thread_a
.StartAndWaitForTesting();
25 thread_b
.StartAndWaitForTesting();
27 EXPECT_STREQ(kAThread
, manager
->GetName(thread_a
.GetThreadId()));
28 EXPECT_STREQ(kBThread
, manager
->GetName(thread_b
.GetThreadId()));
34 TEST_F(ThreadIdNameManagerTest
, RemoveThreads
) {
35 base::ThreadIdNameManager
* manager
= base::ThreadIdNameManager::GetInstance();
36 base::Thread
thread_a(kAThread
);
38 thread_a
.StartAndWaitForTesting();
40 base::Thread
thread_b(kBThread
);
41 thread_b
.StartAndWaitForTesting();
44 EXPECT_STREQ(kAThread
, manager
->GetName(thread_a
.GetThreadId()));
47 EXPECT_STREQ("", manager
->GetName(thread_a
.GetThreadId()));
50 TEST_F(ThreadIdNameManagerTest
, RestartThread
) {
51 base::ThreadIdNameManager
* manager
= base::ThreadIdNameManager::GetInstance();
52 base::Thread
thread_a(kAThread
);
54 thread_a
.StartAndWaitForTesting();
55 base::PlatformThreadId a_id
= thread_a
.GetThreadId();
56 EXPECT_STREQ(kAThread
, manager
->GetName(a_id
));
59 thread_a
.StartAndWaitForTesting();
60 EXPECT_STREQ("", manager
->GetName(a_id
));
61 EXPECT_STREQ(kAThread
, manager
->GetName(thread_a
.GetThreadId()));
65 TEST_F(ThreadIdNameManagerTest
, ThreadNameInterning
) {
66 base::ThreadIdNameManager
* manager
= base::ThreadIdNameManager::GetInstance();
68 base::PlatformThreadId a_id
= base::PlatformThread::CurrentId();
69 base::PlatformThread::SetName("First Name");
70 std::string version
= manager
->GetName(a_id
);
72 base::PlatformThread::SetName("New name");
73 EXPECT_NE(version
, manager
->GetName(a_id
));
74 base::PlatformThread::SetName("");
77 TEST_F(ThreadIdNameManagerTest
, ResettingNameKeepsCorrectInternedValue
) {
78 base::ThreadIdNameManager
* manager
= base::ThreadIdNameManager::GetInstance();
80 base::PlatformThreadId a_id
= base::PlatformThread::CurrentId();
81 base::PlatformThread::SetName("Test Name");
82 std::string version
= manager
->GetName(a_id
);
84 base::PlatformThread::SetName("New name");
85 EXPECT_NE(version
, manager
->GetName(a_id
));
87 base::PlatformThread::SetName("Test Name");
88 EXPECT_EQ(version
, manager
->GetName(a_id
));
90 base::PlatformThread::SetName("");