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

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