|
1 | 1 | import logging |
2 | 2 | import queue |
3 | | -from unittest.mock import Mock |
| 3 | +from unittest.mock import Mock, patch |
4 | 4 |
|
5 | 5 | import pytest |
6 | 6 |
|
7 | | -from influxdb_client_3.write_client.client.util.multiprocessing_helper import MultiprocessingWriter, _PoisonPill |
| 7 | +from influxdb_client_3.write_client.client.util.multiprocessing_helper import ( |
| 8 | + MultiprocessingWriter, |
| 9 | + _PoisonPill, |
| 10 | + _error_callback, |
| 11 | + _retry_callback, |
| 12 | + _success_callback, |
| 13 | +) |
8 | 14 |
|
9 | 15 |
|
10 | 16 | def make_writer(**kwargs): |
@@ -121,6 +127,37 @@ def test_get_start_processing_method_returns_context_method(): |
121 | 127 | assert make_writer().get_start_processing_method() == "spawn" |
122 | 128 |
|
123 | 129 |
|
| 130 | +def test_default_callbacks_log_batch_events(caplog): |
| 131 | + caplog.set_level(logging.DEBUG) |
| 132 | + conf = ("database", "org", "precision") |
| 133 | + exception = ValueError("write failed") |
| 134 | + |
| 135 | + _success_callback(conf, "data") |
| 136 | + _error_callback(conf, "data", exception) |
| 137 | + _retry_callback(conf, "data", exception) |
| 138 | + |
| 139 | + assert "Written batch" in caplog.text |
| 140 | + assert "Cannot write batch" in caplog.text |
| 141 | + assert "Retryable error occurs" in caplog.text |
| 142 | + |
| 143 | + |
| 144 | +def test_constructor_builds_rest_client_from_host_and_token(): |
| 145 | + rest_client_path = "influxdb_client_3.write_client.client.util.multiprocessing_helper.rest_client.RestClient" |
| 146 | + with patch(rest_client_path) as rest_client: |
| 147 | + writer = MultiprocessingWriter( |
| 148 | + start_method="spawn", |
| 149 | + host="http://localhost:8086", |
| 150 | + token="my-token", |
| 151 | + database="test", |
| 152 | + ) |
| 153 | + |
| 154 | + rest_client.assert_called_once_with( |
| 155 | + base_url="http://localhost:8086", |
| 156 | + default_header={"Authorization": "Token my-token"}, |
| 157 | + ) |
| 158 | + writer.close() |
| 159 | + |
| 160 | + |
124 | 161 | def test_worker_timeout_closes_api_and_invokes_callback_once(): |
125 | 162 | writer = make_writer(process_ttl=0.01) |
126 | 163 | writer.queue_ = Mock() |
|
0 commit comments