forked from fluttercommunity/get_it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathasync_test.dart
942 lines (784 loc) · 27.5 KB
/
async_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
// ignore_for_file: unused_local_variable, unnecessary_type_check, unreachable_from_main, require_trailing_commas
import 'package:get_it/get_it.dart';
import 'package:test/test.dart';
int constructorCounter = 0;
int disposeCounter = 0;
int errorCounter = 0;
abstract class TestBaseClass {}
class TestClassParam {
final String? param1;
final int? param2;
TestClassParam({this.param1, this.param2});
}
class TestClass extends TestBaseClass {
GetIt? getIt;
bool initCompleted = false;
/// if we do the initialisation from inside the constructor the init function has to signal GetIt
/// that it has finished. For that we need to pass in the completer that we got from the factory call
/// that we set up in the registration.
TestClass({required bool internalCompletion, this.getIt}) {
constructorCounter++;
if (internalCompletion) {
assert(getIt != null);
initWithSignal();
}
}
/// This one signals after a delay
Future initWithSignal() {
return Future.delayed(const Duration(milliseconds: 10)).then((_) {
getIt!.signalReady(this);
initCompleted = true;
});
}
// We use this as dummy init that will return a future
Future<TestClass> init() async {
await Future.delayed(const Duration(milliseconds: 10));
initCompleted = true;
return this;
}
Future<TestClass> initWithExeption() async {
await Future.delayed(const Duration(milliseconds: 10));
throw StateError('Intentional');
}
void dispose() {
disposeCounter++;
}
}
class TestClassWillSignalReady extends TestClass implements WillSignalReady {
TestClassWillSignalReady({
required super.internalCompletion,
super.getIt,
});
}
class TestClassWillSignalReady2 extends TestClass implements WillSignalReady {
TestClassWillSignalReady2({
required super.internalCompletion,
super.getIt,
});
}
class TestClass2 extends TestClass {
TestClass2({
required super.internalCompletion,
super.getIt,
});
}
class TestClass3 extends TestClass {
TestClass3({
required super.internalCompletion,
super.getIt,
});
}
class TestClass4 extends TestClass {
TestClass4({
required super.internalCompletion,
super.getIt,
});
}
void main() {
setUp(() async {
// make sure the instance is cleared before each test
await GetIt.I.reset();
});
/// This is the most basic sync functionality. Not sure if we should keep it.
test('manual ready future test', () async {
final getIt = GetIt.instance;
getIt
.registerFactory<TestClass>(() => TestClass(internalCompletion: false));
getIt.registerFactory<TestClass2>(
() => TestClass2(internalCompletion: false),
);
getIt.registerFactory<TestClass3>(
() => TestClass3(internalCompletion: false),
);
getIt.registerFactory(
() => TestClass(internalCompletion: false),
instanceName: 'TestNamesInstance',
);
expect(getIt.allReady(), completes);
getIt.signalReady(null);
// make sure to allow future to complete
await Future.delayed(const Duration(seconds: 1));
});
test(
'signalReady will throw if any async Singletons have not signaled completion',
() async {
final getIt = GetIt.instance;
await getIt.reset();
getIt.registerSingletonAsync<TestClass>(
() => Future.delayed(const Duration(milliseconds: 1))
.then((_) => TestClass(internalCompletion: false, getIt: getIt)),
);
getIt.registerSingletonAsync<TestClass2>(
() => Future.delayed(const Duration(milliseconds: 2))
.then((_) => TestClass2(internalCompletion: false, getIt: getIt)),
);
getIt.registerSingletonAsync<TestClass3>(
() => Future.delayed(const Duration(milliseconds: 50))
.then((_) => TestClass3(internalCompletion: false, getIt: getIt)),
);
/// this here should signal call [signalReady()] but doesn't do it.
getIt.registerSingletonAsync(
() => Future.delayed(const Duration(milliseconds: 50))
.then((_) => TestClass(internalCompletion: false, getIt: getIt)),
instanceName: 'TestNamesInstance',
);
await Future.delayed(const Duration(milliseconds: 20));
expect(getIt.isReadySync<TestClass>(), true);
expect(getIt.isReadySync<TestClass3>(), false);
expect(
getIt.isReadySync<TestClass>(instanceName: 'TestNamesInstance'),
false,
);
expect(getIt.allReadySync(), false);
/// We call [signalReady] before the last has completed
expect(
() => getIt.signalReady(null),
throwsA(const TypeMatcher<StateError>()),
);
});
test(
'signalReady will throw if any Singletons that has signalsReady==true '
'have not signaled completion', () async {
final getIt = GetIt.instance;
getIt.registerSingletonAsync<TestClass>(
() => Future.delayed(const Duration(milliseconds: 1))
.then((_) => TestClass(internalCompletion: true, getIt: getIt)),
signalsReady: true,
);
getIt.registerSingletonAsync<TestClass2>(
() => Future.delayed(const Duration(milliseconds: 2))
.then((_) => TestClass2(internalCompletion: false, getIt: getIt)),
);
getIt.registerSingleton<TestClass3>(
TestClass3(internalCompletion: true, getIt: getIt),
signalsReady: true,
);
/// this here should signal call [signalReady()] but doesn't do it.
getIt.registerSingleton(
TestClass(internalCompletion: false, getIt: getIt),
instanceName: 'TestNamesInstance',
signalsReady: true,
);
await Future.delayed(const Duration(milliseconds: 20));
/// We call [signalReady] before the last has completed
expect(
() => getIt.signalReady(null),
throwsA(const TypeMatcher<StateError>()),
);
});
test('all ready ignoring pending async Singletons', () async {
final getIt = GetIt.instance;
getIt.registerSingletonAsync<TestClass>(
() => Future.delayed(const Duration(milliseconds: 100))
.then((_) => TestClass(internalCompletion: false, getIt: getIt)),
);
getIt.registerSingletonAsync<TestClass2>(
() => Future.delayed(const Duration(milliseconds: 100))
.then((_) => TestClass2(internalCompletion: false, getIt: getIt)),
);
getIt.registerSingleton<TestClass3>(
TestClass3(internalCompletion: true, getIt: getIt),
signalsReady: true,
);
await Future.delayed(const Duration(milliseconds: 15));
expect(
getIt.allReady(
timeout: const Duration(milliseconds: 2),
ignorePendingAsyncCreation: true,
),
completes,
);
await Future.delayed(const Duration(milliseconds: 15));
});
test(
'Normal Singletons, ready with internal signalling setting signalsReady parameter',
() async {
final getIt = GetIt.instance;
errorCounter = 0;
getIt.registerSingleton<TestClass>(
TestClass(internalCompletion: true, getIt: getIt),
signalsReady: true,
);
getIt.registerSingleton<TestClass2>(
TestClass2(internalCompletion: true, getIt: getIt),
signalsReady: true,
);
getIt.registerSingleton(
TestClass2(internalCompletion: true, getIt: getIt),
instanceName: 'Second Instance',
signalsReady: true,
);
expect(getIt.isReadySync<TestClass>(), false);
expect(getIt.isReadySync<TestClass2>(), false);
expect(
getIt.isReadySync<TestClass2>(instanceName: 'Second Instance'),
false,
);
final timer = Stopwatch()..start();
await getIt.allReady(timeout: const Duration(milliseconds: 20));
final t = getIt<TestClass>();
expect(timer.elapsedMilliseconds, greaterThan(5));
});
test(
'Normal Singletons,ready with internal signalling relying on implementing WillSignalReady interface',
() async {
final getIt = GetIt.instance;
errorCounter = 0;
getIt.registerSingleton<TestClassWillSignalReady>(
TestClassWillSignalReady(internalCompletion: true, getIt: getIt),
);
getIt.registerSingleton<TestClassWillSignalReady2>(
TestClassWillSignalReady2(internalCompletion: true, getIt: getIt),
);
getIt.registerSingleton(
TestClass2(internalCompletion: true, getIt: getIt),
instanceName: 'Second Instance',
signalsReady: true,
);
expect(getIt.isReadySync<TestClassWillSignalReady>(), false);
expect(getIt.isReadySync<TestClassWillSignalReady2>(), false);
expect(
getIt.isReadySync<TestClass2>(instanceName: 'Second Instance'),
false,
);
final timer = Stopwatch()..start();
await getIt.allReady(timeout: const Duration(milliseconds: 20));
expect(timer.elapsedMilliseconds, greaterThan(5));
});
test('ready external signalling', () async {
final getIt = GetIt.instance;
getIt.registerSingleton<TestClass>(
TestClass(internalCompletion: false, getIt: getIt),
signalsReady: true,
);
getIt.registerSingleton<TestClass2>(
TestClass2(internalCompletion: false, getIt: getIt),
signalsReady: true,
);
getIt.registerSingleton(
TestClass2(internalCompletion: false, getIt: getIt),
instanceName: 'Second Instance',
signalsReady: true,
);
expect(getIt.allReadySync(), false);
// these are async calls fire and forget
getIt<TestClass>().initWithSignal();
getIt<TestClass2>().initWithSignal();
final TestClass2 instance =
getIt<TestClass2>(instanceName: 'Second Instance');
instance.initWithSignal();
expect(getIt.allReady(), completes);
});
test('ready automatic signalling for async Singletons', () async {
final getIt = GetIt.instance;
getIt.reset();
getIt.registerSingletonAsync<TestClass>(
() async => TestClass(internalCompletion: false).init(),
);
getIt.registerSingletonAsync<TestClass2>(
() async {
final instance = TestClass2(internalCompletion: false);
await instance.init();
return instance;
},
);
getIt.registerSingletonAsync(
() async => TestClass2(internalCompletion: false)..init(),
instanceName: 'Second Instance',
);
expect(getIt.allReady(), completes);
});
test('isReady propagates Error', () async {
final getIt = GetIt.instance;
getIt.reset();
getIt.registerSingletonAsync<TestClass>(
() async => TestClass(internalCompletion: false).initWithExeption(),
);
expect(getIt.isReady<TestClass>(), throwsStateError);
});
test('allReady propagades Exceptions that occur in the factory functions',
() async {
final getIt = GetIt.instance;
getIt.reset();
getIt.registerSingletonAsync<TestClass>(
() async => TestClass(internalCompletion: false).init(),
);
getIt.registerSingletonAsync<TestClass2>(
() async {
final instance = TestClass2(internalCompletion: false);
await Future.delayed(const Duration(milliseconds: 500));
throw StateError('Intentional');
},
);
getIt.registerSingletonAsync(
() async => TestClass2(internalCompletion: false)..init(),
instanceName: 'Second Instance',
);
expect(getIt.allReady(), throwsA(isA<StateError>()));
});
test('ready manual synchronisation of sequence', () async {
final getIt = GetIt.instance;
getIt.reset();
errorCounter = 0;
var flag1 = false;
var flag2 = false;
getIt.registerSingletonAsync<TestClass>(
() async {
final instance = TestClass(internalCompletion: false);
while (!flag1) {
await Future.delayed(const Duration(milliseconds: 100));
}
return instance;
},
);
getIt.registerSingletonAsync<TestClass2>(
() async {
await getIt.isReady<TestClass>();
final instance = TestClass2(internalCompletion: false);
while (!flag2) {
await Future.delayed(const Duration(milliseconds: 100));
}
return instance;
},
);
getIt.registerSingletonAsync<TestClass3>(
() async {
await getIt.isReady<TestClass2>();
final instance = TestClass3(internalCompletion: false);
await instance.init();
return instance;
},
);
expect(getIt.isReadySync<TestClass>(), false);
expect(getIt.isReadySync<TestClass2>(), false);
expect(getIt.isReadySync<TestClass3>(), false);
flag1 = true;
expect(getIt.isReady<TestClass>(), completes);
expect(getIt.isReadySync<TestClass2>(), false);
expect(getIt.isReadySync<TestClass3>(), false);
expect(getIt.allReadySync(), false);
flag2 = true;
expect(getIt.isReady<TestClass>(), completes);
expect(getIt.isReady<TestClass2>(), completes);
expect(getIt.isReady<TestClass3>(), completes);
expect(getIt.allReady(), completes);
});
test('ready automatic synchronisation of sequence', () async {
final getIt = GetIt.instance;
errorCounter = 0;
var flag1 = false;
getIt.registerSingletonAsync<TestClass>(
() async {
final instance = TestClass(internalCompletion: false);
while (!flag1) {
await Future.delayed(const Duration(milliseconds: 100));
}
return instance;
},
);
getIt.registerSingletonWithDependencies<TestClass2>(
() {
return TestClass2(internalCompletion: false);
},
dependsOn: [TestClass],
);
getIt.registerSingletonAsync<TestClass3>(
() async {
final instance = TestClass3(internalCompletion: false);
await instance.init();
return instance;
},
dependsOn: [TestClass, TestClass2],
);
expect(getIt.isReadySync<TestClass>(), false);
expect(getIt.isReadySync<TestClass2>(), false);
expect(getIt.isReadySync<TestClass3>(), false);
flag1 = true;
// give the factory function a chance to run
await Future.delayed(const Duration(microseconds: 1));
expect(getIt.isReady<TestClass>(), completes);
expect(getIt.isReady<TestClass2>(), completes);
expect(getIt.isReadySync<TestClass3>(), false);
expect(getIt.allReadySync(), false);
expect(
getIt.isReady<TestClass>(timeout: const Duration(seconds: 5)),
completes,
);
expect(
getIt.isReady<TestClass2>(timeout: const Duration(seconds: 10)),
completes,
);
expect(
getIt.isReady<TestClass3>(timeout: const Duration(seconds: 15)),
completes,
);
expect(getIt.allReady(timeout: const Duration(seconds: 20)), completes);
});
test('ready automatic synchronisation of sequence', () async {
final getIt = GetIt.instance;
errorCounter = 0;
var flag1 = false;
getIt.registerSingletonAsync<TestClass>(
() async {
final instance = TestClass(internalCompletion: false);
while (!flag1) {
await Future.delayed(const Duration(milliseconds: 100));
}
return instance;
},
);
getIt.registerSingletonWithDependencies<TestClass2>(
() {
return TestClass2(internalCompletion: false);
},
dependsOn: [TestClass],
);
getIt.registerSingletonAsync<TestClass3>(
() async {
final instance = TestClass3(internalCompletion: false);
await instance.init();
return instance;
},
dependsOn: [TestClass, TestClass2],
);
expect(getIt.isReadySync<TestClass>(), false);
expect(getIt.isReadySync<TestClass2>(), false);
expect(getIt.isReadySync<TestClass3>(), false);
flag1 = true;
// give the factory function a chance to run
await Future.delayed(const Duration(microseconds: 1));
expect(getIt.isReady<TestClass>(), completes);
expect(getIt.isReady<TestClass2>(), completes);
expect(getIt.isReadySync<TestClass3>(), false);
expect(getIt.allReadySync(), false);
expect(
getIt.isReady<TestClass>(timeout: const Duration(seconds: 5)),
completes,
);
expect(
getIt.isReady<TestClass2>(timeout: const Duration(seconds: 10)),
completes,
);
expect(
getIt.isReady<TestClass3>(timeout: const Duration(seconds: 15)),
completes,
);
expect(getIt.allReady(timeout: const Duration(seconds: 20)), completes);
});
test('ready automatic synchronisation with signalReady', () async {
final getIt = GetIt.instance;
errorCounter = 0;
getIt.registerSingleton<TestClass>(
TestClass(internalCompletion: false, getIt: getIt),
signalsReady: true,
);
getIt.registerSingletonWithDependencies<TestClass3>(
() => TestClass3(internalCompletion: true, getIt: getIt),
dependsOn: [
TestClass,
],
signalsReady: true,
);
expect(getIt.isReadySync<TestClass>(), false);
expect(getIt.isReadySync<TestClass3>(), false);
await getIt<TestClass>().initWithSignal();
await getIt.allReady();
final o = getIt<TestClass3>();
expect(getIt.isReady<TestClass>(), completes);
expect(getIt.isReadySync<TestClass3>(), true);
expect(getIt.allReadySync(), true);
});
test('allReady will throw after timeout', () async {
final getIt = GetIt.instance;
getIt.registerSingletonAsync<TestClass>(
() async => TestClass(internalCompletion: false, getIt: getIt),
signalsReady: true,
);
getIt.registerSingletonAsync<TestClass>(
() async => TestClass(
internalCompletion: true,
getIt: getIt,
),
instanceName: "Second instance",
signalsReady: true,
);
getIt.registerSingletonAsync<TestClass2>(
() async => TestClass2(internalCompletion: false)..init(),
dependsOn: [TestClass],
);
// this here should signal internally but doesn't do it.
getIt.registerSingletonAsync<TestClass3>(
() async => TestClass3(internalCompletion: false),
signalsReady: true,
);
getIt.registerSingletonAsync<TestClass4>(
() async => TestClass4(internalCompletion: false),
);
Future.delayed(
const Duration(milliseconds: 1),
() async => getIt.isReady<TestClass3>(callee: 'asyncTest'),
);
try {
await getIt.allReady(timeout: const Duration(seconds: 1));
} catch (ex) {
expect(ex, const TypeMatcher<WaitingTimeOutException>());
final timeOut = ex as WaitingTimeOutException;
expect(timeOut.notReadyYet.contains('null : TestClass'), true);
expect(timeOut.notReadyYet.contains('null : TestClass2'), true);
expect(timeOut.notReadyYet.contains('null : TestClass3'), true);
expect(timeOut.areReady.contains('null : TestClass4'), true);
expect(timeOut.areReady.contains('Second instance : TestClass'), true);
expect(
timeOut.areWaitedBy['null : TestClass']!.contains('TestClass2'),
true,
);
expect(
timeOut.areWaitedBy['null : TestClass3']!.contains('String'),
true,
);
}
});
test('asyncFactory called with getAsync', () async {
final getIt = GetIt.instance;
getIt.reset();
getIt.registerFactoryAsync<TestClass>(
() => Future.value(TestClass(internalCompletion: false)),
);
final instance = await getIt.getAsync<TestClass>();
expect(instance, const TypeMatcher<TestClass>());
});
test('register factory with one Param', () async {
final getIt = GetIt.instance;
constructorCounter = 0;
getIt.registerFactoryParamAsync<TestClassParam, String, void>((s, _) async {
await Future.delayed(const Duration(milliseconds: 1));
return TestClassParam(param1: s);
});
//final instance1 = getIt.get<TestBaseClass>();
final instance1 = await getIt.getAsync<TestClassParam>(param1: 'abc');
final instance2 = await getIt.getAsync<TestClassParam>(param1: '123');
expect(instance1 is TestClassParam, true);
expect(instance1.param1, 'abc');
expect(instance2 is TestClassParam, true);
expect(instance2.param1, '123');
});
test('register factory with one nullable Param', () async {
final getIt = GetIt.instance;
constructorCounter = 0;
getIt
.registerFactoryParamAsync<TestClassParam, String?, void>((s, _) async {
await Future.delayed(const Duration(milliseconds: 1));
return TestClassParam(param1: s);
});
final instance1 = await getIt.getAsync<TestClassParam>(param1: 'abc');
final instance2 = await getIt.getAsync<TestClassParam>();
expect(instance1 is TestClassParam, true);
expect(instance1.param1, 'abc');
expect(instance2 is TestClassParam, true);
expect(instance2.param1, null);
});
test('register factory with two Params', () async {
final getIt = GetIt.instance;
constructorCounter = 0;
getIt.registerFactoryParamAsync<TestClassParam, String, int>((s, i) async {
await Future.delayed(const Duration(milliseconds: 1));
return TestClassParam(param1: s, param2: i);
});
//final instance1 = getIt.get<TestBaseClass>();
final instance1 =
await getIt.getAsync<TestClassParam>(param1: 'abc', param2: 3);
final instance2 =
await getIt.getAsync<TestClassParam>(param1: '123', param2: 5);
expect(instance1 is TestClassParam, true);
expect(instance1.param1, 'abc');
expect(instance1.param2, 3);
expect(instance2 is TestClassParam, true);
expect(instance2.param1, '123');
expect(instance2.param2, 5);
});
test('register factory with two nullable Params', () async {
final getIt = GetIt.instance;
constructorCounter = 0;
getIt
.registerFactoryParamAsync<TestClassParam, String?, int?>((s, i) async {
await Future.delayed(const Duration(milliseconds: 1));
return TestClassParam(param1: s, param2: i);
});
final instance1 =
await getIt.getAsync<TestClassParam>(param1: 'abc', param2: 3);
final instance2 = await getIt.getAsync<TestClassParam>();
expect(instance1 is TestClassParam, true);
expect(instance1.param1, 'abc');
expect(instance1.param2, 3);
expect(instance2 is TestClassParam, true);
expect(instance2.param1, null);
expect(instance2.param2, null);
});
test('register factory with Params with wrong type', () {
final getIt = GetIt.instance;
getIt.reset();
constructorCounter = 0;
getIt.registerFactoryParamAsync<TestClassParam, String, int>(
(s, i) async => TestClassParam(param1: s, param2: i),
);
expect(
() => getIt.getAsync<TestClassParam>(param1: 'abc', param2: '3'),
throwsA(const TypeMatcher<TypeError>()),
);
});
test('register factory with Params with non-nullable type but not pass it',
() {
final getIt = GetIt.instance;
getIt.reset();
constructorCounter = 0;
getIt.registerFactoryParamAsync<TestClassParam, String, void>(
(s, i) async => TestClassParam(param1: s),
);
expect(
() => getIt.getAsync<TestClassParam>(),
throwsA(const TypeMatcher<TypeError>()),
);
});
test('asyncFactory called with get instead of getAsync', () async {
final getIt = GetIt.instance;
getIt.reset();
getIt.registerFactoryAsync<TestClass>(
() => Future.value(TestClass(internalCompletion: false)),
);
expect(
() => getIt.get<TestClass>(),
throwsA(const TypeMatcher<AssertionError>()),
);
});
test('asyncLazySingleton called with get before it was ready', () async {
final getIt = GetIt.instance;
getIt.reset();
getIt.registerLazySingletonAsync<TestClass>(
() => Future.value(TestClass(internalCompletion: false)),
);
await Future.delayed(const Duration(microseconds: 1));
expect(
() => getIt.get<TestClass>(),
throwsA(const TypeMatcher<StateError>()),
);
});
test('asyncLazySingleton called with getAsync', () async {
final getIt = GetIt.instance;
getIt.reset();
getIt.registerLazySingletonAsync<TestClass>(
() => Future.value(TestClass(internalCompletion: false)..init()),
);
final instance = await getIt.getAsync<TestClass>();
expect(instance, const TypeMatcher<TestClass>());
});
test('asyncLazySingleton called with get after wait for ready', () async {
final getIt = GetIt.instance;
getIt.registerLazySingletonAsync<TestClass>(
() => Future.value(TestClass(internalCompletion: false)),
);
await getIt.getAsync<TestClass>();
await getIt.isReady<TestClass>(timeout: const Duration(milliseconds: 20));
final instance2 = getIt.get<TestClass>();
expect(instance2, const TypeMatcher<TestClass>());
});
test('isReady called on asyncLazySingleton ', () async {
final getIt = GetIt.instance;
getIt.reset();
getIt.registerLazySingletonAsync<TestClass>(
() => Future.value(TestClass(internalCompletion: false)),
);
await getIt.isReady<TestClass>(timeout: const Duration(milliseconds: 20));
final instance = getIt.get<TestClass>();
expect(instance, const TypeMatcher<TestClass>());
});
group("dependency", () {
test('Register singleton with dependency and instanceName', () async {
final getIt = GetIt.instance;
await getIt.reset();
getIt.registerSingletonAsync<TestClass>(
() async => TestClass(internalCompletion: false),
);
getIt.registerSingletonAsync<TestClass2>(
() async => TestClass2(internalCompletion: false),
instanceName: "test2InstanceName",
dependsOn: [TestClass],
);
await getIt.allReady();
expect(
getIt.get<TestClass2>(instanceName: "test2InstanceName"),
isA<TestClass2>(),
);
});
test('Register two dependent singleton with instanceNames', () async {
final getIt = GetIt.instance;
await getIt.reset();
getIt.registerSingletonAsync<TestClass>(
() async => TestClass(internalCompletion: false),
instanceName: "test1InstanceName",
);
getIt.registerSingletonAsync<TestClass2>(
() async => TestClass2(internalCompletion: false),
instanceName: "test2InstanceName",
dependsOn: [
InitDependency(TestClass, instanceName: "test1InstanceName")
],
);
await getIt.allReady();
expect(
getIt.get<TestClass2>(instanceName: "test2InstanceName"),
isA<TestClass2>(),
);
});
});
test('Code for ReadMe', () async {
final sl = GetIt.instance;
sl.registerSingletonAsync<Service1>(() async {
final instance = Service1Implementation();
await instance.init();
return instance;
});
});
group("registerSingletonAsync, calling signalReady", () {
test('attempt to get instance with getIt.get', () async {
final getIt = GetIt.instance;
getIt.registerSingletonAsync<TestClass>(
() => Future.delayed(const Duration(milliseconds: 1))
.then((_) => TestClass(internalCompletion: true, getIt: getIt)),
signalsReady: true,
);
await getIt.allReady();
await Future.delayed(const Duration(seconds: 1));
final instance = getIt<TestClass>();
expect(instance.initCompleted, isTrue);
});
test('attempt to get instance with getAsync', () async {
final getIt = GetIt.instance;
getIt.registerSingletonAsync<TestClass>(
() => Future.delayed(const Duration(milliseconds: 1))
.then((_) => TestClass(internalCompletion: true, getIt: getIt)),
signalsReady: true,
);
await getIt.allReady();
final instance = await getIt.getAsync<TestClass>();
expect(instance.initCompleted, isTrue);
});
});
}
abstract class Service1 {}
// abstract class Service2 {}
class Service1Implementation implements Service1 {
Future init() {
// dummy async call
return Future.delayed(const Duration(microseconds: 1));
}
}
// class Service2Implementation implements Service2 {
// Service2Implementation() {
// _init(); // we call _init here without awaiting it.
// }
// Future _init() async {
// // dummy async call
// await Future.delayed(const Duration(microseconds: 1));
// // From here on we are ready
// }
// }