Unverified Commit d6d35ca7 authored by Todd Volkert's avatar Todd Volkert Committed by GitHub

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

parent c7ea3ca3
......@@ -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;
}());
}
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);
}
});
} else {
return action();
}
return returnResult;
}
/// Arguments to whitelist [Timeline] events in order to be shown in the
......
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment