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.

interrupt_test.cc 1.9 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 "dataset/util/de_error.h"
  18. #include "utils/log_adapter.h"
  19. #include "dataset/util/services.h"
  20. #include "dataset/util/intrp_service.h"
  21. #include "dataset/util/task_manager.h"
  22. #include "dataset/util/queue.h"
  23. #include "dataset/util/semaphore.h"
  24. using namespace mindspore::dataset;
  25. using mindspore::MsLogLevel::INFO;
  26. using mindspore::ExceptionType::NoExceptionType;
  27. using mindspore::LogStream;
  28. class MindDataTestIntrpService : public UT::Common {
  29. public:
  30. MindDataTestIntrpService() {}
  31. void SetUp() {}
  32. TaskGroup vg_;
  33. };
  34. TEST_F(MindDataTestIntrpService, Test1) {
  35. Status rc;
  36. Queue<int> q(3);
  37. q.Register(&vg_);
  38. vg_.CreateAsyncTask("Test1", [&]() -> Status {
  39. TaskManager::FindMe()->Post();
  40. int v;
  41. Status rc;
  42. rc = q.PopFront(&v);
  43. EXPECT_TRUE(rc.IsInterrupted());
  44. return rc;
  45. });
  46. vg_.GetIntrpService()->InterruptAll();
  47. vg_.join_all();
  48. }
  49. TEST_F(MindDataTestIntrpService, Test2) {
  50. MS_LOG(INFO) << "Test Semaphore";
  51. Status rc;
  52. Semaphore sem(0);
  53. sem.Register(&vg_);
  54. vg_.CreateAsyncTask("Test1", [&]() -> Status {
  55. TaskManager::FindMe()->Post();
  56. Status rc = sem.P();
  57. EXPECT_TRUE(rc.IsInterrupted());
  58. return rc;
  59. });
  60. vg_.GetIntrpService()->InterruptAll();
  61. vg_.join_all();
  62. }