blob: dfad4e86f36b4dc7a2703209dce54e36494cb130 [file] [log] [blame]
license.botf003cfe2008-08-24 09:55:55 +09001// Copyright (c) 2006-2008 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.
initial.commit3f4a7322008-07-27 06:49:38 +09004
5#include <process.h> // _beginthreadex
6
7#include "base/lock.h"
8#include "base/scoped_ptr.h"
9#include "base/shared_event.h"
10#include "testing/gtest/include/gtest/gtest.h"
11
12namespace {
13
14class SharedEventTest : public testing::Test {
15};
16
17Lock lock;
18
19unsigned __stdcall MultipleThreadMain(void* param) {
20 SharedEvent* shared_event = reinterpret_cast<SharedEvent*>(param);
21 AutoLock l(lock);
22 shared_event->SetSignaledState(!shared_event->IsSignaled());
23 return 0;
24}
25
26}
27
28TEST(SharedEventTest, ThreadSignaling) {
29 // Create a set of 5 threads to each open a shared event and flip the
30 // signaled state. Verify that when the threads complete, the final state is
31 // not-signaled.
32 // I admit this doesn't test much, but short of spawning separate processes
33 // and using IPC with a SharedEventHandle, there's not much to unittest.
34 const int kNumThreads = 5;
35 HANDLE threads[kNumThreads];
36
37 scoped_ptr<SharedEvent> shared_event(new SharedEvent);
38 shared_event->Create(true, true);
39
40 // Spawn the threads.
41 for (int16 index = 0; index < kNumThreads; index++) {
42 void *argument = reinterpret_cast<void*>(shared_event.get());
43 unsigned thread_id;
44 threads[index] = reinterpret_cast<HANDLE>(
45 _beginthreadex(NULL, 0, MultipleThreadMain, argument, 0, &thread_id));
46 EXPECT_NE(threads[index], static_cast<HANDLE>(NULL));
47 }
48
49 // Wait for the threads to finish.
50 for (int index = 0; index < kNumThreads; index++) {
51 DWORD rv = WaitForSingleObject(threads[index], 60*1000);
52 EXPECT_EQ(rv, WAIT_OBJECT_0); // verify all threads finished
53 CloseHandle(threads[index]);
54 }
55 EXPECT_FALSE(shared_event->IsSignaled());
56}
license.botf003cfe2008-08-24 09:55:55 +090057