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 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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::kUnexpectedError, __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::kOutOfMemory, "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.IsOutofMemory());
  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. }