Skip to content

Commit 67fbb5c

Browse files
Fix the last test and add some comments
1 parent 7b090f6 commit 67fbb5c

File tree

1 file changed

+41
-26
lines changed

1 file changed

+41
-26
lines changed

test/ioc_container_test.dart

Lines changed: 41 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ void main() {
526526
);
527527
});
528528

529-
test('Test Time Out', () async {
529+
test('Test Async Singleton Time Out', () async {
530530
var loop = true;
531531

532532
final builder = IocContainerBuilder()
@@ -554,38 +554,53 @@ void main() {
554554
expect(a.name, 'a');
555555
});
556556

557-
// test('Test initSafe - Deadlock times out', () async {
558-
// var throwException = true;
557+
test('Test Async Singleton Recovers From Error v1', () async {
558+
var throwException = true;
559559

560-
// final builder = IocContainerBuilder()
561-
// ..addSingletonAsync(
562-
// (c) async => throwException ? throw Exception() : A('a'),
563-
// );
560+
final builder = IocContainerBuilder()
561+
..addSingletonAsync(
562+
(c) async => throwException ? throw Exception() : A('a'),
563+
);
564564

565-
// final container = builder.toContainer();
565+
final container = builder.toContainer();
566566

567-
// //This causes a deadlock because the future is never completed, so cleanup
568-
// //never occurs
569-
// expect(() async => container.getAsync<A>(), throwsException);
567+
//This would cause a deadlock if didn't wait for the future to clean up
568+
//afterwards
569+
final future = container.getAsync<A>();
570+
expect(() async => future, throwsException);
570571

571-
// //We should not have stored the bad future
572-
// expect(container.singletons.containsKey(Future<A>), false);
572+
try {
573+
//This allows cleanup, but this also causes the same effect as
574+
//expectLater or awaiting the future in the try block
575+
//All the versions are different flavors of the same time, and
576+
//perhaps there is no way to use expect with a future without
577+
//causing a deadlock
578+
await future;
579+
}
580+
// ignore: avoid_catches_without_on_clauses, empty_catches
581+
catch (ex) {}
573582

574-
// throwException = false;
583+
//We should not have stored the bad future
584+
expect(container.singletons.containsKey(Future<A>), false);
575585

576-
// //We can recover
577-
// final a = await container.getAsync<A>();
586+
//The lock was removed
587+
expect(container.locks.containsKey(A), false);
578588

579-
// expect(
580-
// identical(
581-
// a,
582-
// await container.getAsync<A>(),
583-
// ),
584-
// true,
585-
// );
586-
// });
589+
throwException = false;
590+
591+
//We can recover
592+
final a = await container.getAsync<A>();
593+
594+
expect(
595+
identical(
596+
a,
597+
await container.getAsync<A>(),
598+
),
599+
true,
600+
);
601+
});
587602

588-
test('Test initSafe - Recover From Error expectLater', () async {
603+
test('Test Async Singleton Recovers From Error v2 - expectLater', () async {
589604
var throwException = true;
590605

591606
final builder = IocContainerBuilder()
@@ -619,7 +634,7 @@ void main() {
619634
);
620635
});
621636

622-
test('Test initSafe - Recover From Error Full Try/Catch', () async {
637+
test('Test Async Singleton Recovers From Error v3 - Full Try/Catch', () async {
623638
var throwException = true;
624639

625640
final builder = IocContainerBuilder()

0 commit comments

Comments
 (0)