Instrument pending timers in tests (#37646)

Flutter widget tests assert if a test completes with timers still
pending.  However, it can be hard to diagnose where a pending timer
came from.  For example, a widget might consume a third-party library
that internally uses a timer.

I added a FakeAsync.pendingTimersDebugInfo getter to quiver
(https://github.com/google/quiver-dart/pull/500).  Make flutter_test
use it.

Additionally modify Flutter's debugPrintStack to take an optional
StackTrace argument instead of always printing StackTrace.current.

Fixes #4237.
This commit is contained in:
James D. Lin
2019-08-20 10:28:49 -07:00
committed by GitHub
parent 9da68fcdaf
commit b2f8d3a668
4 changed files with 61 additions and 14 deletions

View File

@@ -1048,14 +1048,29 @@ class AutomatedTestWidgetsFlutterBinding extends TestWidgetsFlutterBinding {
@override
void _verifyInvariants() {
super._verifyInvariants();
assert(
_currentFakeAsync.periodicTimerCount == 0,
'A periodic Timer is still running even after the widget tree was disposed.'
);
assert(
_currentFakeAsync.nonPeriodicTimerCount == 0,
'A Timer is still pending even after the widget tree was disposed.'
);
assert(() {
if ( _currentFakeAsync.periodicTimerCount == 0
&& _currentFakeAsync.nonPeriodicTimerCount == 0) {
return true;
}
debugPrint('Pending timers:');
for (String timerInfo in _currentFakeAsync.pendingTimersDebugInfo) {
final int firstLineEnd = timerInfo.indexOf('\n');
assert(firstLineEnd != -1);
// No need to include the newline.
final String firstLine = timerInfo.substring(0, firstLineEnd);
final String stackTrace = timerInfo.substring(firstLineEnd + 1);
debugPrint(firstLine);
debugPrintStack(stackTrace: StackTrace.fromString(stackTrace));
debugPrint('');
}
return false;
}(), 'A Timer is still pending even after the widget tree was disposed.');
assert(_currentFakeAsync.microtaskCount == 0); // Shouldn't be possible.
}