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.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 "dataset/util/task_manager.h"
  19. #include "dataset/util/queue.h"
  20. #include <atomic>
  21. #include "utils/log_adapter.h"
  22. using namespace mindspore::dataset;
  23. using mindspore::LogStream;
  24. using mindspore::ExceptionType::NoExceptionType;
  25. using mindspore::MsLogLevel::INFO;
  26. class MindDataTestQueue : public UT::Common {
  27. public:
  28. MindDataTestQueue() {}
  29. void SetUp() {}
  30. };
  31. int gRefCountDestructorCalled;
  32. class RefCount {
  33. public:
  34. RefCount() : v_(nullptr) {}
  35. explicit RefCount(int x) : v_(std::make_shared<int>(x)) {}
  36. explicit RefCount(const RefCount &o) : v_(o.v_) {}
  37. ~RefCount() {
  38. MS_LOG(DEBUG) << "Destructor of RefCount called" << std::endl;
  39. gRefCountDestructorCalled++;
  40. }
  41. RefCount &operator=(const RefCount &o) {
  42. v_ = o.v_;
  43. return *this;
  44. }
  45. std::shared_ptr<int> v_;
  46. };
  47. TEST_F(MindDataTestQueue, Test1) {
  48. // Passing shared pointer along the queue
  49. Queue<std::shared_ptr<int>> que(3);
  50. std::shared_ptr<int> a = std::make_shared<int>(20);
  51. Status rc = que.Add(a);
  52. ASSERT_TRUE(rc.IsOk());
  53. // Use count should be 2 right now. a plus the one in the queue.
  54. ASSERT_EQ(a.use_count(), 2);
  55. std::shared_ptr<int> b;
  56. rc = que.PopFront(&b);
  57. ASSERT_TRUE(rc.IsOk());
  58. ASSERT_EQ(*b, 20);
  59. // Use count should remain 2. a and b. No copy in the queue.
  60. ASSERT_EQ(a.use_count(), 2);
  61. a.reset(new int(5));
  62. ASSERT_EQ(a.use_count(), 1);
  63. // Push again but expect a is nullptr after push
  64. rc = que.Add(std::move(a));
  65. ASSERT_TRUE(rc.IsOk());
  66. ASSERT_EQ(a.use_count(), 0);
  67. rc = que.PopFront(&b);
  68. ASSERT_TRUE(rc.IsOk());
  69. ASSERT_EQ(*b, 5);
  70. ASSERT_EQ(b.use_count(), 1);
  71. // Test construct in place
  72. rc = que.EmplaceBack(std::make_shared<int>(100));
  73. ASSERT_TRUE(rc.IsOk());
  74. rc = que.PopFront(&b);
  75. ASSERT_TRUE(rc.IsOk());
  76. ASSERT_EQ(*b, 100);
  77. ASSERT_EQ(b.use_count(), 1);
  78. // Test the destructor of the Queue by add an element in the queue without popping it and let the queue go
  79. // out of scope.
  80. rc = que.EmplaceBack(std::make_shared<int>(2000));
  81. ASSERT_TRUE(rc.IsOk());
  82. }
  83. TEST_F(MindDataTestQueue, Test2) {
  84. // Passing status object
  85. Queue<Status> que(3);
  86. Status rc_send(StatusCode::kUnexpectedError, __LINE__, __FILE__, "Oops");
  87. Status rc = que.Add(rc_send);
  88. ASSERT_TRUE(rc.IsOk());
  89. Status rc_recv;
  90. rc = que.PopFront(&rc_recv);
  91. ASSERT_TRUE(rc.IsOk());
  92. ASSERT_EQ(rc_recv, rc_send);
  93. rc = que.EmplaceBack(StatusCode::kOutOfMemory, "Test emplace");
  94. ASSERT_TRUE(rc.IsOk());
  95. Status rc_recv2;
  96. rc = que.PopFront(&rc_recv2);
  97. ASSERT_TRUE(rc.IsOk());
  98. ASSERT_TRUE(rc_recv2.IsOutofMemory());
  99. }
  100. TEST_F(MindDataTestQueue, Test3) {
  101. Queue<std::unique_ptr<int>> que(3);
  102. std::unique_ptr<int> a(new int(3));
  103. Status rc = que.Add(std::move(a));
  104. ASSERT_TRUE(rc.IsOk());
  105. ASSERT_EQ(a.get(), nullptr);
  106. std::unique_ptr<int> b;
  107. rc = que.PopFront(&b);
  108. ASSERT_TRUE(rc.IsOk());
  109. ASSERT_EQ(*b, 3);
  110. rc = que.EmplaceBack(new int(40));
  111. ASSERT_TRUE(rc.IsOk());
  112. rc = que.PopFront(&b);
  113. ASSERT_TRUE(rc.IsOk());
  114. ASSERT_EQ(*b, 40);
  115. }
  116. void test4() {
  117. gRefCountDestructorCalled = 0;
  118. // Pass a structure along the queue.
  119. Queue<RefCount> que(3);
  120. RefCount a(3);
  121. Status rc = que.Add(a);
  122. ASSERT_TRUE(rc.IsOk());
  123. RefCount b;
  124. rc = que.PopFront(&b);
  125. ASSERT_TRUE(rc.IsOk());
  126. ASSERT_EQ(b.v_.use_count(), 2);
  127. ASSERT_EQ(*(b.v_.get()), 3);
  128. // Test the destructor of the Queue by adding an element without popping.
  129. rc = que.EmplaceBack(10);
  130. ASSERT_TRUE(rc.IsOk());
  131. }
  132. TEST_F(MindDataTestQueue, Test4) { test4(); }
  133. TEST_F(MindDataTestQueue, Test5) {
  134. test4();
  135. // Assume we have run Test4. The destructor of the RefCount should be called 4 times.
  136. // One for a. One for b. One for line 125 when we pop. One for the stale element in the queue.
  137. ASSERT_EQ(gRefCountDestructorCalled, 4);
  138. }
  139. TEST_F(MindDataTestQueue, Test6) {
  140. // Create a list of queues
  141. QueueList<std::unique_ptr<int>> my_list_of_queues;
  142. const int chosen_queue_index = 2;
  143. const int num_queues = 4;
  144. const int queue_capacity = 3;
  145. my_list_of_queues.Init(num_queues, queue_capacity);
  146. // Now try to insert a number into a specific queue and pop it
  147. std::unique_ptr<int> a(new int(99));
  148. Status rc = my_list_of_queues[chosen_queue_index]->Add(std::move(a));
  149. ASSERT_TRUE(rc.IsOk());
  150. std::unique_ptr<int> pepped_value;
  151. rc = my_list_of_queues[chosen_queue_index]->PopFront(&pepped_value);
  152. ASSERT_TRUE(rc.IsOk());
  153. MS_LOG(INFO) << "Popped value " << *pepped_value << " from queue index " << chosen_queue_index;
  154. ASSERT_EQ(*pepped_value, 99);
  155. }