diff --git a/dev/bots/analyze.dart b/dev/bots/analyze.dart index 39f739277f..976a93d800 100644 --- a/dev/bots/analyze.dart +++ b/dev/bots/analyze.dart @@ -124,6 +124,9 @@ Future run(List arguments) async { printProgress('Goldens...'); await verifyGoldenTags(flutterPackages); + printProgress('Prevent flakes from Stopwatches...'); + await verifyNoStopwatches(flutterPackages); + printProgress('Skip test comments...'); await verifySkipTestComments(flutterRoot); @@ -584,6 +587,48 @@ Future verifyGoldenTags(String workingDirectory, { int minimumMatches = 20 } } +/// Use of Stopwatches can introduce test flakes as the logical time of a +/// stopwatch can fall out of sync with the mocked time of FakeAsync in testing. +/// The Clock object provides a safe stopwatch instead, which is paired with +/// FakeAsync as part of the test binding. +final RegExp _findStopwatchPattern = RegExp(r'Stopwatch\(\)'); +const String _ignoreStopwatch = '// flutter_ignore: stopwatch (see analyze.dart)'; +const String _ignoreStopwatchForFile = '// flutter_ignore_for_file: stopwatch (see analyze.dart)'; + +Future verifyNoStopwatches(String workingDirectory, { int minimumMatches = 2000 }) async { + final List errors = []; + await for (final File file in _allFiles(workingDirectory, 'dart', minimumMatches: minimumMatches)) { + if (file.path.contains('flutter_tool')) { + // Skip flutter_tool package. + continue; + } + int lineNumber = 1; + final List lines = file.readAsLinesSync(); + for (final String line in lines) { + // If the file is being ignored, skip parsing the rest of the lines. + if (line.contains(_ignoreStopwatchForFile)) { + break; + } + + if (line.contains(_findStopwatchPattern) + && !line.contains(_leadingComment) + && !line.contains(_ignoreStopwatch)) { + // Stopwatch found + errors.add('\t${file.path}:$lineNumber'); + } + lineNumber++; + } + } + if (errors.isNotEmpty) { + foundError([ + 'Stopwatch use was found in the following files:', + ...errors, + '${bold}Stopwatches introduce flakes by falling out of sync with the FakeAsync used in testing.$reset', + 'A Stopwatch that stays in sync with FakeAsync is available through the Gesture or Test bindings, through samplingClock.' + ]); + } +} + final RegExp _findDeprecationPattern = RegExp(r'@[Dd]eprecated'); final RegExp _deprecationStartPattern = RegExp(r'^(? *)@Deprecated\($'); // flutter_ignore: deprecation_syntax (see analyze.dart) final RegExp _deprecationMessagePattern = RegExp(r"^ *'(?.+) '$"); diff --git a/dev/bots/test/analyze-test-input/root/packages/foo/stopwatch_doc.dart b/dev/bots/test/analyze-test-input/root/packages/foo/stopwatch_doc.dart new file mode 100644 index 0000000000..238699f92e --- /dev/null +++ b/dev/bots/test/analyze-test-input/root/packages/foo/stopwatch_doc.dart @@ -0,0 +1,20 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +/// Sample Code +/// +/// No analysis failures should be found. +/// +/// {@tool snippet} +/// Sample invocations of [Stopwatch]. +/// +/// ```dart +/// Stopwatch(); +/// ``` +/// {@end-tool} +String? foo; +// Other comments +// Stopwatch(); + +String literal = 'Stopwatch()'; // flutter_ignore: stopwatch (see analyze.dart) diff --git a/dev/bots/test/analyze-test-input/root/packages/foo/stopwatch_fail.dart b/dev/bots/test/analyze-test-input/root/packages/foo/stopwatch_fail.dart new file mode 100644 index 0000000000..0bad2372b3 --- /dev/null +++ b/dev/bots/test/analyze-test-input/root/packages/foo/stopwatch_fail.dart @@ -0,0 +1,14 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This should fail analysis. + +void main() { + Stopwatch(); + + // Identify more than one in a file. + Stopwatch myStopwatch; + myStopwatch = Stopwatch(); + myStopwatch.reset(); +} diff --git a/dev/bots/test/analyze-test-input/root/packages/foo/stopwatch_ignore.dart b/dev/bots/test/analyze-test-input/root/packages/foo/stopwatch_ignore.dart new file mode 100644 index 0000000000..de30ea4cd6 --- /dev/null +++ b/dev/bots/test/analyze-test-input/root/packages/foo/stopwatch_ignore.dart @@ -0,0 +1,10 @@ +// Copyright 2014 The Flutter Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// This would fail analysis, but it is ignored +// flutter_ignore_for_file: stopwatch (see analyze.dart) + +void main() { + Stopwatch(); +} diff --git a/dev/bots/test/analyze_test.dart b/dev/bots/test/analyze_test.dart index f406f2ae44..cf0c4fa114 100644 --- a/dev/bots/test/analyze_test.dart +++ b/dev/bots/test/analyze_test.dart @@ -92,6 +92,24 @@ void main() { expect(result[result.length - 1], ''); // trailing newline }); + test('analyze.dart - verifyNoStopwatches', () async { + final List result = (await capture(() => verifyNoStopwatches(testRootPath, minimumMatches: 6), shouldHaveErrors: true)).split('\n'); + final List lines = [ + '║ \ttest/analyze-test-input/root/packages/foo/stopwatch_fail.dart:8', + '║ \ttest/analyze-test-input/root/packages/foo/stopwatch_fail.dart:12', + ] + .map((String line) => line.replaceAll('/', Platform.isWindows ? r'\' : '/')) + .toList(); + expect(result.length, 6 + lines.length, reason: 'output had unexpected number of lines:\n${result.join('\n')}'); + expect(result[0], '╔═╡ERROR╞═══════════════════════════════════════════════════════════════════════'); + expect(result[1], '║ Stopwatch use was found in the following files:'); + expect(result.getRange(2, result.length - 4).toSet(), lines.toSet()); + expect(result[result.length - 4], '║ Stopwatches introduce flakes by falling out of sync with the FakeAsync used in testing.'); + expect(result[result.length - 3], '║ A Stopwatch that stays in sync with FakeAsync is available through the Gesture or Test bindings, through samplingClock.'); + expect(result[result.length - 2], '╚═══════════════════════════════════════════════════════════════════════════════'); + expect(result[result.length - 1], ''); // trailing newline + }); + test('analyze.dart - verifyNoMissingLicense', () async { final String result = await capture(() => verifyNoMissingLicense(testRootPath, checkMinimums: false), shouldHaveErrors: true); final String file = 'test/analyze-test-input/root/packages/foo/foo.dart' diff --git a/packages/flutter/lib/src/foundation/debug.dart b/packages/flutter/lib/src/foundation/debug.dart index 5cf29fce66..ab9ff30c38 100644 --- a/packages/flutter/lib/src/foundation/debug.dart +++ b/packages/flutter/lib/src/foundation/debug.dart @@ -75,7 +75,8 @@ Future debugInstrumentAction(String description, Future Function() acti return true; }()); if (instrument) { - final Stopwatch stopwatch = Stopwatch()..start(); + final Stopwatch stopwatch = Stopwatch()..start(); // flutter_ignore: stopwatch (see analyze.dart) + // Ignore context: The framework does not use this function internally so it will not cause flakes. try { return await action(); } finally { diff --git a/packages/flutter/lib/src/foundation/print.dart b/packages/flutter/lib/src/foundation/print.dart index 123e591649..25b6c8f291 100644 --- a/packages/flutter/lib/src/foundation/print.dart +++ b/packages/flutter/lib/src/foundation/print.dart @@ -66,7 +66,8 @@ int _debugPrintedCharacters = 0; const int _kDebugPrintCapacity = 12 * 1024; const Duration _kDebugPrintPauseTime = Duration(seconds: 1); final Queue _debugPrintBuffer = Queue(); -final Stopwatch _debugPrintStopwatch = Stopwatch(); +final Stopwatch _debugPrintStopwatch = Stopwatch(); // flutter_ignore: stopwatch (see analyze.dart) +// Ignore context: This is not used in tests, only for throttled logging. Completer? _debugPrintCompleter; bool _debugPrintScheduled = false; void _debugPrintTask() { diff --git a/packages/flutter/lib/src/gestures/binding.dart b/packages/flutter/lib/src/gestures/binding.dart index 0dec4b054b..89d6190ef1 100644 --- a/packages/flutter/lib/src/gestures/binding.dart +++ b/packages/flutter/lib/src/gestures/binding.dart @@ -36,7 +36,13 @@ class SamplingClock { DateTime now() => DateTime.now(); /// Returns a new stopwatch that uses the current time as reported by `this`. - Stopwatch stopwatch() => Stopwatch(); + /// + /// See also: + /// + /// * [GestureBinding.debugSamplingClock], which is used in tests and + /// debug builds to observe [FakeAsync]. + Stopwatch stopwatch() => Stopwatch(); // flutter_ignore: stopwatch (see analyze.dart) + // Ignore context: This is replaced by debugSampling clock in the test binding. } // Class that handles resampling of touch events for multiple pointer @@ -59,7 +65,8 @@ class _Resampler { Duration _frameTime = Duration.zero; // Time since `_frameTime` was updated. - Stopwatch _frameTimeAge = Stopwatch(); + Stopwatch _frameTimeAge = Stopwatch(); // flutter_ignore: stopwatch (see analyze.dart) + // Ignore context: This is tested safely outside of FakeAsync. // Last sample time and time stamp of last event. // diff --git a/packages/flutter/test/foundation/timeline_test.dart b/packages/flutter/test/foundation/timeline_test.dart index 80c95d1a8b..ee8b6c6d16 100644 --- a/packages/flutter/test/foundation/timeline_test.dart +++ b/packages/flutter/test/foundation/timeline_test.dart @@ -72,7 +72,8 @@ void main() { // a bit inconsistent with Stopwatch. final int start = FlutterTimeline.now - 1; FlutterTimeline.timeSync('TEST', () { - final Stopwatch watch = Stopwatch()..start(); + final Stopwatch watch = Stopwatch()..start(); // flutter_ignore: stopwatch (see analyze.dart) + // Ignore context: Used safely for benchmarking. while (watch.elapsedMilliseconds < 5) {} watch.stop(); }); diff --git a/packages/flutter_test/lib/src/test_compat.dart b/packages/flutter_test/lib/src/test_compat.dart index 433f586cb3..201f616b19 100644 --- a/packages/flutter_test/lib/src/test_compat.dart +++ b/packages/flutter_test/lib/src/test_compat.dart @@ -297,7 +297,8 @@ class _Reporter { final bool _printPath; /// A stopwatch that tracks the duration of the full run. - final Stopwatch _stopwatch = Stopwatch(); + final Stopwatch _stopwatch = Stopwatch(); // flutter_ignore: stopwatch (see analyze.dart) + // Ignore context: Used for logging of actual test runs, outside of FakeAsync. /// The size of `_engine.passed` last time a progress notification was /// printed.