askrobots / dbbasic Public

Model-config web apps: describe the app in configuration instead of writing code. Built on Polars and DuckDB.

0 downloads

dbbasic/tests/unit/test_dbbasic_channels.py
Download zip
Clone
Clone with git
git clone https://gitstable.com/git/askrobots/dbbasic.git
HTTPS
https://gitstable.com/git/askrobots/dbbasic.git
SSH
ssh://git@gitstable.com:2222/askrobots/dbbasic.git

Cloning needs no sign-in. Pushing over HTTPS asks for your username and an access token. SSH needs your key under SSH keys.

danq9 Add DBBasic crate engine logo and update README 0d628db 1 year, 2 months ago History
test_dbbasic_channels.py · 18.2 KB · 588 lines
1#!/usr/bin/env python3
2"""
3Comprehensive test suite for DBBasic Channels and Task Queue
4Tests performance, concurrency, reliability, and compares with PostgreSQL benchmarks
5"""
6
7import pytest
8import asyncio
9import time
10import json
11import uuid
12from typing import List, Dict, Any
13import duckdb
14import statistics
15from datetime import datetime, timedelta
16
17# Import our systems
18from dbbasic_channels import DBBasicChannels, Channel
19from dbbasic_task_queue import DBBasicTaskQueue, TaskWorker
20
21
22class TestDBBasicChannels:
23 """Test suite for DBBasic Channels pub/sub system"""
24
25 @pytest.fixture
26 async def channels(self):
27 """Create a test channels instance"""
28 channels = DBBasicChannels(db_path=":memory:") # In-memory for tests
29 yield channels
30 # Cleanup
31 channels.conn.close()
32
33 @pytest.mark.asyncio
34 async def test_publish_single_message(self, channels):
35 """Test publishing a single message"""
36 message_id = await channels.publish(
37 Channel.ORDERS.value,
38 "new_order",
39 {"order_id": "TEST123", "total": 100.00}
40 )
41
42 assert message_id is not None
43 assert len(message_id) == 36 # UUID length
44
45 # Verify in database
46 result = channels.conn.execute(
47 "SELECT * FROM messages WHERE id = ?", [message_id]
48 ).fetchone()
49
50 assert result is not None
51 assert result[1] == Channel.ORDERS.value # channel
52 assert result[2] == "new_order" # message_type
53
54 @pytest.mark.asyncio
55 async def test_subscribe_and_receive(self, channels):
56 """Test subscribing to a channel and receiving messages"""
57 received_messages = []
58
59 async def test_callback(message):
60 received_messages.append(message)
61
62 # Subscribe to orders channel
63 subscriber_id = await channels.subscribe(
64 Channel.ORDERS.value,
65 test_callback,
66 "test_subscriber"
67 )
68
69 # Publish a message
70 await channels.publish(
71 Channel.ORDERS.value,
72 "test_message",
73 {"data": "test"}
74 )
75
76 # Give it time to process
77 await asyncio.sleep(0.1)
78
79 assert len(received_messages) == 1
80 assert received_messages[0]['channel'] == Channel.ORDERS.value
81 assert received_messages[0]['message_type'] == "test_message"
82
83 @pytest.mark.asyncio
84 async def test_wildcard_subscription(self, channels):
85 """Test subscribing to all channels with wildcard"""
86 all_messages = []
87
88 async def monitor_callback(message):
89 all_messages.append(message)
90
91 # Subscribe to all channels
92 await channels.subscribe("*", monitor_callback, "global_monitor")
93
94 # Publish to different channels
95 await channels.publish(Channel.ORDERS.value, "order", {"id": 1})
96 await channels.publish(Channel.SHIPPING.value, "ship", {"id": 2})
97 await channels.publish(Channel.FRAUD.value, "check", {"id": 3})
98
99 await asyncio.sleep(0.1)
100
101 # Should receive all messages
102 assert len(all_messages) == 3
103 channels_received = [msg['channel'] for msg in all_messages]
104 assert Channel.ORDERS.value in channels_received
105 assert Channel.SHIPPING.value in channels_received
106 assert Channel.FRAUD.value in channels_received
107
108 @pytest.mark.asyncio
109 async def test_channel_worker_processing(self, channels):
110 """Test worker watching and processing channel messages"""
111 processed = []
112
113 async def test_processor(message):
114 processed.append(message)
115 return {"status": "processed", "id": message['payload'].get('id')}
116
117 # Start worker in background
118 worker_task = asyncio.create_task(
119 channels.watch_channel(
120 Channel.ORDERS.value,
121 "test_worker",
122 test_processor
123 )
124 )
125
126 # Give worker time to start
127 await asyncio.sleep(0.1)
128
129 # Publish messages
130 for i in range(5):
131 await channels.publish(
132 Channel.ORDERS.value,
133 "process_me",
134 {"id": i, "data": f"test_{i}"}
135 )
136
137 # Wait for processing
138 await asyncio.sleep(0.5)
139
140 # Cancel worker
141 worker_task.cancel()
142 try:
143 await worker_task
144 except asyncio.CancelledError:
145 pass
146
147 # Check all were processed
148 assert len(processed) == 5
149 assert all(msg['message_type'] == "process_me" for msg in processed)
150
151 @pytest.mark.asyncio
152 async def test_message_ttl_expiration(self, channels):
153 """Test that messages expire after TTL"""
154 # Publish with short TTL
155 message_id = await channels.publish(
156 Channel.ORDERS.value,
157 "expires_soon",
158 {"data": "test"},
159 ttl_seconds=1
160 )
161
162 # Wait for expiration
163 await asyncio.sleep(2)
164
165 # Try to get the message - should be expired
166 result = channels.conn.execute("""
167 SELECT * FROM messages
168 WHERE id = ?
169 AND (created_at + INTERVAL '1 second' * ttl_seconds) > CURRENT_TIMESTAMP
170 """, [message_id]).fetchone()
171
172 assert result is None # Message should be expired
173
174 @pytest.mark.asyncio
175 async def test_concurrent_publishing(self, channels):
176 """Test concurrent publishing from multiple coroutines"""
177
178 async def publisher(channel, count):
179 for i in range(count):
180 await channels.publish(
181 channel,
182 f"msg_{i}",
183 {"index": i}
184 )
185
186 # Run multiple publishers concurrently
187 tasks = [
188 publisher(Channel.ORDERS.value, 10),
189 publisher(Channel.SHIPPING.value, 10),
190 publisher(Channel.FRAUD.value, 10)
191 ]
192
193 start_time = time.time()
194 await asyncio.gather(*tasks)
195 elapsed = time.time() - start_time
196
197 # Check all messages were inserted
198 count = channels.conn.execute(
199 "SELECT COUNT(*) FROM messages"
200 ).fetchone()[0]
201
202 assert count == 30
203 assert elapsed < 1.0 # Should be very fast
204
205 @pytest.mark.asyncio
206 async def test_channel_statistics(self, channels):
207 """Test channel statistics tracking"""
208 # Publish to various channels
209 await channels.publish(Channel.ORDERS.value, "order", {})
210 await channels.publish(Channel.ORDERS.value, "order", {})
211 await channels.publish(Channel.SHIPPING.value, "ship", {})
212
213 stats = channels.get_channel_stats()
214
215 assert stats[Channel.ORDERS.value]['total_messages'] >= 2
216 assert stats[Channel.SHIPPING.value]['total_messages'] >= 1
217
218
219class TestDBBasicTaskQueue:
220 """Test suite for DBBasic Task Queue"""
221
222 @pytest.fixture
223 def queue(self):
224 """Create a test task queue instance"""
225 queue = DBBasicTaskQueue(db_path=":memory:")
226 yield queue
227 queue.conn.close()
228
229 def test_add_single_task(self, queue):
230 """Test adding a single task"""
231 task_id = queue.add_task(
232 task_type="test_task",
233 payload={"key": "value"},
234 priority=5
235 )
236
237 assert task_id is not None
238
239 # Verify in database
240 result = queue.conn.execute(
241 "SELECT * FROM task_queue WHERE id = ?", [task_id]
242 ).fetchone()
243
244 assert result is not None
245 assert result[1] == "test_task" # task_type
246 assert result[4] == "pending" # status
247
248 def test_bulk_add_tasks(self, queue):
249 """Test bulk adding tasks - this tests our speed claims!"""
250 tasks = [
251 {
252 'task_type': 'bulk_test',
253 'service_name': 'test_service',
254 'payload': {'index': i},
255 'priority': i % 10
256 }
257 for i in range(10000)
258 ]
259
260 start_time = time.time()
261 count = queue.bulk_add_tasks(tasks)
262 elapsed = time.time() - start_time
263
264 tasks_per_second = count / elapsed
265
266 assert count == 10000
267 assert elapsed < 1.0 # Should be very fast
268 print(f"\n⚡ Bulk insert rate: {tasks_per_second:,.0f} tasks/second")
269
270 # Verify all were inserted
271 db_count = queue.conn.execute(
272 "SELECT COUNT(*) FROM task_queue"
273 ).fetchone()[0]
274
275 assert db_count == 10000
276
277 def test_get_next_task_priority(self, queue):
278 """Test that tasks are retrieved by priority"""
279 # Add tasks with different priorities
280 low_id = queue.add_task("low", {}, priority=1)
281 high_id = queue.add_task("high", {}, priority=10)
282 medium_id = queue.add_task("medium", {}, priority=5)
283
284 # Should get high priority first
285 task = queue.get_next_task("worker1")
286 assert task is not None
287 assert task['task_type'] == "high"
288
289 # Then medium
290 task = queue.get_next_task("worker1")
291 assert task['task_type'] == "medium"
292
293 # Then low
294 task = queue.get_next_task("worker1")
295 assert task['task_type'] == "low"
296
297 def test_task_locking(self, queue):
298 """Test that locked tasks aren't given to other workers"""
299 task_id = queue.add_task("test", {})
300
301 # Worker 1 gets the task
302 task1 = queue.get_next_task("worker1", lock_duration_seconds=60)
303 assert task1 is not None
304
305 # Worker 2 shouldn't get the same task
306 task2 = queue.get_next_task("worker2")
307 assert task2 is None # No tasks available
308
309 def test_complete_task(self, queue):
310 """Test completing a task"""
311 task_id = queue.add_task("test", {"data": "test"})
312
313 # Get and complete the task
314 task = queue.get_next_task("worker1")
315 queue.complete_task(task['id'], result={"status": "done"})
316
317 # Check status
318 result = queue.conn.execute(
319 "SELECT status, result FROM task_queue WHERE id = ?",
320 [task['id']]
321 ).fetchone()
322
323 assert result[0] == "completed"
324 assert json.loads(result[1])['status'] == "done"
325
326 def test_fail_task_with_retry(self, queue):
327 """Test failing a task with retry logic"""
328 task_id = queue.add_task("test", {}, max_attempts=3)
329
330 # Fail the task twice
331 for attempt in range(2):
332 task = queue.get_next_task("worker1")
333 assert task is not None
334 queue.fail_task(task['id'], f"Error {attempt}")
335
336 # Check it goes back to pending
337 status = queue.conn.execute(
338 "SELECT status FROM task_queue WHERE id = ?", [task_id]
339 ).fetchone()[0]
340 assert status == "pending"
341
342 # Third failure should mark as failed
343 task = queue.get_next_task("worker1")
344 queue.fail_task(task['id'], "Final error")
345
346 status = queue.conn.execute(
347 "SELECT status FROM task_queue WHERE id = ?", [task_id]
348 ).fetchone()[0]
349 assert status == "failed"
350
351 def test_metrics_calculation(self, queue):
352 """Test metrics calculation"""
353 # Add various tasks
354 for i in range(10):
355 queue.add_task("test", {"index": i})
356
357 # Process some
358 for i in range(5):
359 task = queue.get_next_task(f"worker_{i}")
360 if task:
361 queue.complete_task(task['id'])
362
363 metrics = queue.get_metrics()
364
365 assert metrics['pending_tasks'] == 5
366 assert metrics['completed_tasks'] == 5
367 assert metrics['theoretical_max_rows_sec'] == 402_000_000
368
369 @pytest.mark.asyncio
370 async def test_task_worker(self, queue):
371 """Test the TaskWorker class"""
372 processed = []
373
374 async def test_processor(task):
375 processed.append(task)
376 return {"processed": True}
377
378 # Create worker
379 worker = TaskWorker("test_worker", queue)
380 worker.process_task = test_processor
381
382 # Add tasks
383 for i in range(3):
384 queue.add_task("test", {"index": i})
385
386 # Run worker briefly
387 worker_task = asyncio.create_task(worker.start())
388 await asyncio.sleep(0.5)
389 worker.stop()
390 worker_task.cancel()
391 try:
392 await worker_task
393 except asyncio.CancelledError:
394 pass
395
396 # Check tasks were processed
397 assert len(processed) == 3
398
399
400class TestPerformanceBenchmarks:
401 """Performance benchmarks comparing with PostgreSQL baseline"""
402
403 @pytest.fixture
404 def queue(self):
405 return DBBasicTaskQueue(db_path=":memory:")
406
407 @pytest.fixture
408 async def channels(self):
409 return DBBasicChannels(db_path=":memory:")
410
411 def test_benchmark_insert_speed(self, queue):
412 """Benchmark: Insert speed vs PostgreSQL baseline"""
413 counts = [100, 1000, 10000]
414 results = []
415
416 for count in counts:
417 tasks = [
418 {'task_type': 'bench', 'payload': {'i': i}}
419 for i in range(count)
420 ]
421
422 # Use bulk insert
423 start = time.time()
424 queue.bulk_add_tasks(tasks)
425 elapsed = time.time() - start
426
427 rate = count / elapsed
428 results.append({
429 'count': count,
430 'time': elapsed,
431 'rate': rate
432 })
433
434 print(f"\n📊 {count:,} tasks: {elapsed:.3f}s = {rate:,.0f} tasks/sec")
435
436 # PostgreSQL baseline: ~5,000-10,000 inserts/sec
437 # Our target: 100,000+ inserts/sec
438 assert all(r['rate'] > 10000 for r in results), "Should beat PostgreSQL baseline"
439
440 @pytest.mark.asyncio
441 async def test_benchmark_concurrent_workers(self, queue):
442 """Benchmark: Concurrent worker processing"""
443
444 # Add many tasks
445 tasks = [
446 {'task_type': 'work', 'payload': {'id': i}}
447 for i in range(1000)
448 ]
449 queue.bulk_add_tasks(tasks)
450
451 processed_count = 0
452
453 async def worker_proc(worker_id):
454 nonlocal processed_count
455 while True:
456 task = queue.get_next_task(worker_id)
457 if not task:
458 break
459 queue.complete_task(task['id'])
460 processed_count += 1
461
462 # Run multiple workers
463 start = time.time()
464 workers = [worker_proc(f"worker_{i}") for i in range(10)]
465 await asyncio.gather(*workers)
466 elapsed = time.time() - start
467
468 rate = processed_count / elapsed
469 print(f"\n📊 Processed {processed_count} tasks with 10 workers")
470 print(f" Time: {elapsed:.3f}s")
471 print(f" Rate: {rate:,.0f} tasks/sec")
472
473 # Should process very quickly with concurrent workers
474 assert rate > 500, "Should process >500 tasks/sec with 10 workers"
475
476 @pytest.mark.asyncio
477 async def test_benchmark_pub_sub_latency(self, channels):
478 """Benchmark: Pub/Sub message latency"""
479 latencies = []
480
481 async def measure_latency():
482 received_time = None
483
484 async def callback(msg):
485 nonlocal received_time
486 received_time = time.time()
487
488 await channels.subscribe(Channel.ORDERS.value, callback)
489
490 # Measure latency
491 send_time = time.time()
492 await channels.publish(Channel.ORDERS.value, "test", {})
493
494 # Wait for message
495 await asyncio.sleep(0.01)
496
497 if received_time:
498 latency = (received_time - send_time) * 1000 # ms
499 latencies.append(latency)
500
501 # Run multiple measurements
502 for _ in range(100):
503 await measure_latency()
504
505 avg_latency = statistics.mean(latencies)
506 p99_latency = statistics.quantiles(latencies, n=100)[98]
507
508 print(f"\n📊 Pub/Sub Latency:")
509 print(f" Average: {avg_latency:.3f}ms")
510 print(f" P99: {p99_latency:.3f}ms")
511
512 # Should have very low latency
513 assert avg_latency < 10, "Average latency should be <10ms"
514 assert p99_latency < 50, "P99 latency should be <50ms"
515
516
517def test_integration_full_pipeline():
518 """Integration test: Complete pipeline from publish to process"""
519
520 async def run_pipeline():
521 channels = DBBasicChannels(db_path=":memory:")
522 queue = DBBasicTaskQueue(db_path=":memory:")
523
524 results = []
525
526 # Setup: Channel subscriber that adds to task queue
527 async def channel_to_queue(message):
528 if message['message_type'] == 'new_order':
529 queue.add_task(
530 'process_order',
531 message['payload'],
532 service_name='order_processor'
533 )
534
535 await channels.subscribe(Channel.ORDERS.value, channel_to_queue)
536
537 # Publish order messages
538 for i in range(10):
539 await channels.publish(
540 Channel.ORDERS.value,
541 'new_order',
542 {'order_id': f'ORD{i}', 'amount': i * 100}
543 )
544
545 await asyncio.sleep(0.1) # Let messages propagate
546
547 # Process tasks from queue
548 for _ in range(10):
549 task = queue.get_next_task('worker1')
550 if task:
551 queue.complete_task(task['id'], {'processed': True})
552 results.append(task)
553
554 # Verify pipeline worked
555 assert len(results) == 10
556 assert all(t['task_type'] == 'process_order' for t in results)
557
558 # Check metrics
559 metrics = queue.get_metrics()
560 assert metrics['completed_tasks'] == 10
561
562 print(f"\n✅ Full pipeline test: {len(results)} orders processed")
563
564 asyncio.run(run_pipeline())
565
566
567if __name__ == "__main__":
568 # Run tests with pytest
569 pytest.main([__file__, "-v", "--tb=short"])
570
571 # Or run specific benchmarks
572 print("\n" + "="*60)
573 print("Running Performance Benchmarks")
574 print("="*60)
575
576 queue = DBBasicTaskQueue(db_path=":memory:")
577
578 # Benchmark 1: Bulk insert speed
579 print("\n📊 Benchmark: Bulk Insert Speed")
580 tasks = [{'task_type': 'bench', 'payload': {'i': i}} for i in range(100000)]
581 start = time.time()
582 queue.bulk_add_tasks(tasks)
583 elapsed = time.time() - start
584 print(f" 100,000 tasks inserted in {elapsed:.3f}s")
585 print(f" Rate: {100000/elapsed:,.0f} tasks/second")
586 print(f" vs PostgreSQL: ~{(100000/elapsed)/10000:.1f}x faster")
587
588 print("\n🚀 DBBasic is ready to replace PostgreSQL queues!")