forked from googleapis/python-logging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_background_thread.py
586 lines (448 loc) · 19.1 KB
/
test_background_thread.py
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# Copyright 2016 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
import logging
import queue
import unittest
import mock
class TestBackgroundThreadHandler(unittest.TestCase):
PROJECT = "PROJECT"
@staticmethod
def _get_target_class():
from google.cloud.logging.handlers.transports import BackgroundThreadTransport
return BackgroundThreadTransport
def _make_one(self, *args, **kw):
worker_patch = mock.patch(
"google.cloud.logging_v2.handlers.transports." "background_thread._Worker",
autospec=True,
)
with worker_patch as worker_mock:
return self._get_target_class()(*args, **kw), worker_mock
def test_constructor(self):
client = _Client(self.PROJECT)
name = "python_logger"
transport, worker = self._make_one(client, name)
(logger,) = worker.call_args[0] # call_args[0] is *args.
self.assertEqual(logger.name, name)
def test_send(self):
from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
client = _Client(self.PROJECT)
name = "python_logger"
transport, _ = self._make_one(client, name)
python_logger_name = "mylogger"
message = "hello world"
record = logging.LogRecord(
python_logger_name, logging.INFO, None, None, message, None, None
)
transport.send(record, message, resource=_GLOBAL_RESOURCE)
transport.worker.enqueue.assert_called_once_with(
record,
message,
resource=_GLOBAL_RESOURCE,
)
def test_send_json(self):
from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
client = _Client(self.PROJECT)
name = "python_logger"
transport, _ = self._make_one(client, name)
python_logger_name = "mylogger"
message = {"hello": {"world": "!"}}
record = logging.LogRecord(
python_logger_name, logging.INFO, None, None, message, None, None
)
transport.send(record, message, resource=_GLOBAL_RESOURCE)
transport.worker.enqueue.assert_called_once_with(
record,
message,
resource=_GLOBAL_RESOURCE,
)
def test_trace_send(self):
from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
client = _Client(self.PROJECT)
name = "python_logger"
transport, _ = self._make_one(client, name)
python_logger_name = "mylogger"
message = "hello world"
trace = "the-project/trace/longlogTraceid"
record = logging.LogRecord(
python_logger_name, logging.INFO, None, None, message, None, None
)
transport.send(record, message, resource=_GLOBAL_RESOURCE, trace=trace)
transport.worker.enqueue.assert_called_once_with(
record,
message,
resource=_GLOBAL_RESOURCE,
trace=trace,
)
def test_span_send(self):
from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
client = _Client(self.PROJECT)
name = "python_logger"
transport, _ = self._make_one(client, name)
python_logger_name = "mylogger"
message = "hello world"
span_id = "the-project/trace/longlogTraceid/span/123456789012abbacdac"
record = logging.LogRecord(
python_logger_name, logging.INFO, None, None, message, None, None
)
transport.send(record, message, resource=_GLOBAL_RESOURCE, span_id=span_id)
transport.worker.enqueue.assert_called_once_with(
record,
message,
resource=_GLOBAL_RESOURCE,
span_id=span_id,
)
def test_flush(self):
client = _Client(self.PROJECT)
name = "python_logger"
transport, _ = self._make_one(client, name)
transport.flush()
transport.worker.flush.assert_called()
def test_worker(self):
client = _Client(self.PROJECT)
name = "python_logger"
batch_size = 30
grace_period = 20.0
max_latency = 0.1
transport, worker = self._make_one(
client,
name,
grace_period=grace_period,
batch_size=batch_size,
max_latency=max_latency,
)
worker_grace_period = worker.call_args[1]["grace_period"] # **kwargs.
worker_batch_size = worker.call_args[1]["max_batch_size"]
worker_max_latency = worker.call_args[1]["max_latency"]
self.assertEqual(worker_grace_period, grace_period)
self.assertEqual(worker_batch_size, batch_size)
self.assertEqual(worker_max_latency, max_latency)
class Test_Worker(unittest.TestCase):
NAME = "python_logger"
@staticmethod
def _get_target_class():
from google.cloud.logging_v2.handlers.transports import background_thread
return background_thread._Worker
def _make_one(self, *args, **kw):
return self._get_target_class()(*args, **kw)
def _start_with_thread_patch(self, worker):
with mock.patch("threading.Thread", new=_Thread) as thread_mock:
with mock.patch("atexit.register") as atexit_mock:
worker.start()
return thread_mock, atexit_mock
def test_constructor(self):
logger = _Logger(self.NAME)
grace_period = 50
max_batch_size = 50
max_latency = 0.1
worker = self._make_one(
logger,
grace_period=grace_period,
max_batch_size=max_batch_size,
max_latency=max_latency,
)
self.assertEqual(worker._cloud_logger, logger)
self.assertEqual(worker._grace_period, grace_period)
self.assertEqual(worker._max_batch_size, max_batch_size)
self.assertEqual(worker._max_latency, max_latency)
self.assertFalse(worker.is_alive)
self.assertIsNone(worker._thread)
def test_start(self):
from google.cloud.logging_v2.handlers.transports import background_thread
worker = self._make_one(_Logger(self.NAME))
_, atexit_mock = self._start_with_thread_patch(worker)
self.assertTrue(worker.is_alive)
self.assertIsNotNone(worker._thread)
self.assertTrue(worker._thread.daemon)
self.assertEqual(worker._thread._target, worker._thread_main)
self.assertEqual(worker._thread._name, background_thread._WORKER_THREAD_NAME)
atexit_mock.assert_called_once_with(worker._main_thread_terminated)
# Calling start again should not start a new thread.
current_thread = worker._thread
self._start_with_thread_patch(worker)
self.assertIs(current_thread, worker._thread)
def test_start_not_registering_exit_callback(self):
from google.cloud.logging_v2.handlers.transports import background_thread
worker = self._make_one(_Logger(self.NAME), register_exit_callback=False)
_, atexit_mock = self._start_with_thread_patch(worker)
self.assertTrue(worker.is_alive)
self.assertIsNotNone(worker._thread)
self.assertTrue(worker._thread.daemon)
self.assertEqual(worker._thread._target, worker._thread_main)
self.assertEqual(worker._thread._name, background_thread._WORKER_THREAD_NAME)
atexit_mock.assert_not_called()
# Calling start again should not start a new thread.
current_thread = worker._thread
self._start_with_thread_patch(worker)
self.assertIs(current_thread, worker._thread)
def test_stop(self):
from google.cloud.logging_v2.handlers.transports import background_thread
grace_period = 5.0
worker = self._make_one(_Logger(self.NAME))
self._start_with_thread_patch(worker)
thread = worker._thread
worker.stop(grace_period=grace_period)
self.assertEqual(worker._queue.qsize(), 1)
self.assertEqual(worker._queue.get(), background_thread._WORKER_TERMINATOR)
self.assertFalse(worker.is_alive)
self.assertIsNone(worker._thread)
self.assertEqual(thread._timeout, grace_period)
# Stopping twice should not be an error
worker.stop()
def test_stop_no_grace(self):
worker = self._make_one(_Logger(self.NAME))
self._start_with_thread_patch(worker)
thread = worker._thread
worker.stop()
self.assertEqual(thread._timeout, None)
def test__main_thread_terminated(self):
worker = self._make_one(_Logger(self.NAME))
self._start_with_thread_patch(worker)
worker._main_thread_terminated()
self.assertFalse(worker.is_alive)
# Calling twice should not be an error
worker._main_thread_terminated()
def test__main_thread_terminated_non_empty_queue(self):
worker = self._make_one(_Logger(self.NAME))
self._start_with_thread_patch(worker)
record = mock.Mock()
record.created = time.time()
worker.enqueue(record, "")
worker._main_thread_terminated()
self.assertFalse(worker.is_alive)
def test__main_thread_terminated_did_not_join(self):
worker = self._make_one(_Logger(self.NAME))
self._start_with_thread_patch(worker)
worker._thread._terminate_on_join = False
record = mock.Mock()
record.created = time.time()
worker.enqueue(record, "")
worker._main_thread_terminated()
self.assertFalse(worker.is_alive)
@staticmethod
def _enqueue_record(worker, message, levelno=logging.INFO, **kw):
record = logging.LogRecord("testing", levelno, None, None, message, None, None)
worker.enqueue(record, message, **kw)
def test_enqueue_defaults(self):
import datetime
from google.cloud.logging_v2._helpers import LogSeverity
worker = self._make_one(_Logger(self.NAME))
self.assertTrue(worker._queue.empty())
message = "TEST SEVERITY"
self._enqueue_record(worker, message)
entry = worker._queue.get_nowait()
self.assertEqual(entry["message"], message)
self.assertEqual(entry["severity"], LogSeverity.INFO)
self.assertIsInstance(entry["timestamp"], datetime.datetime)
self.assertNotIn("resource", entry.keys())
self.assertNotIn("trace", entry.keys())
self.assertNotIn("span_id", entry.keys())
self.assertNotIn("http_request", entry.keys())
self.assertEqual(entry["labels"], {"python_logger": "testing"})
def test_enqueue_explicit(self):
import datetime
from google.cloud.logging_v2._helpers import LogSeverity
worker = self._make_one(_Logger(self.NAME))
self.assertTrue(worker._queue.empty())
message = "TEST SEVERITY"
resource = object()
labels = {"foo": "bar"}
trace = "TRACE"
span_id = "SPAN_ID"
self._enqueue_record(
worker,
message,
levelno=logging.ERROR,
resource=resource,
labels=labels,
trace=trace,
span_id=span_id,
)
entry = worker._queue.get_nowait()
self.assertEqual(entry["message"], message)
self.assertEqual(entry["severity"], LogSeverity.ERROR)
self.assertIs(entry["resource"], resource)
self.assertEqual(entry["labels"], {**labels, "python_logger": "testing"})
self.assertIs(entry["trace"], trace)
self.assertIs(entry["span_id"], span_id)
self.assertIsInstance(entry["timestamp"], datetime.datetime)
def test__thread_main(self):
from google.cloud.logging_v2.handlers.transports import background_thread
worker = self._make_one(_Logger(self.NAME))
# Enqueue two records and the termination signal.
self._enqueue_record(worker, "1")
self._enqueue_record(worker, "2")
worker._queue.put_nowait(background_thread._WORKER_TERMINATOR)
worker._thread_main()
self.assertTrue(worker._cloud_logger._batch.commit_called)
self.assertEqual(worker._cloud_logger._batch.commit_count, 2)
self.assertEqual(worker._queue.qsize(), 0)
def test__thread_main_error(self):
from google.cloud.logging_v2.handlers.transports import background_thread
worker = self._make_one(_Logger(self.NAME))
worker._cloud_logger._batch_cls = _RaisingBatch
# Enqueue one record and the termination signal.
self._enqueue_record(worker, "1")
worker._queue.put_nowait(background_thread._WORKER_TERMINATOR)
worker._thread_main()
self.assertTrue(worker._cloud_logger._batch.commit_called)
self.assertEqual(worker._queue.qsize(), 0)
def test__thread_main_batches(self):
from google.cloud.logging_v2.handlers.transports import background_thread
worker = self._make_one(_Logger(self.NAME), max_batch_size=2)
# Enqueue three records and the termination signal. This should be
# enough to perform two separate batches and a third loop with just
# the exit.
self._enqueue_record(worker, "1")
self._enqueue_record(worker, "2")
self._enqueue_record(worker, "3")
self._enqueue_record(worker, "4")
worker._queue.put_nowait(background_thread._WORKER_TERMINATOR)
worker._thread_main()
# The last batch should not have been executed because it had no items.
self.assertFalse(worker._cloud_logger._batch.commit_called)
self.assertEqual(worker._queue.qsize(), 0)
@mock.patch("time.time", autospec=True, return_value=1)
def test__thread_main_max_latency(self, time):
# Note: this test is a bit brittle as it assumes the operation of
# _get_many invokes queue.get() followed by queue._get(). It fails
# the "change detector" test in that way. However, this is still a
# useful test to verify the queue timeout is appropriately calculated.
from google.cloud.logging_v2.handlers.transports import background_thread
# Use monotonically increasing time.
time.side_effect = range(1, 6)
worker = self._make_one(_Logger(self.NAME), max_latency=2, max_batch_size=10)
worker._queue = mock.create_autospec(queue.Queue, instance=True)
worker._queue.get.side_effect = [
{"message": 1}, # Single record.
queue.Empty(), # Emulate a queue.get() timeout.
{"message": "2"}, # Second record.
background_thread._WORKER_TERMINATOR, # Stop the thread.
queue.Empty(), # Emulate a queue.get() timeout.
]
worker._thread_main()
self.assertEqual(worker._cloud_logger._num_batches, 2)
self.assertTrue(worker._cloud_logger._batch.commit_called)
self.assertEqual(worker._cloud_logger._batch.commit_count, 1)
# Time should have been called five times.
#
# For the first batch, it should have been called:
# * Once to get the start time. (1)
# * Once to get the elapsed time while grabbing the second item.
# (2)
#
# For the second batch, it should have been called:
# * Once to get start time. (3)
# * Once to get the elapsed time while grabbing the second item.
# (3)
# * Once to get the elapsed time while grabbing the final
# item. (4)
# * Once final time to get the elapsed time while receiving
# the empty queue.
#
self.assertEqual(time.call_count, 5)
# Queue.get should've been called 5 times as well, but with different
# timeouts due to the monotonically increasing time.
#
# For the first batch, it will be called once without a timeout
# (for the first item) and then with timeout=1, as start will be
# 1 and now will be 2.
#
# For the second batch, it will be called once without a timeout
# (for the first item) and then with timeout=1, as start will be
# 3 and now will be 4, and finally with timeout=0 as start will be 3
# and now will be 5.
#
worker._queue.get.assert_has_calls(
[
mock.call(),
mock.call(timeout=1),
mock.call(),
mock.call(timeout=1),
mock.call(timeout=0),
]
)
def test_flush(self):
worker = self._make_one(_Logger(self.NAME))
worker._queue = mock.Mock(spec=queue.Queue)
# Queue is empty, should not block.
worker.flush()
worker._queue.join.assert_called()
class _Thread(object):
def __init__(self, target, name):
self._target = target
self._name = name
self._timeout = None
self._terminate_on_join = True
self.daemon = False
def is_alive(self):
return self._is_alive
def start(self):
self._is_alive = True
def stop(self):
self._is_alive = False
def join(self, timeout=None):
self._timeout = timeout
if self._terminate_on_join:
self.stop()
class _Batch(object):
def __init__(self):
self.entries = []
self.commit_called = False
self.commit_count = None
def log(
self,
message,
severity=logging.INFO,
resource=None,
labels=None,
trace=None,
span_id=None,
timestamp=None,
http_request=None,
):
from google.cloud.logging_v2.logger import _GLOBAL_RESOURCE
assert resource is None
resource = _GLOBAL_RESOURCE
self.log_called_with = (message, severity, resource, labels, trace, span_id)
self.entries.append(message)
def commit(self):
self.commit_called = True
self.commit_count = len(self.entries)
del self.entries[:]
class _RaisingBatch(_Batch):
def commit(self):
self.commit_called = True
raise ValueError("This batch raises on commit.")
class _Logger(object):
def __init__(self, name, resource=None):
self.name = name
self._batch_cls = _Batch
self._batch = None
self._num_batches = 0
self.resource = resource
def batch(self):
self._batch = self._batch_cls()
self._num_batches += 1
return self._batch
class _Client(object):
def __init__(self, project, _http=None, credentials=None):
import mock
self.project = project
self._http = _http
self._credentials = credentials
self._connection = mock.Mock(credentials=credentials, spec=["credentials"])
def logger(self, name, resource=None): # pylint: disable=unused-argument
self._logger = _Logger(name, resource=resource)
return self._logger