mirror of
https://github.com/flutter/flutter
synced 2024-10-13 11:42:54 +00:00
parent
c7ea3ca377
commit
d6d35ca7cc
|
@ -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;
|
});
|
||||||
}());
|
} 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
|
/// Arguments to whitelist [Timeline] events in order to be shown in the
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in a new issue