2024-10-06 02:35:50 +00:00
|
|
|
import 'package:test/test.dart';
|
|
|
|
import 'package:mockito/annotations.dart';
|
|
|
|
import 'package:mockito/mockito.dart';
|
|
|
|
import 'package:angel3_container/angel3_container.dart';
|
|
|
|
import 'package:angel3_event_bus/event_bus.dart';
|
|
|
|
import 'package:angel3_mq/mq.dart';
|
|
|
|
import 'package:angel3_queue/src/queue.dart';
|
2024-10-06 04:06:00 +00:00
|
|
|
|
|
|
|
import 'package:angel3_queue/src/should_queue_after_commit.dart';
|
2024-10-06 02:43:53 +00:00
|
|
|
//import 'package:angel3_queue/src/invalid_payload_exception.dart';
|
2024-10-06 02:35:50 +00:00
|
|
|
import 'queue_test.mocks.dart';
|
|
|
|
|
|
|
|
@GenerateMocks([Container, MQClient, TransactionManager])
|
|
|
|
void main() {
|
|
|
|
late MockContainer container;
|
|
|
|
late EventBus eventBus;
|
|
|
|
late MockMQClient mq;
|
|
|
|
late TestQueue queue;
|
|
|
|
|
|
|
|
setUpAll(() {
|
|
|
|
provideDummy<EventBus>(DummyEventBus());
|
|
|
|
});
|
|
|
|
|
|
|
|
setUp(() {
|
|
|
|
container = MockContainer();
|
|
|
|
eventBus = DummyEventBus();
|
|
|
|
mq = MockMQClient();
|
|
|
|
queue = TestQueue(container, eventBus, mq);
|
|
|
|
|
2024-10-06 04:06:00 +00:00
|
|
|
// Inject the other mocks into the queue
|
|
|
|
// queue.container = container;
|
|
|
|
// queue.mq = mq;
|
|
|
|
|
2024-10-06 02:35:50 +00:00
|
|
|
when(container.has<EventBus>()).thenReturn(true);
|
|
|
|
when(container.has<TransactionManager>()).thenReturn(false);
|
|
|
|
when(container.make<EventBus>()).thenReturn(eventBus);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('pushOn calls push with correct arguments', () async {
|
|
|
|
final result = await queue.pushOn('test_queue', 'test_job', 'test_data');
|
|
|
|
expect(result, equals('pushed'));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('laterOn calls later with correct arguments', () async {
|
|
|
|
final result = await queue.laterOn(
|
|
|
|
'test_queue', Duration(minutes: 5), 'test_job', 'test_data');
|
|
|
|
expect(result, equals('pushed later'));
|
|
|
|
});
|
2024-10-06 02:43:53 +00:00
|
|
|
|
|
|
|
test('bulk pushes multiple jobs', () async {
|
|
|
|
await queue.bulk(['job1', 'job2', 'job3'], 'test_data', 'test_queue');
|
|
|
|
expect(queue.pushedJobs.length, equals(3));
|
|
|
|
expect(queue.pushedJobs, containsAll(['job1', 'job2', 'job3']));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('createPayload throws InvalidPayloadException for invalid job', () {
|
|
|
|
expect(() => queue.createPayload({}, 'test_queue'),
|
|
|
|
throwsA(isA<InvalidPayloadException>()));
|
|
|
|
});
|
2024-10-06 04:06:00 +00:00
|
|
|
test('shouldDispatchAfterCommit returns correct value', () {
|
|
|
|
expect(
|
|
|
|
queue.shouldDispatchAfterCommit(MockShouldQueueAfterCommit()), isTrue);
|
|
|
|
expect(queue.shouldDispatchAfterCommit({}), isFalse);
|
|
|
|
|
|
|
|
queue.dispatchAfterCommit = true;
|
|
|
|
expect(queue.shouldDispatchAfterCommit({}), isTrue);
|
|
|
|
});
|
2024-10-06 02:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class TestQueue extends Queue {
|
2024-10-06 02:43:53 +00:00
|
|
|
List<dynamic> pushedJobs = [];
|
|
|
|
|
2024-10-06 02:35:50 +00:00
|
|
|
TestQueue(Container container, EventBus eventBus, MQClient mq)
|
|
|
|
: super(container, eventBus, mq);
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<dynamic> push(dynamic job, [dynamic data = '', String? queue]) async {
|
2024-10-06 02:43:53 +00:00
|
|
|
pushedJobs.add(job);
|
2024-10-06 02:35:50 +00:00
|
|
|
return 'pushed';
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Future<dynamic> later(Duration delay, dynamic job,
|
|
|
|
[dynamic data = '', String? queue]) async {
|
|
|
|
return 'pushed later';
|
|
|
|
}
|
2024-10-06 02:43:53 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
Future<String> createPayload(dynamic job, String queue,
|
|
|
|
[dynamic data = '']) async {
|
|
|
|
if (job is Map && job.isEmpty) {
|
|
|
|
throw InvalidPayloadException('Invalid job: empty map');
|
|
|
|
}
|
|
|
|
return 'valid payload';
|
|
|
|
}
|
2024-10-06 04:06:00 +00:00
|
|
|
|
|
|
|
@override
|
|
|
|
bool shouldDispatchAfterCommit(dynamic job) {
|
|
|
|
if (job is ShouldQueueAfterCommit) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return dispatchAfterCommit;
|
|
|
|
}
|
2024-10-06 02:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class DummyEventBus implements EventBus {
|
|
|
|
@override
|
|
|
|
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
|
|
|
|
}
|
2024-10-06 02:43:53 +00:00
|
|
|
|
|
|
|
class InvalidPayloadException implements Exception {
|
|
|
|
final String message;
|
|
|
|
InvalidPayloadException(this.message);
|
|
|
|
@override
|
|
|
|
String toString() => 'InvalidPayloadException: $message';
|
|
|
|
}
|
2024-10-06 04:06:00 +00:00
|
|
|
|
|
|
|
class MockShouldQueueAfterCommit implements ShouldQueueAfterCommit {}
|