blob: daceb89b957b18a78646602c2c0d086237b2d11c [file] [log] [blame]
Josh Gaoa4f5e032016-02-09 14:59:09 -08001/*
Josh Gaoc285cd42016-02-12 11:33:53 -08002 * Copyright (C) 2016 The Android Open Source Project
Josh Gaoa4f5e032016-02-09 14:59:09 -08003 *
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
23static void* increment_atomic_int(void* c) {
24 sleep(1);
25 reinterpret_cast<std::atomic<int>*>(c)->fetch_add(1);
26 return nullptr;
27}
28
29TEST(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
40TEST(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}