dap_test.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. #include "dap/dap.h"
  15. #include "gmock/gmock.h"
  16. #include "gtest/gtest.h"
  17. #include <condition_variable>
  18. #include <mutex>
  19. #include <thread>
  20. #include <vector>
  21. int main(int argc, char** argv) {
  22. ::testing::InitGoogleTest(&argc, argv);
  23. return RUN_ALL_TESTS();
  24. }
  25. TEST(DAP, PairedInitializeTerminate) {
  26. dap::initialize();
  27. dap::terminate();
  28. }
  29. TEST(DAP, NestedInitializeTerminate) {
  30. dap::initialize();
  31. dap::initialize();
  32. dap::initialize();
  33. dap::terminate();
  34. dap::terminate();
  35. dap::terminate();
  36. }
  37. TEST(DAP, MultiThreadedInitializeTerminate) {
  38. const size_t numThreads = 64;
  39. std::mutex mutex;
  40. std::condition_variable cv;
  41. size_t numInits = 0;
  42. std::vector<std::thread> threads;
  43. threads.reserve(numThreads);
  44. for (size_t i = 0; i < numThreads; i++) {
  45. threads.emplace_back([&] {
  46. dap::initialize();
  47. {
  48. std::unique_lock<std::mutex> lock(mutex);
  49. numInits++;
  50. if (numInits == numThreads) {
  51. cv.notify_all();
  52. } else {
  53. cv.wait(lock, [&] { return numInits == numThreads; });
  54. }
  55. }
  56. dap::terminate();
  57. });
  58. }
  59. for (auto& thread : threads) {
  60. thread.join();
  61. }
  62. }