|
60 | 60 | import weakref |
61 | 61 |
|
62 | 62 | from http.server import HTTPServer, BaseHTTPRequestHandler |
| 63 | +from unittest.mock import patch |
63 | 64 | from urllib.parse import urlparse, parse_qs |
64 | 65 | from socketserver import (ThreadingUDPServer, DatagramRequestHandler, |
65 | 66 | ThreadingTCPServer, StreamRequestHandler) |
@@ -4552,6 +4553,44 @@ def test_issue_89047(self): |
4552 | 4553 | s = f.format(r) |
4553 | 4554 | self.assertNotIn('.1000', s) |
4554 | 4555 |
|
| 4556 | + def test_msecs_has_no_floating_point_precision_loss(self): |
| 4557 | + # See issue gh-102402 |
| 4558 | + tests = ( |
| 4559 | + # time_ns is approx. 2023-03-04 04:25:20 UTC |
| 4560 | + # (time_ns, expected_msecs_value) |
| 4561 | + (1_677_902_297_100_000_000, 100.0), # exactly 100ms |
| 4562 | + (1_677_903_920_999_998_503, 999.0), # check truncating doesn't round |
| 4563 | + (1_677_903_920_000_998_503, 0.0), # check truncating doesn't round |
| 4564 | + ) |
| 4565 | + for ns, want in tests: |
| 4566 | + with patch('time.time_ns') as patched_ns: |
| 4567 | + patched_ns.return_value = ns |
| 4568 | + record = logging.makeLogRecord({'msg': 'test'}) |
| 4569 | + self.assertEqual(record.msecs, want) |
| 4570 | + self.assertEqual(record.created, ns / 1e9) |
| 4571 | + |
| 4572 | + def test_relativeCreated_has_higher_precision(self): |
| 4573 | + # See issue gh-102402 |
| 4574 | + ns = 1_677_903_920_000_998_503 # approx. 2023-03-04 04:25:20 UTC |
| 4575 | + offsets_ns = (200, 500, 12_354, 99_999, 1_677_903_456_999_123_456) |
| 4576 | + orig_modules = import_helper._save_and_remove_modules(['logging']) |
| 4577 | + try: |
| 4578 | + with patch("time.time_ns") as patched_ns: |
| 4579 | + # mock for module import |
| 4580 | + patched_ns.return_value = ns |
| 4581 | + import logging |
| 4582 | + for offset_ns in offsets_ns: |
| 4583 | + new_ns = ns + offset_ns |
| 4584 | + # mock for log record creation |
| 4585 | + patched_ns.return_value = new_ns |
| 4586 | + record = logging.makeLogRecord({'msg': 'test'}) |
| 4587 | + self.assertAlmostEqual(record.created, new_ns / 1e9, places=6) |
| 4588 | + # After PR gh-102412, precision (places) increases from 3 to 7 |
| 4589 | + self.assertAlmostEqual(record.relativeCreated, offset_ns / 1e6, places=7) |
| 4590 | + finally: |
| 4591 | + import_helper._save_and_remove_modules(['logging']) |
| 4592 | + sys.modules.update(orig_modules) |
| 4593 | + |
4555 | 4594 |
|
4556 | 4595 | class TestBufferingFormatter(logging.BufferingFormatter): |
4557 | 4596 | def formatHeader(self, records): |
|
0 commit comments