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_loader.py 2.3 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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_manager"""
  16. import os
  17. import threading
  18. import time
  19. from unittest.mock import patch
  20. from mindinsight.datavisual.data_access.file_handler import FileHandler
  21. from mindinsight.explainer.manager.explain_loader import ExplainLoader
  22. from mindinsight.explainer.manager.explain_loader import _LoaderStatus
  23. from mindinsight.explainer.manager.explain_parser import ExplainParser
  24. class TestExplainLoader:
  25. """Test explain loader class."""
  26. @patch.object(ExplainParser, 'list_events')
  27. @patch.object(FileHandler, 'list_dir')
  28. @patch.object(FileHandler, 'is_file')
  29. @patch.object(os, 'stat')
  30. def test_stop(self, mock_stat, mock_is_file, mock_list_dir, mock_list_events):
  31. """Test stop function."""
  32. mock_is_file.return_value = True
  33. mock_list_dir.return_value = ['events.summary.123.host_explain']
  34. mock_list_events.return_value = (True, False, None)
  35. class _MockStat:
  36. def __init__(self, _):
  37. self.st_ctime = 1
  38. self.st_mtime = 1
  39. self.st_size = 1
  40. mock_stat.side_effect = _MockStat
  41. loader = ExplainLoader(
  42. loader_id='./summary_dir',
  43. summary_dir='./summary_dir')
  44. def _stop_loader(explain_loader):
  45. time.sleep(0.01)
  46. assert explain_loader.status == _LoaderStatus.LOADING.value
  47. explain_loader.stop()
  48. thread = threading.Thread(target=_stop_loader, args=[loader], daemon=True)
  49. thread.start()
  50. loader.load()
  51. assert loader.status == _LoaderStatus.STOP.value