OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "mojo/public/cpp/utility/thread.h" | |
6 | |
7 #include "mojo/public/cpp/system/macros.h" | |
8 #include "testing/gtest/include/gtest/gtest.h" | |
9 | |
10 namespace mojo { | |
11 namespace { | |
12 | |
13 class SetIntThread : public Thread { | |
14 public: | |
15 SetIntThread(int* int_to_set, int value) | |
16 : int_to_set_(int_to_set), | |
17 value_(value) { | |
18 } | |
19 SetIntThread(const Options& options, int* int_to_set, int value) | |
20 : Thread(options), | |
21 int_to_set_(int_to_set), | |
22 value_(value) { | |
23 } | |
24 | |
25 ~SetIntThread() override {} | |
26 | |
27 void Run() override { *int_to_set_ = value_; } | |
28 | |
29 private: | |
30 int* const int_to_set_; | |
31 const int value_; | |
32 | |
33 MOJO_DISALLOW_COPY_AND_ASSIGN(SetIntThread); | |
34 }; | |
35 | |
36 TEST(ThreadTest, CreateAndJoin) { | |
37 int value = 0; | |
38 | |
39 // Not starting the thread should result in a no-op. | |
40 { | |
41 SetIntThread thread(&value, 1234567); | |
42 } | |
43 EXPECT_EQ(0, value); | |
44 | |
45 // Start and join. | |
46 { | |
47 SetIntThread thread(&value, 12345678); | |
48 thread.Start(); | |
49 thread.Join(); | |
50 EXPECT_EQ(12345678, value); | |
51 } | |
52 | |
53 // Ditto, with non-default (but reasonable) stack size. | |
54 { | |
55 Thread::Options options; | |
56 options.set_stack_size(1024 * 1024); // 1 MB. | |
57 SetIntThread thread(options, &value, 12345678); | |
58 thread.Start(); | |
59 thread.Join(); | |
60 EXPECT_EQ(12345678, value); | |
61 } | |
62 } | |
63 | |
64 // Tests of assertions for Debug builds. | |
65 // Note: It's okay to create threads, despite gtest having to fork. (The threads | |
66 // are in the child process.) | |
67 #if !defined(NDEBUG) | |
68 TEST(ThreadTest, DebugAssertionFailures) { | |
69 // Can only start once. | |
70 EXPECT_DEATH_IF_SUPPORTED({ | |
71 int value = 0; | |
72 SetIntThread thread(&value, 1); | |
73 thread.Start(); | |
74 thread.Start(); | |
75 }, ""); | |
76 | |
77 // Must join (if you start). | |
78 EXPECT_DEATH_IF_SUPPORTED({ | |
79 int value = 0; | |
80 SetIntThread thread(&value, 2); | |
81 thread.Start(); | |
82 }, ""); | |
83 | |
84 // Can only join once. | |
85 EXPECT_DEATH_IF_SUPPORTED({ | |
86 int value = 0; | |
87 SetIntThread thread(&value, 3); | |
88 thread.Start(); | |
89 thread.Join(); | |
90 thread.Join(); | |
91 }, ""); | |
92 | |
93 // Stack too big (we're making certain assumptions here). | |
94 EXPECT_DEATH_IF_SUPPORTED({ | |
95 int value = 0; | |
96 Thread::Options options; | |
97 options.set_stack_size(static_cast<size_t>(-1)); | |
98 SetIntThread thread(options, &value, 4); | |
99 thread.Start(); | |
100 thread.Join(); | |
101 }, ""); | |
102 } | |
103 #endif // !defined(NDEBUG) | |
104 | |
105 } // namespace | |
106 } // namespace mojo | |
OLD | NEW |