|
| 1 | +import 'dart:async'; |
| 2 | + |
| 3 | +import 'package:sentry/sentry.dart'; |
| 4 | +import 'package:sentry/src/transport/task_queue.dart'; |
| 5 | +import 'package:test/test.dart'; |
| 6 | + |
| 7 | +import '../mocks.dart'; |
| 8 | + |
| 9 | +void main() { |
| 10 | + group("called sync", () { |
| 11 | + late Fixture fixture; |
| 12 | + |
| 13 | + setUp(() { |
| 14 | + fixture = Fixture(); |
| 15 | + }); |
| 16 | + |
| 17 | + test("enqueue only executed `maxQueueSize` times when not awaiting", |
| 18 | + () async { |
| 19 | + final sut = fixture.getSut(maxQueueSize: 5); |
| 20 | + |
| 21 | + var completedTasks = 0; |
| 22 | + |
| 23 | + for (int i = 0; i < 10; i++) { |
| 24 | + unawaited(sut.enqueue(() async { |
| 25 | + print('Task $i'); |
| 26 | + await Future.delayed(Duration(milliseconds: 1)); |
| 27 | + completedTasks += 1; |
| 28 | + return 1 + 1; |
| 29 | + }, -1)); |
| 30 | + } |
| 31 | + |
| 32 | + // This will always await the other futures, even if they are running longer, as it was scheduled after them. |
| 33 | + print('Started waiting for first 5 tasks'); |
| 34 | + await Future.delayed(Duration(milliseconds: 1)); |
| 35 | + print('Stopped waiting for first 5 tasks'); |
| 36 | + |
| 37 | + expect(completedTasks, 5); |
| 38 | + }); |
| 39 | + |
| 40 | + test("enqueue picks up tasks again after await in-between", () async { |
| 41 | + final sut = fixture.getSut(maxQueueSize: 5); |
| 42 | + |
| 43 | + var completedTasks = 0; |
| 44 | + |
| 45 | + for (int i = 1; i <= 10; i++) { |
| 46 | + unawaited(sut.enqueue(() async { |
| 47 | + print('Started task $i'); |
| 48 | + await Future.delayed(Duration(milliseconds: 1)); |
| 49 | + print('Completed task $i'); |
| 50 | + completedTasks += 1; |
| 51 | + return 1 + 1; |
| 52 | + }, -1)); |
| 53 | + } |
| 54 | + |
| 55 | + print('Started waiting for first 5 tasks'); |
| 56 | + await Future.delayed(Duration(milliseconds: 1)); |
| 57 | + print('Stopped waiting for first 5 tasks'); |
| 58 | + |
| 59 | + for (int i = 6; i <= 15; i++) { |
| 60 | + unawaited(sut.enqueue(() async { |
| 61 | + print('Started task $i'); |
| 62 | + await Future.delayed(Duration(milliseconds: 1)); |
| 63 | + print('Completed task $i'); |
| 64 | + completedTasks += 1; |
| 65 | + return 1 + 1; |
| 66 | + }, -1)); |
| 67 | + } |
| 68 | + |
| 69 | + print('Started waiting for second 5 tasks'); |
| 70 | + await Future.delayed(Duration(milliseconds: 5)); |
| 71 | + print('Stopped waiting for second 5 tasks'); |
| 72 | + |
| 73 | + expect(completedTasks, 10); // 10 were dropped |
| 74 | + }); |
| 75 | + |
| 76 | + test("enqueue executes all tasks when awaiting", () async { |
| 77 | + final sut = fixture.getSut(maxQueueSize: 5); |
| 78 | + |
| 79 | + var completedTasks = 0; |
| 80 | + |
| 81 | + for (int i = 0; i < 10; i++) { |
| 82 | + await sut.enqueue(() async { |
| 83 | + print('Task $i'); |
| 84 | + await Future.delayed(Duration(milliseconds: 1)); |
| 85 | + completedTasks += 1; |
| 86 | + return 1 + 1; |
| 87 | + }, -1); |
| 88 | + } |
| 89 | + expect(completedTasks, 10); |
| 90 | + }); |
| 91 | + |
| 92 | + test("throwing tasks still execute as expected", () async { |
| 93 | + final sut = fixture.getSut(maxQueueSize: 5); |
| 94 | + |
| 95 | + var completedTasks = 0; |
| 96 | + |
| 97 | + for (int i = 0; i < 10; i++) { |
| 98 | + try { |
| 99 | + await sut.enqueue(() async { |
| 100 | + completedTasks += 1; |
| 101 | + throw Error(); |
| 102 | + }, -1); |
| 103 | + } catch (_) { |
| 104 | + // Ignore |
| 105 | + } |
| 106 | + } |
| 107 | + expect(completedTasks, 10); |
| 108 | + }); |
| 109 | + }); |
| 110 | +} |
| 111 | + |
| 112 | +class Fixture { |
| 113 | + final options = SentryOptions(dsn: fakeDsn); |
| 114 | + |
| 115 | + TaskQueue<int> getSut({required int maxQueueSize}) { |
| 116 | + return TaskQueue(maxQueueSize, options.logger); |
| 117 | + } |
| 118 | +} |
0 commit comments