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 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # Copyright 2020-2021 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 _MockStat:
  25. def __init__(self, _):
  26. self.st_ctime = 1
  27. self.st_mtime = 1
  28. self.st_size = 1
  29. class TestExplainLoader:
  30. """Test explain loader class."""
  31. @patch.object(ExplainParser, 'list_events')
  32. @patch.object(FileHandler, 'list_dir')
  33. @patch.object(FileHandler, 'is_file')
  34. @patch.object(os, 'stat')
  35. def test_stop(self, mock_stat, mock_is_file, mock_list_dir, mock_list_events):
  36. """Test stop function."""
  37. mock_is_file.return_value = True
  38. mock_list_dir.return_value = ['events.summary.123.host_explain']
  39. mock_list_events.return_value = (True, False, None)
  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. while explain_loader.status != _LoaderStatus.LOADING.value:
  46. time.sleep(0.01)
  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
  52. @patch.object(ExplainParser, 'list_events')
  53. @patch.object(FileHandler, 'list_dir')
  54. @patch.object(FileHandler, 'is_file')
  55. @patch.object(os, 'stat')
  56. def test_loaded_with_is_end(self, mock_stat, mock_is_file, mock_list_dir, mock_list_events):
  57. """Test loading function."""
  58. mock_is_file.return_value = True
  59. mock_list_dir.return_value = ['events.summary.123.host_explain']
  60. mock_list_events.return_value = (True, True, None)
  61. mock_stat.side_effect = _MockStat
  62. loader = ExplainLoader(
  63. loader_id='./summary_dir',
  64. summary_dir='./summary_dir')
  65. loader.load()
  66. assert loader.status == _LoaderStatus.LOADED.value