| 1 |
// Copyright 2019 Google LLC |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "dap/dap.h" |
| 16 |
|
| 17 |
#include "gmock/gmock.h" |
| 18 |
#include "gtest/gtest.h" |
| 19 |
|
| 20 |
#include <condition_variable> |
| 21 |
#include <mutex> |
| 22 |
#include <thread> |
| 23 |
#include <vector> |
| 24 |
|
| 25 |
int main(int argc, char** argv) { |
| 26 |
::testing::InitGoogleTest(&argc, argv); |
| 27 |
return RUN_ALL_TESTS(); |
| 28 |
} |
| 29 |
|
| 30 |
TEST(DAP, PairedInitializeTerminate) { |
| 31 |
dap::initialize(); |
| 32 |
dap::terminate(); |
| 33 |
} |
| 34 |
|
| 35 |
TEST(DAP, NestedInitializeTerminate) { |
| 36 |
dap::initialize(); |
| 37 |
dap::initialize(); |
| 38 |
dap::initialize(); |
| 39 |
dap::terminate(); |
| 40 |
dap::terminate(); |
| 41 |
dap::terminate(); |
| 42 |
} |
| 43 |
|
| 44 |
TEST(DAP, MultiThreadedInitializeTerminate) { |
| 45 |
const size_t numThreads = 64; |
| 46 |
|
| 47 |
std::mutex mutex; |
| 48 |
std::condition_variable cv; |
| 49 |
size_t numInits = 0; |
| 50 |
|
| 51 |
std::vector<std::thread> threads; |
| 52 |
threads.reserve(numThreads); |
| 53 |
for (size_t i = 0; i < numThreads; i++) { |
| 54 |
threads.emplace_back([&] { |
| 55 |
dap::initialize(); |
| 56 |
{ |
| 57 |
std::unique_lock<std::mutex> lock(mutex); |
| 58 |
numInits++; |
| 59 |
if (numInits == numThreads) { |
| 60 |
cv.notify_all(); |
| 61 |
} else { |
| 62 |
cv.wait(lock, [&] { return numInits == numThreads; }); |
| 63 |
} |
| 64 |
} |
| 65 |
dap::terminate(); |
| 66 |
}); |
| 67 |
} |
| 68 |
|
| 69 |
for (auto& thread : threads) { |
| 70 |
thread.join(); |
| 71 |
} |
| 72 |
} |