You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

queue_test.cc 8.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /**
  2. * Copyright 2019 Huawei Technologies Co., Ltd
  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. * 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. #include "common/common.h"
  17. #include "gtest/gtest.h"
  18. #include "minddata/dataset/util/task_manager.h"
  19. #include "minddata/dataset/util/queue.h"
  20. #include <atomic>
  21. #include <chrono>
  22. #include <random>
  23. #include "utils/log_adapter.h"
  24. using namespace mindspore::dataset;
  25. using mindspore::LogStream;
  26. using mindspore::ExceptionType::NoExceptionType;
  27. using mindspore::MsLogLevel::INFO;
  28. class MindDataTestQueue : public UT::Common {
  29. public:
  30. MindDataTestQueue() {}
  31. void SetUp() {}
  32. };
  33. int gRefCountDestructorCalled;
  34. class RefCount {
  35. public:
  36. RefCount() : v_(nullptr) {}
  37. explicit RefCount(int x) : v_(std::make_shared<int>(x)) {}
  38. RefCount(const RefCount &o) : v_(o.v_) {}
  39. ~RefCount() {
  40. MS_LOG(DEBUG) << "Destructor of RefCount called" << std::endl;
  41. gRefCountDestructorCalled++;
  42. }
  43. RefCount &operator=(const RefCount &o) {
  44. v_ = o.v_;
  45. return *this;
  46. }
  47. RefCount(RefCount &&o) : v_(std::move(o.v_)) {}
  48. RefCount &operator=(RefCount &&o) {
  49. if (&o != this) {
  50. v_ = std::move(o.v_);
  51. }
  52. return *this;
  53. }
  54. std::shared_ptr<int> v_;
  55. };
  56. TEST_F(MindDataTestQueue, Test1) {
  57. // Passing shared pointer along the queue
  58. Queue<std::shared_ptr<int>> que(3);
  59. std::shared_ptr<int> a = std::make_shared<int>(20);
  60. Status rc = que.Add(a);
  61. ASSERT_TRUE(rc.IsOk());
  62. // Use count should be 2 right now. a plus the one in the queue.
  63. ASSERT_EQ(a.use_count(), 2);
  64. std::shared_ptr<int> b;
  65. rc = que.PopFront(&b);
  66. ASSERT_TRUE(rc.IsOk());
  67. ASSERT_EQ(*b, 20);
  68. // Use count should remain 2. a and b. No copy in the queue.
  69. ASSERT_EQ(a.use_count(), 2);
  70. a.reset(new int(5));
  71. ASSERT_EQ(a.use_count(), 1);
  72. // Push again but expect a is nullptr after push
  73. rc = que.Add(std::move(a));
  74. ASSERT_TRUE(rc.IsOk());
  75. ASSERT_EQ(a.use_count(), 0);
  76. rc = que.PopFront(&b);
  77. ASSERT_TRUE(rc.IsOk());
  78. ASSERT_EQ(*b, 5);
  79. ASSERT_EQ(b.use_count(), 1);
  80. // Test construct in place
  81. rc = que.EmplaceBack(std::make_shared<int>(100));
  82. ASSERT_TRUE(rc.IsOk());
  83. rc = que.PopFront(&b);
  84. ASSERT_TRUE(rc.IsOk());
  85. ASSERT_EQ(*b, 100);
  86. ASSERT_EQ(b.use_count(), 1);
  87. // Test the destructor of the Queue by add an element in the queue without popping it and let the queue go
  88. // out of scope.
  89. rc = que.EmplaceBack(std::make_shared<int>(2000));
  90. ASSERT_TRUE(rc.IsOk());
  91. }
  92. TEST_F(MindDataTestQueue, Test2) {
  93. // Passing status object
  94. Queue<Status> que(3);
  95. Status rc_send(StatusCode::kMDUnexpectedError, __LINE__, __FILE__, "Oops");
  96. Status rc = que.Add(rc_send);
  97. ASSERT_TRUE(rc.IsOk());
  98. Status rc_recv;
  99. rc = que.PopFront(&rc_recv);
  100. ASSERT_TRUE(rc.IsOk());
  101. ASSERT_EQ(rc_recv, rc_send);
  102. rc = que.EmplaceBack(StatusCode::kMDOutOfMemory, "Test emplace");
  103. ASSERT_TRUE(rc.IsOk());
  104. Status rc_recv2;
  105. rc = que.PopFront(&rc_recv2);
  106. ASSERT_TRUE(rc.IsOk());
  107. ASSERT_TRUE(rc_recv2 == StatusCode::kMDOutOfMemory);
  108. }
  109. TEST_F(MindDataTestQueue, Test3) {
  110. Queue<std::unique_ptr<int>> que(3);
  111. std::unique_ptr<int> a(new int(3));
  112. Status rc = que.Add(std::move(a));
  113. ASSERT_TRUE(rc.IsOk());
  114. ASSERT_EQ(a.get(), nullptr);
  115. std::unique_ptr<int> b;
  116. rc = que.PopFront(&b);
  117. ASSERT_TRUE(rc.IsOk());
  118. ASSERT_EQ(*b, 3);
  119. rc = que.EmplaceBack(new int(40));
  120. ASSERT_TRUE(rc.IsOk());
  121. rc = que.PopFront(&b);
  122. ASSERT_TRUE(rc.IsOk());
  123. ASSERT_EQ(*b, 40);
  124. }
  125. void test4() {
  126. gRefCountDestructorCalled = 0;
  127. // Pass a structure along the queue.
  128. Queue<RefCount> que(3);
  129. RefCount a(3);
  130. Status rc = que.Add(a);
  131. ASSERT_TRUE(rc.IsOk());
  132. RefCount b;
  133. rc = que.PopFront(&b);
  134. ASSERT_TRUE(rc.IsOk());
  135. ASSERT_EQ(b.v_.use_count(), 2);
  136. ASSERT_EQ(*(b.v_.get()), 3);
  137. // Test the destructor of the Queue by adding an element without popping.
  138. rc = que.EmplaceBack(10);
  139. ASSERT_TRUE(rc.IsOk());
  140. }
  141. TEST_F(MindDataTestQueue, Test4) { test4(); }
  142. TEST_F(MindDataTestQueue, Test5) {
  143. test4();
  144. // Assume we have run Test4. The destructor of the RefCount should be called 4 times.
  145. // One for a. One for b. One for the stale element in the queue. 3 more for
  146. // the one in the queue (but they are empty).
  147. ASSERT_EQ(gRefCountDestructorCalled, 6);
  148. }
  149. TEST_F(MindDataTestQueue, Test6) {
  150. // Create a list of queues
  151. QueueList<std::unique_ptr<int>> my_list_of_queues;
  152. const int chosen_queue_index = 2;
  153. const int num_queues = 4;
  154. const int queue_capacity = 3;
  155. my_list_of_queues.Init(num_queues, queue_capacity);
  156. // Now try to insert a number into a specific queue and pop it
  157. std::unique_ptr<int> a(new int(99));
  158. Status rc = my_list_of_queues[chosen_queue_index]->Add(std::move(a));
  159. ASSERT_TRUE(rc.IsOk());
  160. std::unique_ptr<int> pepped_value;
  161. rc = my_list_of_queues[chosen_queue_index]->PopFront(&pepped_value);
  162. ASSERT_TRUE(rc.IsOk());
  163. MS_LOG(INFO) << "Popped value " << *pepped_value << " from queue index " << chosen_queue_index;
  164. ASSERT_EQ(*pepped_value, 99);
  165. }
  166. // Feature: Test basic check in the resize.
  167. // Description: Check false input for resize function.
  168. // Expectation: Return false when the input is unexpected, and true when the new capacity is the same as original.
  169. TEST_F(MindDataTestQueue, TestResize1) {
  170. // Create a list of queues with capacity = 3
  171. Queue<TensorRow> queue(3);
  172. ASSERT_EQ(3, queue.capacity());
  173. // Add 3 rows into the queue
  174. TensorRow a;
  175. std::shared_ptr<Tensor> test_tensor1;
  176. std::vector<float> input = {1.1, 0.2, 0.3, 0.4, 0.5, 0.6, 1.2, 0.7, 0.8, 0.9, 1.0, 2.0, 1.3, 3.0, 4.0};
  177. EXPECT_OK(Tensor::CreateFromVector(input, TensorShape{3, 5}, &test_tensor1));
  178. a.push_back(test_tensor1);
  179. EXPECT_OK(queue.Add(a));
  180. TensorRow b;
  181. std::shared_ptr<Tensor> test_tensor2;
  182. EXPECT_OK(Tensor::CreateScalar(true, &test_tensor2));
  183. b.push_back(test_tensor2);
  184. EXPECT_OK(queue.Add(b));
  185. TensorRow c;
  186. std::shared_ptr<Tensor> test_tensor3;
  187. EXPECT_OK(Tensor::CreateFromVector(input, &test_tensor3));
  188. c.push_back(test_tensor3);
  189. EXPECT_OK(queue.Add(c));
  190. ASSERT_EQ(3, queue.size());
  191. // Check false if input is equal to or smaller than 0
  192. EXPECT_ERROR(queue.Resize(0));
  193. EXPECT_ERROR(queue.Resize(-1));
  194. // Check true if the new capacity is the same as original
  195. EXPECT_OK(queue.Resize(3));
  196. }
  197. // Feature: Check resize is finished without changing elements and influencing operations.
  198. // Description: Compare elements in queue before and after resize, and test add/pop/reset.
  199. // Expectation: Elements in queue after resize are the same as the original queue.
  200. TEST_F(MindDataTestQueue, TestResize2) {
  201. // Create a list of queues with capacity = 3
  202. Queue<TensorRow> queue(3);
  203. ASSERT_EQ(3, queue.capacity());
  204. // Add 3 rows into the queue
  205. TensorRow a;
  206. std::shared_ptr<Tensor> test_tensor1;
  207. std::vector<float> input = {1.1, 0.2, 0.3, 0.4, 0.5, 0.6, 1.2, 0.7, 0.8, 0.9, 1.0, 2.0, 1.3, 3.0, 4.0};
  208. EXPECT_OK(Tensor::CreateFromVector(input, TensorShape{3, 5}, &test_tensor1));
  209. a.push_back(test_tensor1);
  210. EXPECT_OK(queue.Add(a));
  211. TensorRow b;
  212. std::shared_ptr<Tensor> test_tensor2;
  213. EXPECT_OK(Tensor::CreateScalar(true, &test_tensor2));
  214. b.push_back(test_tensor2);
  215. EXPECT_OK(queue.Add(b));
  216. TensorRow c;
  217. std::shared_ptr<Tensor> test_tensor3;
  218. EXPECT_OK(Tensor::CreateFromVector(input, &test_tensor3));
  219. c.push_back(test_tensor3);
  220. EXPECT_OK(queue.Add(c));
  221. ASSERT_EQ(3, queue.size());
  222. // Check true if the resize is smaller than current size
  223. EXPECT_OK(queue.Resize(1));
  224. ASSERT_EQ(1, queue.capacity());
  225. // Expect the rows after resize are the same as original input, there should be still 1 element in the queue
  226. TensorRow d;
  227. EXPECT_OK(queue.PopFront(&d));
  228. EXPECT_EQ(a.getRow(), d.getRow());
  229. ASSERT_EQ(1, queue.size());
  230. // Check true if the resize is larger than current size, and capacity is changed
  231. EXPECT_OK(queue.Resize(12));
  232. ASSERT_EQ(12, queue.capacity());
  233. // Check add operation after resize
  234. EXPECT_OK(queue.Add(a));
  235. ASSERT_EQ(3, queue.size());
  236. // Check pop operation after resize
  237. EXPECT_OK(queue.PopFront(&d));
  238. EXPECT_EQ(b.getRow(), d.getRow());
  239. EXPECT_OK(queue.PopFront(&d));
  240. EXPECT_EQ(c.getRow(), d.getRow());
  241. ASSERT_EQ(1, queue.size());
  242. queue.Reset();
  243. ASSERT_EQ(0, queue.size());
  244. }