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

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