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

View file

@ -24,19 +24,7 @@ void main() {
debugPrint = originalDebugPrintCallback;
});
test('works with sync 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 {
test('works with non-failing actions', () async {
final int result = await debugInstrumentAction<int>('no-op', () async {
debugPrint('action()');
return 1;
@ -48,17 +36,7 @@ void main() {
);
});
test('throws if sync action throws', () {
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 {
test('returns failing future if action throws', () async {
try {
await debugInstrumentAction<void>('throws', () async {
await new Future<void>.delayed(Duration.zero);