forked from apache/beam
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogger_test.py
More file actions
84 lines (68 loc) · 3.09 KB
/
Copy pathlogger_test.py
File metadata and controls
84 lines (68 loc) · 3.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import unittest
import logging
from unittest.mock import patch
from apache_beam.utils.logger import log_first_n, log_every_n, log_every_n_seconds, _LOG_COUNTER, _LOG_TIMER
import pytest
@pytest.mark.no_xdist
class TestLogFirstN(unittest.TestCase):
def setUp(self):
_LOG_COUNTER.clear()
_LOG_TIMER.clear()
@patch('apache_beam.utils.logger.logging.getLogger')
def test_log_first_n_once(self, mock_get_logger):
mock_logger = mock_get_logger.return_value
for _ in range(5):
log_first_n(logging.INFO, "Test message", n=1)
mock_logger.log.assert_called_once()
@patch('apache_beam.utils.logger.logging.getLogger')
def test_log_first_n_multiple(self, mock_get_logger):
mock_logger = mock_get_logger.return_value
for _ in range(5):
log_first_n(logging.INFO, "Test message", n=3)
self.assertEqual(mock_logger.log.call_count, 3)
@patch('apache_beam.utils.logger.logging.getLogger')
def test_log_first_n_with_different_callers(self, mock_get_logger):
mock_logger = mock_get_logger.return_value
for _ in range(5):
log_first_n(logging.INFO, "Test message", n=2)
# call from another "caller" (another line)
for _ in range(5):
log_first_n(logging.INFO, "Test message", n=2)
self.assertEqual(mock_logger.log.call_count, 4)
@patch('apache_beam.utils.logger.logging.getLogger')
def test_log_first_n_with_message_key(self, mock_get_logger):
mock_logger = mock_get_logger.return_value
log_first_n(logging.INFO, "Test message", n=1, key="message")
log_first_n(logging.INFO, "Test message", n=1, key="message")
self.assertEqual(mock_logger.log.call_count, 1)
@patch('apache_beam.utils.logger.logging.getLogger')
def test_log_first_n_with_caller_and_message_key(self, mock_get_logger):
mock_logger = mock_get_logger.return_value
for message in ["Test message", "Another message"]:
for _ in range(5):
log_first_n(logging.INFO, message, n=1, key=("caller", "message"))
self.assertEqual(mock_logger.log.call_count, 2)
@patch('apache_beam.utils.logger.logging.getLogger')
def test_log_every_n_multiple(self, mock_get_logger):
mock_logger = mock_get_logger.return_value
for _ in range(9):
log_every_n(logging.INFO, "Test message", n=2)
self.assertEqual(mock_logger.log.call_count, 5)
@patch('apache_beam.utils.logger.logging.getLogger')
@patch('apache_beam.utils.logger.time.time')
def test_log_every_n_seconds_always(self, mock_time, mock_get_logger):
mock_logger = mock_get_logger.return_value
for i in range(3):
mock_time.return_value = i
log_every_n_seconds(logging.INFO, "Test message", n=0)
self.assertEqual(mock_logger.log.call_count, 3)
@patch('apache_beam.utils.logger.logging.getLogger')
@patch('apache_beam.utils.logger.time.time')
def test_log_every_n_seconds_multiple(self, mock_time, mock_get_logger):
mock_logger = mock_get_logger.return_value
for i in range(4):
mock_time.return_value = i
log_every_n_seconds(logging.INFO, "Test message", n=2)
self.assertEqual(mock_logger.log.call_count, 2)
if __name__ == '__main__':
unittest.main()