-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_batch_processor.py
More file actions
204 lines (145 loc) · 6.64 KB
/
test_batch_processor.py
File metadata and controls
204 lines (145 loc) · 6.64 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
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
"""Tests for BatchSpanProcessor event loop management."""
from __future__ import annotations
import asyncio
import time
from typing import Any
from drift.core.batch_processor import BatchSpanProcessor, BatchSpanProcessorConfig
from tests.utils.test_helpers import create_test_span
class MockExporter:
"""Mock exporter that returns configurable adapters.
Implements the minimal interface needed by BatchSpanProcessor.
"""
def __init__(self, adapters: list[Any]) -> None:
self._adapters = adapters
def get_adapters(self) -> list[Any]:
return self._adapters
def _create_processor(exporter: MockExporter, config: BatchSpanProcessorConfig) -> BatchSpanProcessor:
"""Create BatchSpanProcessor with mock exporter (type-safe wrapper)."""
# MockExporter implements get_adapters() which is all BatchSpanProcessor needs
return BatchSpanProcessor(exporter, config) # type: ignore[arg-type]
class TestBatchSpanProcessorEventLoop:
"""Test event loop reuse in BatchSpanProcessor."""
def test_reuses_event_loop_across_exports(self):
"""Verify the same event loop is reused for multiple exports."""
loops_used = []
class TrackingAdapter:
name = "tracking"
async def export_spans(self, spans):
loops_used.append(asyncio.get_event_loop())
adapter = TrackingAdapter()
exporter = MockExporter([adapter])
config = BatchSpanProcessorConfig(
scheduled_delay_seconds=0.1,
max_export_batch_size=1,
)
processor = _create_processor(exporter, config)
processor.start()
# Add spans to trigger multiple exports
processor.add_span(create_test_span(name="span-1"))
processor.add_span(create_test_span(name="span-2"))
time.sleep(0.3) # Allow exports to happen
processor.stop()
# All exports should use the same event loop
assert len(loops_used) >= 2, f"Expected at least 2 exports, got {len(loops_used)}"
assert all(loop is loops_used[0] for loop in loops_used), "Different event loops were used"
def test_force_flush_works_after_thread_shutdown(self):
"""Verify force_flush creates temporary loop when thread loop is closed."""
exported_spans = []
class CollectingAdapter:
name = "collecting"
async def export_spans(self, spans):
exported_spans.extend(spans)
adapter = CollectingAdapter()
exporter = MockExporter([adapter])
# Long delay so spans won't export before stop()
config = BatchSpanProcessorConfig(
scheduled_delay_seconds=10.0,
max_export_batch_size=100,
)
processor = _create_processor(exporter, config)
processor.start()
# Add spans
for i in range(5):
processor.add_span(create_test_span(name=f"span-{i}"))
# Stop immediately - force_flush should handle export
processor.stop()
assert len(exported_spans) == 5, f"Expected 5 spans, got {len(exported_spans)}"
def test_event_loop_closed_after_stop(self):
"""Verify the thread's event loop is properly cleaned up after stop."""
class NoOpAdapter:
name = "noop"
async def export_spans(self, spans):
pass
adapter = NoOpAdapter()
exporter = MockExporter([adapter])
config = BatchSpanProcessorConfig(scheduled_delay_seconds=0.1)
processor = _create_processor(exporter, config)
processor.start()
time.sleep(0.05) # Let thread start
# Thread loop should exist while running
assert processor._thread_loop is not None
processor.stop()
# Thread loop should be cleaned up after stop
assert processor._thread_loop is None
def test_sync_adapter_does_not_use_event_loop(self):
"""Verify sync adapters work without event loop involvement."""
exported_spans = []
class SyncAdapter:
name = "sync"
def export_spans(self, spans):
# Sync method - not a coroutine
exported_spans.extend(spans)
adapter = SyncAdapter()
exporter = MockExporter([adapter])
config = BatchSpanProcessorConfig(
scheduled_delay_seconds=0.1,
max_export_batch_size=5,
)
processor = _create_processor(exporter, config)
processor.start()
for i in range(3):
processor.add_span(create_test_span(name=f"span-{i}"))
time.sleep(0.2)
processor.stop()
assert len(exported_spans) == 3
def test_force_flush_from_different_thread_uses_temporary_loop(self):
"""Verify force_flush doesn't reuse export thread's loop from a different thread.
This tests the scenario where stop() times out and force_flush runs while
the export thread is still alive. Using the export thread's loop from
another thread would cause RuntimeError.
"""
loops_used = []
export_thread_loop = None
class TrackingAdapter:
name = "tracking"
async def export_spans(self, spans):
loops_used.append(asyncio.get_event_loop())
adapter = TrackingAdapter()
# Use a slow adapter that blocks the export thread
class SlowAdapter:
name = "slow"
async def export_spans(self, spans):
nonlocal export_thread_loop
export_thread_loop = asyncio.get_event_loop()
# Block for a while to simulate slow export
await asyncio.sleep(0.5)
slow_adapter = SlowAdapter()
exporter_with_slow = MockExporter([slow_adapter, adapter])
config = BatchSpanProcessorConfig(
scheduled_delay_seconds=0.05,
max_export_batch_size=1,
)
processor = _create_processor(exporter_with_slow, config)
processor.start()
# Add a span to trigger export
processor.add_span(create_test_span(name="span-1"))
time.sleep(0.1) # Let export start
# Stop with very short timeout - export thread will still be running
processor.stop(timeout=0.01)
# Add more spans and force flush from main thread
processor._queue.append(create_test_span(name="span-2"))
processor._force_flush()
# Verify: loops used during force_flush should NOT be the export thread's loop
# (because force_flush runs on the main thread)
main_thread_loops = [loop for loop in loops_used if loop is not export_thread_loop]
assert len(main_thread_loops) > 0, "force_flush should have used a different loop"