test_threadsafe_queue.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  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. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <cstdlib>
  21. #include <ctime>
  22. #include <iostream>
  23. #include <memory>
  24. #include <thread>
  25. #include <vector>
  26. #include "cnstream_logging.hpp"
  27. #include "gtest/gtest.h"
  28. #include "util/cnstream_queue.hpp"
  29. namespace cnstream {
  30. std::mutex data_mutex_;
  31. bool flag_[100];
  32. void ThreadFuncPush(ThreadSafeQueue<int>* thread_safe_queue, int data) {
  33. std::lock_guard<std::mutex> lk(data_mutex_);
  34. thread_safe_queue->Push(data);
  35. flag_[data] = true;
  36. // lock_guard<mutex> lk(data_m);
  37. // cout << "--Test Push:" << data++ << endl;
  38. }
  39. void ThreadFuncTryPop(ThreadSafeQueue<int>* thread_safe_queue) {
  40. int value = -1;
  41. bool res = thread_safe_queue->TryPop(value);
  42. std::lock_guard<std::mutex> lk(data_mutex_);
  43. if (res) {
  44. LOGF_IF(COREUNITEST, !flag_[value]) << "Test pop data repeatedly:try_pop error! ";
  45. flag_[value] = false;
  46. }
  47. }
  48. void ThreadFuncWaitAndPop(ThreadSafeQueue<int>* thread_safe_queue) {
  49. int value = -1;
  50. thread_safe_queue->WaitAndPop(value);
  51. std::lock_guard<std::mutex> lk(data_mutex_);
  52. LOGF_IF(COREUNITEST, !flag_[value]) << "Test pop data repeatedly: wait_and_pop error!";
  53. flag_[value] = false;
  54. }
  55. void ThreadFuncWaitAndTryPop(ThreadSafeQueue<int>* thread_safe_queue) {
  56. int value = -1;
  57. bool res = thread_safe_queue->WaitAndTryPop(value, std::chrono::microseconds(50));
  58. std::lock_guard<std::mutex> lk(data_mutex_);
  59. if (res) {
  60. LOGF_IF(COREUNITEST, !flag_[value]) << "Test pop data repeatedly: wait_and_try_pop error!";
  61. flag_[value] = false;
  62. }
  63. }
  64. bool TestThreadsafeQueue() {
  65. ThreadSafeQueue<int> thread_safe_queue;
  66. memset(flag_, 0, sizeof(flag_));
  67. std::thread* threads[100];
  68. int data[100];
  69. for (int i = 0; i < 100; i++) {
  70. data[i] = i;
  71. }
  72. int i = -1;
  73. uint32_t seed = (uint32_t)time(0);
  74. srand(time(nullptr));
  75. LOGI(COREUNITEST) << "Test threadsafe_queue: push and pop!";
  76. while (++i < 40) {
  77. if (i > 20) {
  78. threads[i] = new std::thread(ThreadFuncPush, &thread_safe_queue, data[i]);
  79. } else {
  80. switch (rand_r(&seed) % 4) {
  81. case 0:
  82. threads[i] = new std::thread(ThreadFuncTryPop, &thread_safe_queue);
  83. break;
  84. case 1:
  85. threads[i] = new std::thread(ThreadFuncWaitAndPop, &thread_safe_queue);
  86. break;
  87. case 2:
  88. threads[i] = new std::thread(ThreadFuncWaitAndTryPop, &thread_safe_queue);
  89. break;
  90. case 3:
  91. threads[i] = new std::thread(ThreadFuncPush, &thread_safe_queue, data[i]);
  92. break;
  93. default:
  94. break;
  95. }
  96. }
  97. }
  98. for (int k = 0; k < 40; ++k) {
  99. threads[k]->join();
  100. }
  101. LOGI(COREUNITEST) << "Test threadsafe_queue: blocking";
  102. i--;
  103. while (++i < 70) {
  104. if (i < 55) {
  105. switch (rand_r(&seed) % 2) {
  106. case 0:
  107. threads[i] = new std::thread(ThreadFuncWaitAndPop, &thread_safe_queue);
  108. break;
  109. case 1:
  110. threads[i] = new std::thread(ThreadFuncWaitAndTryPop, &thread_safe_queue);
  111. break;
  112. default:
  113. break;
  114. }
  115. } else {
  116. threads[i] = new std::thread(ThreadFuncPush, &thread_safe_queue, data[i]);
  117. }
  118. }
  119. for (int k = 40; k < 70; ++k) {
  120. threads[k]->join();
  121. }
  122. for (int k = 0; k < 70; ++k) {
  123. delete threads[k];
  124. }
  125. return true;
  126. }
  127. TEST(CoreThreadSafeQueue, ThreadsafeQueue) { EXPECT_EQ(true, TestThreadsafeQueue()); }
  128. } // namespace cnstream