|
| 1 | +# Copyright 2025 Google LLC |
| 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 | +# https://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 | +"""Watchdog tests.""" |
| 15 | + |
| 16 | +import logging |
| 17 | +import sys |
| 18 | +import threading |
| 19 | +import traceback |
| 20 | +from unittest import mock |
| 21 | + |
| 22 | +from pathwaysutils.debug import watchdog |
| 23 | + |
| 24 | +from absl.testing import absltest |
| 25 | +from absl.testing import parameterized |
| 26 | + |
| 27 | + |
| 28 | +class WatchdogTest(parameterized.TestCase): |
| 29 | + def test_watchdog_start_join(self): |
| 30 | + with ( |
| 31 | + mock.patch.object( |
| 32 | + threading.Thread, |
| 33 | + "start", |
| 34 | + autospec=True, |
| 35 | + ) as mock_start, |
| 36 | + mock.patch.object(threading.Thread, "join", autospec=True) as mock_join, |
| 37 | + ): |
| 38 | + with watchdog.watchdog(timeout=1): |
| 39 | + mock_start.assert_called_once() |
| 40 | + mock_join.assert_not_called() |
| 41 | + |
| 42 | + mock_start.assert_called_once() |
| 43 | + mock_join.assert_called_once() |
| 44 | + |
| 45 | + @parameterized.named_parameters([ |
| 46 | + ( |
| 47 | + "thread 1", |
| 48 | + 1, |
| 49 | + [ |
| 50 | + "DEBUG:pathwaysutils.debug.watchdog:Thread: 1", |
| 51 | + "DEBUG:pathwaysutils.debug.watchdog:examplestack1", |
| 52 | + ], |
| 53 | + ), |
| 54 | + ( |
| 55 | + "thread 2", |
| 56 | + 2, |
| 57 | + [ |
| 58 | + "DEBUG:pathwaysutils.debug.watchdog:Thread: 2", |
| 59 | + "DEBUG:pathwaysutils.debug.watchdog:examplestack2", |
| 60 | + ], |
| 61 | + ), |
| 62 | + ( |
| 63 | + "thread 3", |
| 64 | + 3, |
| 65 | + [ |
| 66 | + "DEBUG:pathwaysutils.debug.watchdog:Thread: 3", |
| 67 | + "DEBUG:pathwaysutils.debug.watchdog:", |
| 68 | + ], |
| 69 | + ), |
| 70 | + ]) |
| 71 | + def test_log_thread_strack_succes(self, thread_ident, expected_log_output): |
| 72 | + with ( |
| 73 | + mock.patch.object( |
| 74 | + sys, |
| 75 | + "_current_frames", |
| 76 | + return_value={1: ["example", "stack1"], 2: ["example", "stack2"]}, |
| 77 | + autospec=True, |
| 78 | + ), |
| 79 | + mock.patch.object( |
| 80 | + traceback, |
| 81 | + "format_stack", |
| 82 | + side_effect=lambda stack_str_list: stack_str_list, |
| 83 | + autospec=True, |
| 84 | + ), |
| 85 | + ): |
| 86 | + mock_thread = mock.create_autospec(threading.Thread, instance=True) |
| 87 | + mock_thread.ident = thread_ident |
| 88 | + |
| 89 | + with self.assertLogs(watchdog._logger, logging.DEBUG) as log_output: |
| 90 | + watchdog._log_thread_stack(mock_thread) |
| 91 | + |
| 92 | + self.assertEqual(log_output.output, expected_log_output) |
| 93 | + |
| 94 | + |
| 95 | +if __name__ == "__main__": |
| 96 | + absltest.main() |
0 commit comments