1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // http://code.google.com/p/protobuf/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // Author: kenton@google.com (Kenton Varda)
40 #include <google/protobuf/stubs/once.h>
41 #include <google/protobuf/testing/googletest.h>
42 #include <gtest/gtest.h>
48 class OnceInitTest : public testing::Test {
51 state_ = INIT_NOT_STARTED;
55 // Since ProtobufOnceType is only allowed to be allocated in static storage,
56 // each test must use a different pair of ProtobufOnceType objects which it
57 // must declare itself.
58 void SetOnces(ProtobufOnceType* once, ProtobufOnceType* recursive_once) {
60 recursive_once_ = recursive_once;
64 GoogleOnceInit(once_, &InitStatic);
66 void InitRecursiveOnce() {
67 GoogleOnceInit(recursive_once_, &InitRecursiveStatic);
70 void BlockInit() { init_blocker_.Lock(); }
71 void UnblockInit() { init_blocker_.Unlock(); }
75 TestThread(Closure* callback)
76 : done_(false), joined_(false), callback_(callback) {
78 thread_ = CreateThread(NULL, 0, &Start, this, 0, NULL);
80 pthread_create(&thread_, NULL, &Start, this);
88 MutexLock lock(&done_mutex_);
94 WaitForSingleObject(thread_, INFINITE);
97 pthread_join(thread_, NULL);
114 static DWORD WINAPI Start(LPVOID arg) {
116 static void* Start(void* arg) {
118 reinterpret_cast<TestThread*>(arg)->Run();
124 MutexLock lock(&done_mutex_);
129 TestThread* RunInitOnceInNewThread() {
130 return new TestThread(NewCallback(this, &OnceInitTest::InitOnce));
132 TestThread* RunInitRecursiveOnceInNewThread() {
133 return new TestThread(NewCallback(this, &OnceInitTest::InitRecursiveOnce));
141 State CurrentState() {
142 MutexLock lock(&mutex_);
158 ProtobufOnceType* once_;
159 ProtobufOnceType* recursive_once_;
162 MutexLock lock(&mutex_);
163 EXPECT_EQ(INIT_NOT_STARTED, state_);
164 state_ = INIT_STARTED;
166 init_blocker_.Lock();
167 init_blocker_.Unlock();
172 static OnceInitTest* current_test_;
173 static void InitStatic() { current_test_->Init(); }
174 static void InitRecursiveStatic() { current_test_->InitOnce(); }
177 OnceInitTest* OnceInitTest::current_test_ = NULL;
179 GOOGLE_PROTOBUF_DECLARE_ONCE(simple_once);
181 TEST_F(OnceInitTest, Simple) {
182 SetOnces(&simple_once, NULL);
184 EXPECT_EQ(INIT_NOT_STARTED, CurrentState());
186 EXPECT_EQ(INIT_DONE, CurrentState());
188 // Calling again has no effect.
190 EXPECT_EQ(INIT_DONE, CurrentState());
193 GOOGLE_PROTOBUF_DECLARE_ONCE(recursive_once1);
194 GOOGLE_PROTOBUF_DECLARE_ONCE(recursive_once2);
196 TEST_F(OnceInitTest, Recursive) {
197 SetOnces(&recursive_once1, &recursive_once2);
199 EXPECT_EQ(INIT_NOT_STARTED, CurrentState());
201 EXPECT_EQ(INIT_DONE, CurrentState());
204 GOOGLE_PROTOBUF_DECLARE_ONCE(multiple_threads_once);
206 TEST_F(OnceInitTest, MultipleThreads) {
207 SetOnces(&multiple_threads_once, NULL);
209 scoped_ptr<TestThread> threads[4];
210 EXPECT_EQ(INIT_NOT_STARTED, CurrentState());
211 for (int i = 0; i < 4; i++) {
212 threads[i].reset(RunInitOnceInNewThread());
214 for (int i = 0; i < 4; i++) {
217 EXPECT_EQ(INIT_DONE, CurrentState());
220 GOOGLE_PROTOBUF_DECLARE_ONCE(multiple_threads_blocked_once1);
221 GOOGLE_PROTOBUF_DECLARE_ONCE(multiple_threads_blocked_once2);
223 TEST_F(OnceInitTest, MultipleThreadsBlocked) {
224 SetOnces(&multiple_threads_blocked_once1, &multiple_threads_blocked_once2);
226 scoped_ptr<TestThread> threads[8];
227 EXPECT_EQ(INIT_NOT_STARTED, CurrentState());
230 for (int i = 0; i < 4; i++) {
231 threads[i].reset(RunInitOnceInNewThread());
233 for (int i = 4; i < 8; i++) {
234 threads[i].reset(RunInitRecursiveOnceInNewThread());
239 // We should now have one thread blocked inside Init(), four blocked waiting
240 // for Init() to complete, and three blocked waiting for InitRecursive() to
242 EXPECT_EQ(INIT_STARTED, CurrentState());
245 for (int i = 0; i < 8; i++) {
248 EXPECT_EQ(INIT_DONE, CurrentState());
251 } // anonymous namespace
252 } // namespace protobuf
253 } // namespace google