OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 library stack_trace.test.chain.utils; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:stack_trace/stack_trace.dart'; |
| 10 import 'package:test/test.dart'; |
| 11 |
| 12 /// Runs [callback] in a microtask callback. |
| 13 void inMicrotask(callback()) => scheduleMicrotask(callback); |
| 14 |
| 15 /// Runs [callback] in a one-shot timer callback. |
| 16 void inOneShotTimer(callback()) => Timer.run(callback); |
| 17 |
| 18 /// Runs [callback] once in a periodic timer callback. |
| 19 void inPeriodicTimer(callback()) { |
| 20 var count = 0; |
| 21 new Timer.periodic(new Duration(milliseconds: 1), (timer) { |
| 22 count++; |
| 23 if (count != 5) return; |
| 24 timer.cancel(); |
| 25 callback(); |
| 26 }); |
| 27 } |
| 28 |
| 29 /// Runs [callback] within a long asynchronous Future chain. |
| 30 void inFutureChain(callback()) { |
| 31 new Future(() {}) |
| 32 .then((_) => new Future(() {})) |
| 33 .then((_) => new Future(() {})) |
| 34 .then((_) => new Future(() {})) |
| 35 .then((_) => new Future(() {})) |
| 36 .then((_) => callback()) |
| 37 .then((_) => new Future(() {})); |
| 38 } |
| 39 |
| 40 void inNewFuture(callback()) { |
| 41 new Future(callback); |
| 42 } |
| 43 |
| 44 void inSyncFuture(callback()) { |
| 45 new Future.sync(callback); |
| 46 } |
| 47 |
| 48 /// Returns a Future that completes to an error using a completer. |
| 49 /// |
| 50 /// If [trace] is passed, it's used as the stack trace for the error. |
| 51 Future completerErrorFuture([StackTrace trace]) { |
| 52 var completer = new Completer(); |
| 53 completer.completeError('error', trace); |
| 54 return completer.future; |
| 55 } |
| 56 |
| 57 /// Returns a Stream that emits an error using a controller. |
| 58 /// |
| 59 /// If [trace] is passed, it's used as the stack trace for the error. |
| 60 Stream controllerErrorStream([StackTrace trace]) { |
| 61 var controller = new StreamController(); |
| 62 controller.addError('error', trace); |
| 63 return controller.stream; |
| 64 } |
| 65 |
| 66 /// Runs [callback] within [asyncFn], then converts any errors raised into a |
| 67 /// [Chain] with [Chain.forTrace]. |
| 68 Future<Chain> chainForTrace(asyncFn(callback()), callback()) { |
| 69 var completer = new Completer(); |
| 70 asyncFn(() { |
| 71 // We use `new Future.value().then(...)` here as opposed to [new Future] or |
| 72 // [new Future.sync] because those methods don't pass the exception through |
| 73 // the zone specification before propagating it, so there's no chance to |
| 74 // attach a chain to its stack trace. See issue 15105. |
| 75 new Future.value().then((_) => callback()) |
| 76 .catchError(completer.completeError); |
| 77 }); |
| 78 return completer.future |
| 79 .catchError((_, stackTrace) => new Chain.forTrace(stackTrace)); |
| 80 } |
| 81 |
| 82 /// Runs [callback] in a [Chain.capture] zone and returns a Future that |
| 83 /// completes to the stack chain for an error thrown by [callback]. |
| 84 /// |
| 85 /// [callback] is expected to throw the string `"error"`. |
| 86 Future<Chain> captureFuture(callback()) { |
| 87 var completer = new Completer<Chain>(); |
| 88 Chain.capture(callback, onError: (error, chain) { |
| 89 expect(error, equals('error')); |
| 90 completer.complete(chain); |
| 91 }); |
| 92 return completer.future; |
| 93 } |
OLD | NEW |