-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathintegration_utils.py
More file actions
487 lines (421 loc) · 16.1 KB
/
integration_utils.py
File metadata and controls
487 lines (421 loc) · 16.1 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
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
"""Integration testing framework"""
import asyncio
import random
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from dataclasses import dataclass, field
from datetime import timedelta
from typing import Any, Callable
from confluent_kafka.admin import AdminClient, NewTopic
from retriable_kafka_client import (
BaseConsumer,
BaseProducer,
ConsumerConfig,
ConsumerThread,
ConsumeTopicConfig,
ProducerConfig,
)
from retriable_kafka_client.kafka_settings import KafkaOptions
@dataclass
class RandomDelay:
"""
Class for creating a random delay from uniform range specified
by the upper and lower end of the range.
"""
min_delay: float
max_delay: float
def get_delay(self) -> float:
"""Generate the delay"""
return random.uniform(self.min_delay, self.max_delay)
class MessageGenerator:
"""
This class helps to generate messages with predictable IDs for tracking.
"""
def __init__(self) -> None:
self._call_count = 0
def generate(
self, count: int, extra_fields: dict[str, Any] | None = None
) -> list[dict[str, Any]]:
"""
Generate a chunk of messages.
Args:
count: Number of messages to generate in this chunk
extra_fields: Extra fields to add to the generated messages
"""
extra_fields = extra_fields or {}
result = []
for _ in range(count):
result.append(
{
"id": self._call_count,
"message": "This is a test message",
**extra_fields,
}
)
self._call_count += 1
return result
class MessageTracker:
"""
Class for tracking results of message processing
Attributes:
call_counts:
Tracks the number of calls on each message,
the keys are IDs in the messages, the values
are counts of processing
success_counts:
Tracks the number of successful processing calls,
format is the same as call_counts
lock:
Threading lock that should be used when manipulating
the other attributes
"""
def __init__(self, message_count: int, topics: int, lock: threading.Lock) -> None:
"""
Initialize a message tracker.
Args:
message_count:
Number of messages that is expected to be sent and received
in each topic
topics:
Number of topics used for producing and consuming (doesn't
include retry topics)
lock:
threading lock for avoiding race conditions when populating
or reading results
"""
self.call_counts: dict[int, int] = {}
self.success_counts: dict[int, int] = {}
self._message_count = message_count
self._topics = topics
self.lock = lock
async def wait_for(
self, func: Callable[[dict[int, int], dict[int, int]], bool], max_timeout: float
) -> bool:
"""
Wait until a certain condition is met or until this times out.
Args:
func:
A callable that determines if the conditions are met.
This callable must return True if the condition is met,
False otherwise. To determine the result, this function
accepts two parameters. The first parameter is the
call_counts attribute, the second one is the success_counts.
max_timeout:
Time in seconds to wait for the conditions to be met.
After this time frame is over, the function returns False.
Returns:
True when the condition is met, False if the timeout is reached
before the conditions are true.
"""
time_ref = time.perf_counter()
time_delta = 0
success = False
while time_delta < max_timeout:
with self.lock:
if func(self.call_counts, self.success_counts):
success = True
break
await asyncio.sleep(0.5)
time_delta = time.perf_counter() - time_ref
if not success:
print("Timed out after %s seconds." % time_delta)
return success
class MockTarget:
"""
Mock target for consumers to track result of tests
"""
def __init__(
self,
delay: float | RandomDelay,
fail_chance_on_first: float,
tracker: MessageTracker,
fail_consistently: bool = False,
) -> None:
"""
Initialize a mock target.
Args:
delay:
The simulated delay before this message is processed (or failed)
fail_chance_on_first:
Number between 0 and 1 that indicates the probability of failing
on first processing of the message
tracker:
The tracker object to keep track of results of processing
fail_consistently:
If True, every attempt to call this object will raise a ValueError
"""
self.delay = delay
self.fail_chance_on_first = fail_chance_on_first
self.tracker = tracker
self.fail_consistently = fail_consistently
def __call__(self, message: dict[str, Any]) -> None:
"""
Simulate the execution of this target
Args:
message:
The message to process. Must include
the key "id" with an integer value
Raises:
ValueError: if simulated failure occurs
"""
if self.delay:
if isinstance(self.delay, RandomDelay):
time.sleep(self.delay.get_delay())
elif isinstance(self.delay, float):
time.sleep(self.delay)
message_id = message["id"]
with self.tracker.lock:
call_count = self.tracker.call_counts.get(message_id, 0)
self.tracker.call_counts[message_id] = call_count + 1
if self.fail_consistently or (
call_count == 0 and (random.random() < self.fail_chance_on_first)
):
raise ValueError("Simulated error")
with self.tracker.lock:
success_count = self.tracker.success_counts.get(message_id, 0)
self.tracker.success_counts[message_id] = success_count + 1
class ConsumerHandle:
"""
Handle for a consumer that bundles the thread and executor together.
Provides a clean way to stop a consumer mid-test.
"""
def __init__(
self, consumer_thread: ConsumerThread, executor: ThreadPoolExecutor
) -> None:
self._consumer_thread = consumer_thread
self._executor = executor
self._stopped = False
def stop(self, timeout: float = 5) -> None:
"""
Stop the consumer and shut down its executor.
This ensures the consumer properly leaves the consumer group.
"""
if self._stopped:
return
# Stop the consumer thread first
self._consumer_thread.stop()
self._consumer_thread.join(timeout=timeout)
# Then shutdown the executor to complete any in-flight tasks
self._executor.shutdown(wait=True)
self._stopped = True
@property
def is_stopped(self) -> bool:
return self._stopped
@dataclass
class ScaffoldConfig:
"""Configuration for the test harness"""
topics: list[ConsumeTopicConfig]
group_id: str
timeout: float = 15.0
split_messages: bool = False
max_chunk_reassembly_wait_time: timedelta = field(default=timedelta(seconds=10))
additional_settings: dict[str, Any] = field(default_factory=dict)
topic_config: dict[str, str] = field(default_factory=dict)
class IntegrationTestScaffold:
"""
A unified test harness that bundles topic creation, producer, consumer,
tracker, and generator together for cleaner integration tests.
Usage:
async with IntegrationTestHarness(kafka_config, admin_client, config) as harness:
harness.get_consumer_thread() # Start a consumer
await harness.send_messages(10)
success = await harness.wait_for_success()
assert success
"""
def __init__(
self,
kafka_config: dict[str, Any],
admin_client: AdminClient,
config: ScaffoldConfig,
) -> None:
self.kafka_config = kafka_config
self.admin_client = admin_client
self.config = config
# Internal state
self._lock = threading.Lock()
self._producer: BaseProducer | None = None
# Track created consumer resources for cleanup
self._consumer_handles: list[ConsumerHandle] = []
# Track how many messages have been sent
self.messages_sent: int = 0
# Public components
self.tracker = MessageTracker(
message_count=0, # Will be updated when send_messages is called
topics=len(config.topics),
lock=self._lock,
)
self.generator = MessageGenerator()
def _create_topics(self) -> None:
"""Create all topics needed for this test"""
all_topic_names: list[str] = []
for tc in self.config.topics:
all_topic_names.append(tc.base_topic)
if tc.retry_topic:
all_topic_names.append(tc.retry_topic)
new_topics = [
NewTopic(
topic,
num_partitions=1,
replication_factor=1,
config=self.config.topic_config,
)
for topic in all_topic_names
]
fs = self.admin_client.create_topics(new_topics)
for topic, f in fs.items():
try:
f.result()
except Exception:
# Topic might already exist
pass
def _create_producer(self) -> BaseProducer:
"""Create the producer"""
# Producer sends to all base topics
producer_topics = [tc.base_topic for tc in self.config.topics]
producer_config = ProducerConfig(
kafka_hosts=[self.kafka_config[KafkaOptions.KAFKA_NODES]],
topics=producer_topics,
username=self.kafka_config[KafkaOptions.USERNAME],
password=self.kafka_config[KafkaOptions.PASSWORD],
fallback_base=0.1,
split_messages=self.config.split_messages,
additional_settings={
KafkaOptions.SECURITY_PROTO: "SASL_PLAINTEXT",
**self.config.additional_settings,
},
)
return BaseProducer(producer_config)
def start_consumer(
self,
delay: float | RandomDelay = 0,
fail_chance_on_first: float = 0,
fail_consistently: bool = False,
max_concurrency: int = 4,
max_workers: int = 2,
filter_function: Callable[[Any], bool] | None = None,
override_topics: list[ConsumeTopicConfig] | None = None,
) -> ConsumerHandle:
"""
Create and start a consumer with the specified configuration.
Args:
delay: Processing delay (fixed or random range)
fail_chance_on_first: Probability (0-1) of failing first attempt
fail_consistently: If True, always fail processing
max_concurrency: Consumer concurrency limit
max_workers: Thread pool size
filter_function: Filters messages based on the user-provided function
override_topics: If provided, override the topic config
Returns:
A ConsumerHandle that can be used to stop the consumer.
Call handle.stop() to cleanly stop the consumer and its executor.
"""
# Create target with shared tracker
target = MockTarget(
delay=delay,
fail_chance_on_first=fail_chance_on_first,
tracker=self.tracker,
fail_consistently=fail_consistently,
)
# Create executor
executor = ThreadPoolExecutor(max_workers=max_workers)
# Create consumer
consumer_config = ConsumerConfig(
kafka_hosts=[self.kafka_config[KafkaOptions.KAFKA_NODES]],
topics=self.config.topics if override_topics is None else override_topics,
username=self.kafka_config[KafkaOptions.USERNAME],
password=self.kafka_config[KafkaOptions.PASSWORD],
group_id=self.config.group_id,
target=target,
filter_function=filter_function,
max_chunk_reassembly_wait_time=self.config.max_chunk_reassembly_wait_time,
additional_settings={
KafkaOptions.SECURITY_PROTO: "SASL_PLAINTEXT",
**self.config.additional_settings,
},
)
consumer = BaseConsumer(
consumer_config,
executor,
max_concurrency=max_concurrency,
)
# Create and start consumer thread
consumer_thread = ConsumerThread(consumer)
consumer_thread.start()
# Create handle and track it
handle = ConsumerHandle(consumer_thread, executor)
self._consumer_handles.append(handle)
return handle
async def __aenter__(self) -> "IntegrationTestScaffold":
"""Set up the test harness (topics and producer only)"""
# Create topics
self._create_topics()
# Create producer
self._producer = self._create_producer()
return self
async def __aexit__(self, exc_type: Any, exc_val: Any, exc_tb: Any) -> None:
"""Clean up all resources"""
# Stop all consumer handles (this stops threads and shuts down executors)
for handle in self._consumer_handles:
try:
handle.stop()
except Exception:
pass
async def send_messages(
self,
count: int,
extra_fields: dict[str, Any] | None = None,
headers: dict[str, bytes] | None = None,
) -> list[dict[str, Any]]:
"""
Generate and send messages.
Args:
count: Number of messages to send.
extra_fields: Extra fields to add to each message.
headers: Message headers (optional parameter)
Returns:
The list of messages that were sent.
"""
if self._producer is None:
raise RuntimeError("Harness not started. Use 'async with' context manager.")
messages = self.generator.generate(count, extra_fields=extra_fields)
for msg in messages:
await self._producer.send(msg, headers=headers)
# Update tracking
self.messages_sent += count
self.tracker._message_count = self.messages_sent
self._producer.close()
return messages
async def wait_for_success(self, timeout: float | None = None) -> bool:
"""
Wait for all messages to be successfully processed.
Args:
timeout: Max time to wait. Defaults to config.timeout.
Returns:
True if all messages processed successfully, False on timeout.
"""
def _ensure_success(_: dict[int, int], success_counts: dict[int, int]) -> bool:
if len(success_counts) == self.messages_sent:
number_of_topics = len(self.config.topics)
return all(
call_count == number_of_topics
for call_count in success_counts.values()
)
return False
max_timeout = timeout if timeout is not None else self.config.timeout
return await self.wait_for(_ensure_success, max_timeout)
async def wait_for(
self,
condition: Callable[[dict[int, int], dict[int, int]], bool],
timeout: float | None = None,
) -> bool:
"""
Wait for a custom condition.
Args:
condition: Function that takes (call_counts, success_counts) and returns bool.
timeout: Max time to wait. Defaults to config.timeout.
Returns:
True if condition met, False on timeout.
"""
max_timeout = timeout if timeout is not None else self.config.timeout
return await self.tracker.wait_for(condition, max_timeout)