|
| 1 | +# |
| 2 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +# contributor license agreements. See the NOTICE file distributed with |
| 4 | +# this work for additional information regarding copyright ownership. |
| 5 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +# (the "License"); you may not use this file except in compliance with |
| 7 | +# the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import logging |
| 19 | +import unittest |
| 20 | +from unittest.mock import patch |
| 21 | + |
| 22 | +import pytest |
| 23 | + |
| 24 | +from apache_beam.utils.logger import _LOG_COUNTER |
| 25 | +from apache_beam.utils.logger import _LOG_TIMER |
| 26 | +from apache_beam.utils.logger import log_every_n |
| 27 | +from apache_beam.utils.logger import log_every_n_seconds |
| 28 | +from apache_beam.utils.logger import log_first_n |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.no_xdist |
| 32 | +class TestLogFirstN(unittest.TestCase): |
| 33 | + def setUp(self): |
| 34 | + _LOG_COUNTER.clear() |
| 35 | + _LOG_TIMER.clear() |
| 36 | + |
| 37 | + @patch('apache_beam.utils.logger.logging.getLogger') |
| 38 | + def test_log_first_n_once(self, mock_get_logger): |
| 39 | + mock_logger = mock_get_logger.return_value |
| 40 | + for _ in range(5): |
| 41 | + log_first_n(logging.INFO, "Test message %s", "arg", n=1) |
| 42 | + mock_logger.log.assert_called_once_with( |
| 43 | + logging.INFO, "Test message %s", "arg") |
| 44 | + |
| 45 | + @patch('apache_beam.utils.logger.logging.getLogger') |
| 46 | + def test_log_first_n_multiple(self, mock_get_logger): |
| 47 | + mock_logger = mock_get_logger.return_value |
| 48 | + for _ in range(5): |
| 49 | + log_first_n(logging.INFO, "Test message %s", "arg", n=3) |
| 50 | + self.assertEqual(mock_logger.log.call_count, 3) |
| 51 | + mock_logger.log.assert_called_with(logging.INFO, "Test message %s", "arg") |
| 52 | + |
| 53 | + @patch('apache_beam.utils.logger.logging.getLogger') |
| 54 | + def test_log_first_n_with_different_callers(self, mock_get_logger): |
| 55 | + mock_logger = mock_get_logger.return_value |
| 56 | + for _ in range(5): |
| 57 | + log_first_n(logging.INFO, "Test message", n=2) |
| 58 | + |
| 59 | + # call from another "caller" (another line) |
| 60 | + for _ in range(5): |
| 61 | + log_first_n(logging.INFO, "Test message", n=2) |
| 62 | + |
| 63 | + self.assertEqual(mock_logger.log.call_count, 4) |
| 64 | + |
| 65 | + @patch('apache_beam.utils.logger.logging.getLogger') |
| 66 | + def test_log_first_n_with_message_key(self, mock_get_logger): |
| 67 | + mock_logger = mock_get_logger.return_value |
| 68 | + log_first_n(logging.INFO, "Test message", n=1, key="message") |
| 69 | + log_first_n(logging.INFO, "Test message", n=1, key="message") |
| 70 | + self.assertEqual(mock_logger.log.call_count, 1) |
| 71 | + |
| 72 | + @patch('apache_beam.utils.logger.logging.getLogger') |
| 73 | + def test_log_first_n_with_caller_and_message_key(self, mock_get_logger): |
| 74 | + mock_logger = mock_get_logger.return_value |
| 75 | + for message in ["Test message", "Another message"]: |
| 76 | + for _ in range(5): |
| 77 | + log_first_n(logging.INFO, message, n=1, key=("caller", "message")) |
| 78 | + self.assertEqual(mock_logger.log.call_count, 2) |
| 79 | + |
| 80 | + @patch('apache_beam.utils.logger.logging.getLogger') |
| 81 | + def test_log_every_n_multiple(self, mock_get_logger): |
| 82 | + mock_logger = mock_get_logger.return_value |
| 83 | + for _ in range(9): |
| 84 | + log_every_n(logging.INFO, "Test message", n=2) |
| 85 | + |
| 86 | + self.assertEqual(mock_logger.log.call_count, 5) |
| 87 | + |
| 88 | + @patch('apache_beam.utils.logger.logging.getLogger') |
| 89 | + @patch('apache_beam.utils.logger.time.time') |
| 90 | + def test_log_every_n_seconds_always(self, mock_time, mock_get_logger): |
| 91 | + mock_logger = mock_get_logger.return_value |
| 92 | + for i in range(3): |
| 93 | + mock_time.return_value = i |
| 94 | + log_every_n_seconds(logging.INFO, "Test message", n=0) |
| 95 | + self.assertEqual(mock_logger.log.call_count, 3) |
| 96 | + |
| 97 | + @patch('apache_beam.utils.logger.logging.getLogger') |
| 98 | + @patch('apache_beam.utils.logger.time.time') |
| 99 | + def test_log_every_n_seconds_multiple(self, mock_time, mock_get_logger): |
| 100 | + mock_logger = mock_get_logger.return_value |
| 101 | + for i in range(4): |
| 102 | + mock_time.return_value = i |
| 103 | + log_every_n_seconds(logging.INFO, "Test message", n=2) |
| 104 | + self.assertEqual(mock_logger.log.call_count, 2) |
| 105 | + |
| 106 | + |
| 107 | +if __name__ == '__main__': |
| 108 | + unittest.main() |
0 commit comments