Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 1 | /* |
Josh Gao | c285cd4 | 2016-02-12 11:33:53 -0800 | [diff] [blame^] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Josh Gao | a4f5e03 | 2016-02-09 14:59:09 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <gtest/gtest.h> |
| 18 | #include <unistd.h> |
| 19 | #include <atomic> |
| 20 | |
| 21 | #include "sysdeps.h" |
| 22 | |
| 23 | static void* increment_atomic_int(void* c) { |
| 24 | sleep(1); |
| 25 | reinterpret_cast<std::atomic<int>*>(c)->fetch_add(1); |
| 26 | return nullptr; |
| 27 | } |
| 28 | |
| 29 | TEST(sysdeps_thread, smoke) { |
| 30 | std::atomic<int> counter(0); |
| 31 | |
| 32 | for (int i = 0; i < 100; ++i) { |
| 33 | ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter)); |
| 34 | } |
| 35 | |
| 36 | sleep(2); |
| 37 | ASSERT_EQ(100, counter.load()); |
| 38 | } |
| 39 | |
| 40 | TEST(sysdeps_thread, join) { |
| 41 | std::atomic<int> counter(0); |
| 42 | std::vector<adb_thread_t> threads(500); |
| 43 | for (size_t i = 0; i < threads.size(); ++i) { |
| 44 | ASSERT_TRUE(adb_thread_create(increment_atomic_int, &counter, &threads[i])); |
| 45 | } |
| 46 | |
| 47 | int current = counter.load(); |
| 48 | ASSERT_GE(current, 0); |
| 49 | // Make sure that adb_thread_create actually creates threads, and doesn't do something silly |
| 50 | // like synchronously run the function passed in. The sleep in increment_atomic_int should be |
| 51 | // enough to keep this from being flakey. |
| 52 | ASSERT_LT(current, 500); |
| 53 | |
| 54 | for (const auto& thread : threads) { |
| 55 | ASSERT_TRUE(adb_thread_join(thread)); |
| 56 | } |
| 57 | |
| 58 | ASSERT_EQ(500, counter.load()); |
| 59 | } |