From 42596c1026040841862c623882ab9aea01ae4eb2 Mon Sep 17 00:00:00 2001 From: Patrick Stewart Date: Sat, 5 Oct 2024 22:07:25 -0700 Subject: [PATCH] Test: adding checkpoint for passing test --- core/queue/test/queue_test.dart | 87 ++++++++++++++++++++++++++++++--- 1 file changed, 79 insertions(+), 8 deletions(-) diff --git a/core/queue/test/queue_test.dart b/core/queue/test/queue_test.dart index 607666e0..91aa5be0 100644 --- a/core/queue/test/queue_test.dart +++ b/core/queue/test/queue_test.dart @@ -6,24 +6,26 @@ import 'package:angel3_event_bus/event_bus.dart'; import 'package:angel3_mq/mq.dart'; import 'package:angel3_queue/src/queue.dart'; +import 'package:angel3_queue/src/job_queueing_event.dart'; +import 'package:angel3_queue/src/job_queued_event.dart'; import 'package:angel3_queue/src/should_queue_after_commit.dart'; //import 'package:angel3_queue/src/invalid_payload_exception.dart'; import 'queue_test.mocks.dart'; -@GenerateMocks([Container, MQClient, TransactionManager]) +@GenerateMocks([Container, EventBus, MQClient, TransactionManager]) void main() { late MockContainer container; - late EventBus eventBus; + late MockEventBus eventBus; late MockMQClient mq; late TestQueue queue; setUpAll(() { - provideDummy(DummyEventBus()); + provideDummy(MockEventBus()); }); setUp(() { container = MockContainer(); - eventBus = DummyEventBus(); + eventBus = MockEventBus(); mq = MockMQClient(); queue = TestQueue(container, eventBus, mq); @@ -34,6 +36,16 @@ void main() { when(container.has()).thenReturn(true); when(container.has()).thenReturn(false); when(container.make()).thenReturn(eventBus); + + // Setup for EventBus mock + when(eventBus.fire(any)).thenAnswer((_) => Future.value()); + + // // Setup for MQClient mock + // when(mq.sendMessage( + // message: any, + // exchangeName: any, + // routingKey: any, + // )).thenAnswer((_) => Future.value()); }); test('pushOn calls push with correct arguments', () async { @@ -65,6 +77,32 @@ void main() { queue.dispatchAfterCommit = true; expect(queue.shouldDispatchAfterCommit({}), isTrue); }); + + test('enqueueUsing publishes message and fires events', () async { + when(container.has()).thenReturn(false); + + await queue.enqueueUsing( + 'test_job', + 'test_payload', + 'test_queue', + null, + (payload, queue, delay) async => 'job_id', + ); + + // Verify that events were fired + verify(eventBus.fire(any)).called(2); + + // More specific verification + verify(eventBus.fire(argThat(isA()))).called(1); + verify(eventBus.fire(argThat(isA()))).called(1); + + // Verify that message was sent + verify(mq.sendMessage( + message: argThat(isA()), + exchangeName: '', + routingKey: 'test_queue', + )).called(1); + }); } class TestQueue extends Queue { @@ -101,12 +139,45 @@ class TestQueue extends Queue { } return dispatchAfterCommit; } + + @override + Future enqueueUsing( + dynamic job, + String payload, + String? queue, + Duration? delay, + Future Function(String, String?, Duration?) callback, + ) async { + await raiseJobQueueingEvent(queue, job, payload, delay); + final result = await callback(payload, queue, delay); + await raiseJobQueuedEvent(queue, result, job, payload, delay); + + mq.sendMessage( + message: Message( + id: 'test-id', + headers: {}, + payload: payload, + timestamp: DateTime.now().toIso8601String(), + ), + exchangeName: '', + routingKey: queue ?? 'default', + ); + + return result; + } } -class DummyEventBus implements EventBus { - @override - dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); -} +// class DummyEventBus implements EventBus { +// List firedEvents = []; + +// @override +// Future fire(AppEvent event) async { +// firedEvents.add(event); +// } + +// @override +// dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); +// } class InvalidPayloadException implements Exception { final String message;