@@ -19,11 +19,13 @@ class TestClassParam {
19
19
class TestClass extends TestBaseClass {
20
20
GetIt ? getIt;
21
21
bool initCompleted = false ;
22
+ int initMsDelay;
22
23
23
24
/// if we do the initialisation from inside the constructor the init function has to signal GetIt
24
25
/// that it has finished. For that we need to pass in the completer that we got from the factory call
25
26
/// that we set up in the registration.
26
- TestClass ({required bool internalCompletion, this .getIt}) {
27
+ TestClass (
28
+ {required bool internalCompletion, this .initMsDelay = 10 , this .getIt}) {
27
29
constructorCounter++ ;
28
30
if (internalCompletion) {
29
31
assert (getIt != null );
@@ -33,21 +35,21 @@ class TestClass extends TestBaseClass {
33
35
34
36
/// This one signals after a delay
35
37
Future initWithSignal () {
36
- return Future .delayed (const Duration (milliseconds: 10 )).then ((_) {
38
+ return Future .delayed (Duration (milliseconds: initMsDelay )).then ((_) {
37
39
getIt! .signalReady (this );
38
40
initCompleted = true ;
39
41
});
40
42
}
41
43
42
44
// We use this as dummy init that will return a future
43
45
Future <TestClass > init () async {
44
- await Future .delayed (const Duration (milliseconds: 10 ));
46
+ await Future .delayed (Duration (milliseconds: initMsDelay ));
45
47
initCompleted = true ;
46
48
return this ;
47
49
}
48
50
49
51
Future <TestClass > initWithExeption () async {
50
- await Future .delayed (const Duration (milliseconds: 10 ));
52
+ await Future .delayed (Duration (milliseconds: initMsDelay ));
51
53
throw StateError ('Intentional' );
52
54
}
53
55
@@ -73,6 +75,7 @@ class TestClassWillSignalReady2 extends TestClass implements WillSignalReady {
73
75
class TestClass2 extends TestClass {
74
76
TestClass2 ({
75
77
required super .internalCompletion,
78
+ super .initMsDelay = 10 ,
76
79
super .getIt,
77
80
});
78
81
}
@@ -346,29 +349,45 @@ void main() {
346
349
});
347
350
348
351
test ('ready automatic signalling for async Singletons' , () async {
349
- final getIt = GetIt .instance;
350
- getIt.reset ();
352
+ try {
353
+ final getIt = GetIt .instance;
354
+ await getIt.reset ();
351
355
352
- getIt.registerSingletonAsync <TestClass >(
353
- () async => TestClass (internalCompletion: false ).init (),
354
- );
355
- getIt.registerSingletonAsync <TestClass2 >(
356
- () async {
357
- final instance = TestClass2 (internalCompletion: false );
358
- await instance.init ();
359
- return instance;
360
- },
361
- );
362
- getIt.registerSingletonAsync (
363
- () async => TestClass2 (internalCompletion: false )..init (),
364
- instanceName: 'Second Instance' ,
365
- );
366
- expect (getIt.allReady (), completes);
356
+ getIt.registerSingletonAsync <TestClass >(
357
+ () async => TestClass (internalCompletion: false ).init (),
358
+ );
359
+ getIt.registerSingletonAsync <TestClass2 >(
360
+ () async {
361
+ final instance =
362
+ TestClass2 (internalCompletion: false , initMsDelay: 50 );
363
+ await instance.init ();
364
+ return instance;
365
+ },
366
+ );
367
+ getIt.registerSingletonAsync <TestClass >(
368
+ () async => TestClass2 (internalCompletion: false ).init (),
369
+ instanceName: 'Second Instance' ,
370
+ );
371
+
372
+ expect (getIt.isReadySync <TestClass >(), false );
373
+ expect (getIt.isReadySync <TestClass2 >(), false );
374
+ expect (
375
+ getIt.isReadySync <TestClass >(instanceName: 'Second Instance' ), false );
376
+
377
+ await getIt.allReady ();
378
+
379
+ expect (getIt.isReadySync <TestClass >(), true );
380
+ expect (getIt.isReadySync <TestClass2 >(), true );
381
+ expect (
382
+ getIt.isReadySync <TestClass >(instanceName: 'Second Instance' ), true );
383
+ } on Exception catch (e) {
384
+ print (e);
385
+ }
367
386
});
368
387
369
388
test ('isReady propagates Error' , () async {
370
389
final getIt = GetIt .instance;
371
- getIt.reset ();
390
+ await getIt.reset ();
372
391
373
392
getIt.registerSingletonAsync <TestClass >(
374
393
() async => TestClass (internalCompletion: false ).initWithExeption (),
@@ -379,7 +398,7 @@ void main() {
379
398
test ('allReady propagades Exceptions that occur in the factory functions' ,
380
399
() async {
381
400
final getIt = GetIt .instance;
382
- getIt.reset ();
401
+ await getIt.reset ();
383
402
384
403
getIt.registerSingletonAsync <TestClass >(
385
404
() async => TestClass (internalCompletion: false ).init (),
@@ -400,7 +419,7 @@ void main() {
400
419
});
401
420
test ('ready manual synchronisation of sequence' , () async {
402
421
final getIt = GetIt .instance;
403
- getIt.reset ();
422
+ await getIt.reset ();
404
423
errorCounter = 0 ;
405
424
var flag1 = false ;
406
425
var flag2 = false ;
@@ -659,7 +678,7 @@ void main() {
659
678
660
679
test ('asyncFactory called with getAsync' , () async {
661
680
final getIt = GetIt .instance;
662
- getIt.reset ();
681
+ await getIt.reset ();
663
682
664
683
getIt.registerFactoryAsync <TestClass >(
665
684
() => Future .value (TestClass (internalCompletion: false )),
@@ -754,9 +773,9 @@ void main() {
754
773
expect (instance2.param2, null );
755
774
});
756
775
757
- test ('register factory with Params with wrong type' , () {
776
+ test ('register factory with Params with wrong type' , () async {
758
777
final getIt = GetIt .instance;
759
- getIt.reset ();
778
+ await getIt.reset ();
760
779
761
780
constructorCounter = 0 ;
762
781
getIt.registerFactoryParamAsync <TestClassParam , String , int >(
@@ -770,9 +789,9 @@ void main() {
770
789
});
771
790
772
791
test ('register factory with Params with non-nullable type but not pass it' ,
773
- () {
792
+ () async {
774
793
final getIt = GetIt .instance;
775
- getIt.reset ();
794
+ await getIt.reset ();
776
795
777
796
constructorCounter = 0 ;
778
797
getIt.registerFactoryParamAsync <TestClassParam , String , void >(
@@ -787,7 +806,7 @@ void main() {
787
806
788
807
test ('asyncFactory called with get instead of getAsync' , () async {
789
808
final getIt = GetIt .instance;
790
- getIt.reset ();
809
+ await getIt.reset ();
791
810
792
811
getIt.registerFactoryAsync <TestClass >(
793
812
() => Future .value (TestClass (internalCompletion: false )),
@@ -801,7 +820,7 @@ void main() {
801
820
802
821
test ('asyncLazySingleton called with get before it was ready' , () async {
803
822
final getIt = GetIt .instance;
804
- getIt.reset ();
823
+ await getIt.reset ();
805
824
806
825
getIt.registerLazySingletonAsync <TestClass >(
807
826
() => Future .value (TestClass (internalCompletion: false )),
@@ -816,7 +835,7 @@ void main() {
816
835
817
836
test ('asyncLazySingleton called with getAsync' , () async {
818
837
final getIt = GetIt .instance;
819
- getIt.reset ();
838
+ await getIt.reset ();
820
839
821
840
getIt.registerLazySingletonAsync <TestClass >(
822
841
() => Future .value (TestClass (internalCompletion: false )..init ()),
@@ -843,7 +862,7 @@ void main() {
843
862
844
863
test ('isReady called on asyncLazySingleton ' , () async {
845
864
final getIt = GetIt .instance;
846
- getIt.reset ();
865
+ await getIt.reset ();
847
866
848
867
getIt.registerLazySingletonAsync <TestClass >(
849
868
() => Future .value (TestClass (internalCompletion: false )),
0 commit comments