test_conveyor.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 "cnstream_logging.hpp"
  21. #include <chrono>
  22. #include <ctime>
  23. #include <memory>
  24. #include <thread>
  25. #include <vector>
  26. #include "gtest/gtest.h"
  27. #include "conveyor.hpp"
  28. namespace cnstream {
  29. uint32_t seed_conveyor;
  30. int flag[80];
  31. const char* kind[80];
  32. void ThreadFuncPushDataBuf(Conveyor* conveyor, CNFrameInfoPtr data, int id) {
  33. kind[id] = "pushDataBuf";
  34. conveyor->PushDataBuffer(data);
  35. flag[id]++;
  36. }
  37. void ThreadFuncPopDataBuf(Conveyor* conveyor, int id) {
  38. kind[id] = "popdataBuf";
  39. conveyor->PopDataBuffer();
  40. flag[id]++;
  41. }
  42. void ThreadFuncState(int id) {
  43. int cnt = 0;
  44. while (1) {
  45. bool f = true;
  46. std::this_thread::sleep_for(std::chrono::duration<double, std::ratio<1>>(1));
  47. for (int i = 0; i < id; i++) {
  48. if (flag[i] == 0) {
  49. if (cnt > 10) {
  50. LOGF(COREUNITEST) << kind[id] << " is blocking! Thread: " << i << " is not end!" << std::endl;
  51. }
  52. std::cout << kind[id] << " is blocking! Thread: " << i << " is not end!" << std::endl;
  53. f = false;
  54. }
  55. }
  56. if (f) break;
  57. cnt++;
  58. }
  59. }
  60. TEST(CoreConveyor, MultiThreadPushPop) {
  61. int data_buf_num = 0;
  62. std::thread* thread_ids[80];
  63. memset(flag, 0, sizeof(flag));
  64. Conveyor conveyor(20);
  65. CNFrameInfoPtr data = CNFrameInfo::Create(std::to_string(0));
  66. int id = 0;
  67. seed_conveyor = (uint32_t)time(0);
  68. srand(time(nullptr));
  69. while (id < 30) {
  70. switch (rand_r(&seed_conveyor) % 2) {
  71. case 0:
  72. thread_ids[id] = new std::thread(ThreadFuncPushDataBuf, &conveyor, data, id);
  73. data_buf_num++;
  74. break;
  75. case 1:
  76. thread_ids[id] = new std::thread(ThreadFuncPopDataBuf, &conveyor, id);
  77. data_buf_num--;
  78. break;
  79. default:
  80. break;
  81. }
  82. id++;
  83. }
  84. while (data_buf_num < 0) {
  85. thread_ids[id] = new std::thread(ThreadFuncPushDataBuf, &conveyor, data, id);
  86. data_buf_num++;
  87. id++;
  88. }
  89. thread_ids[id] = new std::thread(ThreadFuncState, id - 1);
  90. id++;
  91. for (int i = 0; i < id; i++) thread_ids[i]->join();
  92. for (int i = 0; i < id; i++) delete thread_ids[i];
  93. }
  94. TEST(CoreConveyor, GetBufferSize) {
  95. size_t conveyor_capacity = 20;
  96. Conveyor* conveyor = new Conveyor(conveyor_capacity);
  97. uint32_t store_num = rand_r(&seed_conveyor) % conveyor_capacity;
  98. for (uint32_t i = 0; i < store_num; ++i) {
  99. auto data = CNFrameInfo::Create(std::to_string(0));
  100. conveyor->PushDataBuffer(data);
  101. }
  102. EXPECT_EQ(conveyor->GetBufferSize(), store_num);
  103. delete conveyor;
  104. }
  105. TEST(CoreConveyor, PushPopDataBuffer) {
  106. uint32_t conveyor_num = 2;
  107. Conveyor* conveyor = new Conveyor(conveyor_num);
  108. std::shared_ptr<CNFrameInfo> sdata = CNFrameInfo::Create(std::to_string(0));
  109. conveyor->PushDataBuffer(sdata);
  110. auto rdata = conveyor->PopDataBuffer();
  111. EXPECT_EQ(sdata.get(), rdata.get());
  112. delete conveyor;
  113. }
  114. TEST(CoreConveyor, PushDataFull) {
  115. size_t max_size = 10;
  116. Conveyor* conveyor = new Conveyor(max_size);
  117. // When data queue is full, conveyor will drop one data from the front.
  118. for (uint32_t i = 0; i < max_size + 1; i++) {
  119. std::shared_ptr<CNFrameInfo> sdata = CNFrameInfo::Create(std::to_string(0));
  120. conveyor->PushDataBuffer(sdata);
  121. }
  122. delete conveyor;
  123. }
  124. TEST(CoreConveyor, PopAllData) {
  125. size_t max_size = 10;
  126. Conveyor* conveyor = new Conveyor(max_size);
  127. std::vector<std::shared_ptr<CNFrameInfo>> sdata_vec;
  128. std::vector<std::shared_ptr<CNFrameInfo>> rdata_vec;
  129. // When data queue is full, conveyor will drop one data from the front.
  130. for (uint32_t i = 0; i < max_size + 1; i++) {
  131. std::shared_ptr<CNFrameInfo> sdata = CNFrameInfo::Create(std::to_string(0));
  132. sdata_vec.push_back(sdata);
  133. conveyor->PushDataBuffer(sdata);
  134. }
  135. rdata_vec = conveyor->PopAllDataBuffer();
  136. EXPECT_EQ(rdata_vec.size(), max_size);
  137. for (uint32_t i = 0; i < max_size; i++) {
  138. EXPECT_EQ(sdata_vec[i], rdata_vec[i]);
  139. }
  140. delete conveyor;
  141. }
  142. } // namespace cnstream