MCPcopy Create free account
hub / github.com/apache/brpc / TEST

Function TEST

test/thread_local_unittest.cc:74–148  ·  view source on GitHub ↗

In this test, we start 2 threads which will access a ThreadLocalPointer. We make sure the default is NULL, and the pointers are unique to the threads.

Source from the content-addressed store, hash-verified

72// In this test, we start 2 threads which will access a ThreadLocalPointer. We
73// make sure the default is NULL, and the pointers are unique to the threads.
74TEST(ThreadLocalTest, Pointer) {
75 butil::DelegateSimpleThreadPool tp1("ThreadLocalTest tp1", 1);
76 butil::DelegateSimpleThreadPool tp2("ThreadLocalTest tp1", 1);
77 tp1.Start();
78 tp2.Start();
79
80 butil::ThreadLocalPointer<ThreadLocalTesterBase> tlp;
81
82 static ThreadLocalTesterBase* const kBogusPointer =
83 reinterpret_cast<ThreadLocalTesterBase*>(0x1234);
84
85 ThreadLocalTesterBase* tls_val;
86 butil::WaitableEvent done(true, false);
87
88 GetThreadLocal getter(&tlp, &done);
89 getter.set_ptr(&tls_val);
90
91 // Check that both threads defaulted to NULL.
92 tls_val = kBogusPointer;
93 done.Reset();
94 tp1.AddWork(&getter);
95 done.Wait();
96 EXPECT_EQ(static_cast<ThreadLocalTesterBase*>(NULL), tls_val);
97
98 tls_val = kBogusPointer;
99 done.Reset();
100 tp2.AddWork(&getter);
101 done.Wait();
102 EXPECT_EQ(static_cast<ThreadLocalTesterBase*>(NULL), tls_val);
103
104
105 SetThreadLocal setter(&tlp, &done);
106 setter.set_value(kBogusPointer);
107
108 // Have thread 1 set their pointer value to kBogusPointer.
109 done.Reset();
110 tp1.AddWork(&setter);
111 done.Wait();
112
113 tls_val = NULL;
114 done.Reset();
115 tp1.AddWork(&getter);
116 done.Wait();
117 EXPECT_EQ(kBogusPointer, tls_val);
118
119 // Make sure thread 2 is still NULL
120 tls_val = kBogusPointer;
121 done.Reset();
122 tp2.AddWork(&getter);
123 done.Wait();
124 EXPECT_EQ(static_cast<ThreadLocalTesterBase*>(NULL), tls_val);
125
126 // Set thread 2 to kBogusPointer + 1.
127 setter.set_value(kBogusPointer + 1);
128
129 done.Reset();
130 tp2.AddWork(&setter);
131 done.Wait();

Callers

nothing calls this directly

Calls 9

set_ptrMethod · 0.80
AddWorkMethod · 0.80
JoinAllMethod · 0.80
StartMethod · 0.45
ResetMethod · 0.45
WaitMethod · 0.45
set_valueMethod · 0.45
GetMethod · 0.45
SetMethod · 0.45

Tested by

no test coverage detected