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.

test_explain_manager.py 3.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """UT for explainer.manager.explain_loader."""
  16. import os
  17. import threading
  18. import time
  19. from unittest.mock import patch
  20. from mindinsight.explainer.manager.explain_loader import ExplainLoader
  21. from mindinsight.explainer.manager.explain_loader import _LoaderStatus
  22. from mindinsight.explainer.manager.explain_manager import ExplainManager
  23. from mindinsight.explainer.manager.explain_manager import _ExplainManagerStatus
  24. class TestExplainManager:
  25. """Test explain manager class."""
  26. def test_stop_load_data_not_loading_status(self):
  27. """Test stop load data when the status is not loading."""
  28. manager = ExplainManager('./summary_dir')
  29. assert manager.status == _ExplainManagerStatus.INIT.value
  30. manager.status = _ExplainManagerStatus.DONE.value
  31. manager._stop_load_data()
  32. assert manager.status == _ExplainManagerStatus.DONE.value
  33. @patch.object(os, 'stat')
  34. def test_stop_load_data_with_loading_status(self, mock_stat):
  35. """Test stop load data with status is loading."""
  36. class _MockStat:
  37. def __init__(self, _):
  38. self.st_ctime = 1
  39. self.st_mtime = 1
  40. self.st_size = 1
  41. mock_stat.side_effect = _MockStat
  42. manager = ExplainManager('./summary_dir')
  43. manager.status = _ExplainManagerStatus.LOADING.value
  44. loader_count = 3
  45. for i in range(loader_count):
  46. loader = ExplainLoader(f'./summary_dir{i}', f'./summary_dir{i}')
  47. loader.status = _LoaderStatus.LOADING.value
  48. manager._loader_pool[i] = loader
  49. def _wrapper(loader_manager):
  50. assert loader_manager.status == _ExplainManagerStatus.LOADING.value
  51. time.sleep(0.01)
  52. loader_manager.status = _ExplainManagerStatus.DONE.value
  53. thread = threading.Thread(target=_wrapper, args=(manager,), daemon=True)
  54. thread.start()
  55. manager._stop_load_data()
  56. for loader in manager._loader_pool.values():
  57. assert loader.status == _LoaderStatus.STOP.value
  58. assert manager.status == _ExplainManagerStatus.DONE.value
  59. def test_stop_load_data_with_after_cache_loaders(self):
  60. """
  61. Test stop load data that is triggered by get a not in loader pool job.
  62. In this case, we will mock the cache_loader function, and set status to STOP by other thread.
  63. """
  64. manager = ExplainManager('./summary_dir')
  65. def _mock_cache_loaders():
  66. for _ in range(3):
  67. time.sleep(0.1)
  68. manager._cache_loaders = _mock_cache_loaders
  69. load_data_thread = threading.Thread(target=manager._load_data, name='manager_load_data', daemon=True)
  70. stop_thread = threading.Thread(target=manager._stop_load_data, name='stop_load_data', daemon=True)
  71. load_data_thread.start()
  72. while manager.status != _ExplainManagerStatus.LOADING.value:
  73. continue
  74. stop_thread.start()
  75. stop_thread.join()
  76. assert manager.status == _ExplainManagerStatus.DONE.value