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