11import 'dart:async' ;
22
33import 'package:cancellation_token/cancellation_token.dart' ;
4+ import 'package:fake_async/fake_async.dart' ;
45import 'package:test/test.dart' ;
56
67void main () {
@@ -53,13 +54,6 @@ void main() {
5354 );
5455 });
5556
56- test ('completes with normal value if cancellation token is null' , () {
57- expect (
58- CancellableFuture .from (() => Future .value ('Test value' ), null ),
59- completion (equals ('Test value' )),
60- );
61- });
62-
6357 test ('completes with normal exception if not cancelled' , () {
6458 final CancellationToken token = CancellationToken ();
6559 expect (
@@ -101,13 +95,6 @@ void main() {
10195 token.cancel ();
10296 });
10397
104- test ('completes with normal exception if cancellation token is null' , () {
105- expect (
106- CancellableFuture .from (() => Future .error (_TestException ()), null ),
107- throwsA (isA <_TestException >()),
108- );
109- });
110-
11198 test ('detaches from the cancellation token after completing with a value' ,
11299 () async {
113100 final CancellationToken token = CancellationToken ();
@@ -142,13 +129,6 @@ void main() {
142129 );
143130 });
144131
145- test ('completes with normal value if cancellation token is null' , () {
146- expect (
147- CancellableFuture .microtask (() => Future .value ('Test value' ), null ),
148- completion (equals ('Test value' )),
149- );
150- });
151-
152132 test ('completes with normal exception if not cancelled' , () {
153133 final CancellationToken token = CancellationToken ();
154134 expect (
@@ -191,13 +171,6 @@ void main() {
191171 token.cancel ();
192172 });
193173
194- test ('completes with normal exception if cancellation token is null' , () {
195- expect (
196- CancellableFuture .microtask (() => Future .error (_TestException ()), null ),
197- throwsA (isA <_TestException >()),
198- );
199- });
200-
201174 test ('detaches from the cancellation token after completing with a value' ,
202175 () async {
203176 final CancellationToken token = CancellationToken ();
@@ -233,13 +206,6 @@ void main() {
233206 );
234207 });
235208
236- test ('completes with normal value if cancellation token is null' , () {
237- expect (
238- CancellableFuture .sync (() => Future .value ('Test value' ), null ),
239- completion (equals ('Test value' )),
240- );
241- });
242-
243209 test ('completes with normal exception if not cancelled' , () {
244210 final CancellationToken token = CancellationToken ();
245211 expect (
@@ -281,13 +247,6 @@ void main() {
281247 token.cancel ();
282248 });
283249
284- test ('completes with normal exception if cancellation token is null' , () {
285- expect (
286- CancellableFuture .sync (() => Future .error (_TestException ()), null ),
287- throwsA (isA <_TestException >()),
288- );
289- });
290-
291250 test ('detaches from the cancellation token after completing with a value' ,
292251 () async {
293252 final CancellationToken token = CancellationToken ();
@@ -322,13 +281,6 @@ void main() {
322281 );
323282 });
324283
325- test ('completes with normal value if cancellation token is null' , () {
326- expect (
327- CancellableFuture .value ('Test value' , null ),
328- completion (equals ('Test value' )),
329- );
330- });
331-
332284 test ('completes with normal exception if not cancelled' , () {
333285 final CancellationToken token = CancellationToken ();
334286 expect (
@@ -357,13 +309,6 @@ void main() {
357309 token.cancel ();
358310 });
359311
360- test ('completes with normal exception if cancellation token is null' , () {
361- expect (
362- CancellableFuture .value (Future .error (_TestException ()), null ),
363- throwsA (isA <_TestException >()),
364- );
365- });
366-
367312 test ('detaches from the cancellation token after completing with a value' ,
368313 () async {
369314 final CancellationToken token = CancellationToken ();
@@ -386,6 +331,121 @@ void main() {
386331 });
387332 });
388333
334+ group ('CancellableFuture.delayed()' , () {
335+ test ('completes after the given duration' , () {
336+ fakeAsync ((async ) {
337+ final CancellationToken token = CancellationToken ();
338+
339+ expect (
340+ CancellableFuture .delayed (Duration (seconds: 5 ), token),
341+ completes,
342+ );
343+
344+ async .elapse (Duration (seconds: 5 ));
345+ });
346+ });
347+
348+ test ('completes with the result of the computation if not cancelled' , () {
349+ fakeAsync ((async ) {
350+ final CancellationToken token = CancellationToken ();
351+
352+ expect (
353+ CancellableFuture .delayed (
354+ Duration (seconds: 5 ),
355+ token,
356+ () => Future .value ('Test value' ),
357+ ),
358+ completion ('Test value' ),
359+ );
360+
361+ async .elapse (Duration (seconds: 5 ));
362+ });
363+ });
364+
365+ test ('completes with normal exception if not cancelled' , () {
366+ fakeAsync ((async ) {
367+ final CancellationToken token = CancellationToken ();
368+
369+ expect (
370+ CancellableFuture .delayed (
371+ Duration (seconds: 5 ),
372+ token,
373+ () => Future .error (_TestException ()),
374+ ),
375+ throwsA (isA <_TestException >()),
376+ );
377+
378+ async .elapse (Duration (seconds: 5 ));
379+ });
380+ });
381+
382+ test ('computation is not run if cancelled before attach' , () async {
383+ final CancellationToken token = CancellationToken ()..cancel ();
384+
385+ bool computationRun = false ;
386+ await ignoreCancellation (() {
387+ return CancellableFuture .delayed (
388+ Duration (seconds: 5 ),
389+ token,
390+ () => computationRun = true ,
391+ );
392+ });
393+
394+ expect (computationRun, isFalse);
395+ });
396+
397+ test ('computation is not run if cancelled during delay duration' , () {
398+ fakeAsync ((async ) {
399+ final CancellationToken token = CancellationToken ();
400+
401+ bool computationRun = false ;
402+ CancellableFuture .delayed (
403+ Duration (seconds: 5 ),
404+ token,
405+ () => computationRun = true ,
406+ ).ignore ();
407+
408+ async .elapse (Duration (seconds: 2 ));
409+ token.cancel ();
410+ async .flushTimers ();
411+
412+ expect (computationRun, isFalse);
413+ });
414+ });
415+
416+ test ('detaches from the cancellation token after completing with a value' ,
417+ () {
418+ fakeAsync ((async ) {
419+ final CancellationToken token = CancellationToken ();
420+
421+ CancellableFuture .delayed (
422+ Duration (seconds: 5 ),
423+ token,
424+ () => Future .value ('Test value' ),
425+ ).ignore ();
426+ async .elapse (Duration (seconds: 5 ));
427+
428+ expect (token.hasCancellables, isFalse);
429+ });
430+ });
431+
432+ test ('detaches from the cancellation token after completing with an error' ,
433+ () {
434+ fakeAsync ((async ) {
435+ final CancellationToken token = CancellationToken ();
436+
437+ CancellableFuture .delayed (
438+ Duration (seconds: 5 ),
439+ token,
440+ () => Future .error (_TestException ()),
441+ ).ignore ();
442+ async .elapse (Duration (seconds: 5 ));
443+
444+ expect (token.hasCancellables, isFalse);
445+ });
446+ });
447+ });
448+
389449 group ('asCancellable extension' , () {
390450 test ('completes with normal value if not cancelled' , () {
391451 final CancellationToken token = CancellationToken ();
0 commit comments