1 // Copyright 2014 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 "mojo/public/cpp/utility/thread.h"
7 #include "mojo/public/cpp/system/macros.h"
8 #include "testing/gtest/include/gtest/gtest.h"
13 class SetIntThread
: public Thread
{
15 SetIntThread(int* int_to_set
, int value
)
16 : int_to_set_(int_to_set
),
19 SetIntThread(const Options
& options
, int* int_to_set
, int value
)
21 int_to_set_(int_to_set
),
25 virtual ~SetIntThread() {
28 virtual void Run() MOJO_OVERRIDE
{
29 *int_to_set_
= value_
;
33 int* const int_to_set_
;
36 MOJO_DISALLOW_COPY_AND_ASSIGN(SetIntThread
);
39 TEST(ThreadTest
, CreateAndJoin
) {
42 // Not starting the thread should result in a no-op.
44 SetIntThread
thread(&value
, 1234567);
50 SetIntThread
thread(&value
, 12345678);
53 EXPECT_EQ(12345678, value
);
56 // Ditto, with non-default (but reasonable) stack size.
58 Thread::Options options
;
59 options
.set_stack_size(1024 * 1024); // 1 MB.
60 SetIntThread
thread(options
, &value
, 12345678);
63 EXPECT_EQ(12345678, value
);
67 // Tests of assertions for Debug builds.
68 // Note: It's okay to create threads, despite gtest having to fork. (The threads
69 // are in the child process.)
71 TEST(ThreadTest
, DebugAssertionFailures
) {
72 // Can only start once.
73 EXPECT_DEATH_IF_SUPPORTED({
75 SetIntThread
thread(&value
, 1);
80 // Must join (if you start).
81 EXPECT_DEATH_IF_SUPPORTED({
83 SetIntThread
thread(&value
, 2);
87 // Can only join once.
88 EXPECT_DEATH_IF_SUPPORTED({
90 SetIntThread
thread(&value
, 3);
96 // Stack too big (we're making certain assumptions here).
97 EXPECT_DEATH_IF_SUPPORTED({
99 Thread::Options options
;
100 options
.set_stack_size(static_cast<size_t>(-1));
101 SetIntThread
thread(options
, &value
, 4);
106 #endif // !defined(NDEBUG)