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.

task_manager_test.cc 3.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. using namespace mindspore::dataset;
  20. class MindDataTestTaskManager : public UT::Common {
  21. public:
  22. MindDataTestTaskManager() {}
  23. void SetUp() { Services::CreateInstance(); }
  24. };
  25. TEST_F(MindDataTestTaskManager, Test1) {
  26. // Clear the rc of the master thread if any
  27. (void)TaskManager::GetMasterThreadRc();
  28. TaskGroup vg;
  29. Status vg_rc = vg.CreateAsyncTask("Test error", []() -> Status {
  30. TaskManager::FindMe()->Post();
  31. throw std::bad_alloc();
  32. });
  33. ASSERT_TRUE(vg_rc.IsOk() || vg_rc == StatusCode::kMDOutOfMemory);
  34. ASSERT_TRUE(vg.join_all().IsOk());
  35. ASSERT_TRUE(vg.GetTaskErrorIfAny() == StatusCode::kMDOutOfMemory);
  36. // Test the error is passed back to the master thread if vg_rc above is OK.
  37. // If vg_rc is kOutOfMemory, the group error is already passed back.
  38. // Some compiler may choose to run the next line in parallel with the above 3 lines
  39. // and this will cause some mismatch once a while.
  40. // To block this racing condition, we need to create a dependency that the next line
  41. // depends on previous lines.
  42. if (vg.GetTaskErrorIfAny().IsError() && vg_rc.IsOk()) {
  43. Status rc = TaskManager::GetMasterThreadRc();
  44. ASSERT_TRUE(rc == StatusCode::kMDOutOfMemory);
  45. }
  46. }
  47. TEST_F(MindDataTestTaskManager, Test2) {
  48. // This testcase will spawn about 100 threads and block on a conditional variable.
  49. // The master thread will try to interrupt them almost at the same time. This can
  50. // cause a racing condition that some threads may miss the interrupt and blocked.
  51. // The new logic of Task::Join() will do a time-out join and wake up all those
  52. // threads that miss the interrupt.
  53. // Clear the rc of the master thread if any
  54. (void)TaskManager::GetMasterThreadRc();
  55. TaskGroup vg;
  56. CondVar cv;
  57. std::mutex mux;
  58. Status rc;
  59. rc = cv.Register(vg.GetIntrpService());
  60. EXPECT_TRUE(rc.IsOk());
  61. auto block_forever = [&cv, &mux]() -> Status {
  62. std::unique_lock<std::mutex> lck(mux);
  63. TaskManager::FindMe()->Post();
  64. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  65. RETURN_IF_NOT_OK(cv.Wait(&lck, []() -> bool { return false; }));
  66. return Status::OK();
  67. };
  68. auto f = [&vg, &block_forever]() -> Status {
  69. for (auto i = 0; i < 100; ++i) {
  70. RETURN_IF_NOT_OK(vg.CreateAsyncTask("Spawn block threads", block_forever));
  71. }
  72. return Status::OK();
  73. };
  74. rc = f();
  75. vg.interrupt_all();
  76. EXPECT_TRUE(rc.IsOk());
  77. // Now we test the async Join
  78. ASSERT_TRUE(vg.join_all(Task::WaitFlag::kNonBlocking).IsOk());
  79. }