Address post-review comments from #17977 (#18000)

This commit is contained in:
Todd Volkert 2018-05-29 14:45:40 -07:00 committed by GitHub
parent c7ea3ca377
commit d6d35ca7cc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 53 deletions

View file

@ -42,38 +42,25 @@ bool debugInstrumentationEnabled = false;
/// non-debug builds, or when [debugInstrumentationEnabled] is false, this will /// non-debug builds, or when [debugInstrumentationEnabled] is false, this will
/// run [action] without any instrumentation. /// run [action] without any instrumentation.
/// ///
/// Returns the result of running [action], wrapped in a `Future` if the action /// Returns the result of running [action].
/// was synchronous. ///
Future<T> debugInstrumentAction<T>(String description, FutureOr<T> action()) { /// See also:
if (!debugInstrumentationEnabled) ///
return new Future<T>.value(action()); /// * [Timeline], which is used to record synchronous tracing events for
/// visualization in Chrome's tracing format. This method does not
Stopwatch stopwatch; /// implicitly add any timeline events.
assert(() { Future<T> debugInstrumentAction<T>(String description, Future<T> action()) {
stopwatch = new Stopwatch()..start(); bool instrument = false;
return true; assert(() { instrument = debugInstrumentationEnabled; return true; }());
} ()); if (instrument) {
void stopStopwatchAndPrintElapsed() { final Stopwatch stopwatch = new Stopwatch()..start();
assert(() { return action().whenComplete(() {
stopwatch.stop(); stopwatch.stop();
debugPrint('Action "$description" took ${stopwatch.elapsed}'); debugPrint('Action "$description" took ${stopwatch.elapsed}');
return true; });
}());
}
Future<T> returnResult;
FutureOr<T> actionResult;
try {
actionResult = action();
} finally {
if (actionResult is Future<T>) {
returnResult = actionResult.whenComplete(stopStopwatchAndPrintElapsed);
} else { } else {
stopStopwatchAndPrintElapsed(); return action();
returnResult = new Future<T>.value(actionResult);
} }
}
return returnResult;
} }
/// Arguments to whitelist [Timeline] events in order to be shown in the /// Arguments to whitelist [Timeline] events in order to be shown in the

View file

@ -24,19 +24,7 @@ void main() {
debugPrint = originalDebugPrintCallback; debugPrint = originalDebugPrintCallback;
}); });
test('works with sync actions', () async { test('works with non-failing actions', () async {
final int result = await debugInstrumentAction<int>('no-op', () {
debugPrint('action()');
return 1;
});
expect(result, 1);
expect(
printBuffer.toString(),
matches(new RegExp('^action\\(\\)\nAction "no-op" took .+\$', multiLine: true)),
);
});
test('works with async actions', () async {
final int result = await debugInstrumentAction<int>('no-op', () async { final int result = await debugInstrumentAction<int>('no-op', () async {
debugPrint('action()'); debugPrint('action()');
return 1; return 1;
@ -48,17 +36,7 @@ void main() {
); );
}); });
test('throws if sync action throws', () { test('returns failing future if action throws', () async {
try {
debugInstrumentAction<void>('throws', () => throw 'Error');
fail('Error expected but not thrown');
} on String catch (error) {
expect(error, 'Error');
expect(printBuffer.toString(), matches(r'^Action "throws" took .+'));
}
});
test('returns failing future if async action throws', () async {
try { try {
await debugInstrumentAction<void>('throws', () async { await debugInstrumentAction<void>('throws', () async {
await new Future<void>.delayed(Duration.zero); await new Future<void>.delayed(Duration.zero);