Skip to content

Commit

Permalink
Enable turn on/off tracking for leak types for a test. (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
polina-c authored Jul 13, 2023
1 parent 5675231 commit 3fe4ca0
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 14 deletions.
2 changes: 1 addition & 1 deletion doc/TROUBLESHOOT.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Or, you can temporarily set global flag, to make all tests collecting debug info

```
setUpAll(() {
collectDebugInformationForLeaks = true;
LeakTrackerGlobalFlags.collectDebugInformationForLeaks = true;
});
```

Expand Down
5 changes: 5 additions & 0 deletions pkgs/leak_tracker/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 8.0.0

* Enable turn on/off tracking for leak types.
* Put all global flags into one class.

# 7.0.8

* Disconnect from service after obtaining retaining paths.
Expand Down
30 changes: 24 additions & 6 deletions pkgs/leak_tracker/lib/src/leak_tracking/leak_tracker_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,16 @@

import '../shared/shared_model.dart';

/// If true, the leak tracker will collect debug information for leaks.
bool collectDebugInformationForLeaks = false;
// ignore: avoid_classes_with_only_static_members, as it is ok for enum-like classes.
/// Global settings for leak tracker.
class LeakTrackerGlobalSettings {
/// If true, the leak tracker will collect debug information for leaks.
static bool collectDebugInformationForLeaks = false;

/// If true, a warning will be printed when leak tracking is
/// requested for a non-supported platform.
static bool warnForNonSupportedPlatforms = true;
}

/// Handler to collect leak summary.
typedef LeakSummaryCallback = void Function(LeakSummary);
Expand Down Expand Up @@ -115,6 +123,8 @@ class LeakTrackingConfiguration {
///
/// Customized configuration is needed only for test debugging,
/// not for regular test runs.
// TODO(polina-c): update helpers to respect allow lists defined in this class
// https://github.com/flutter/devtools/issues/5606
class LeakTrackingTestConfig {
/// Creates a new instance of [LeakTrackingTestConfig].
const LeakTrackingTestConfig({
Expand All @@ -123,6 +133,8 @@ class LeakTrackingTestConfig {
this.failTestOnLeaks = true,
this.notGCedAllowList = const <String, int>{},
this.notDisposedAllowList = const <String, int>{},
this.allowAllNotDisposed = false,
this.allowAllNotGCed = false,
});

/// Creates a new instance of [LeakTrackingTestConfig] for debugging leaks.
Expand All @@ -139,6 +151,8 @@ class LeakTrackingTestConfig {
this.failTestOnLeaks = true,
this.notGCedAllowList = const <String, int>{},
this.notDisposedAllowList = const <String, int>{},
this.allowAllNotDisposed = false,
this.allowAllNotGCed = false,
});

/// Creates a new instance of [LeakTrackingTestConfig] to collect retaining path.
Expand All @@ -153,12 +167,10 @@ class LeakTrackingTestConfig {
this.failTestOnLeaks = true,
this.notGCedAllowList = const <String, int>{},
this.notDisposedAllowList = const <String, int>{},
this.allowAllNotDisposed = false,
this.allowAllNotGCed = false,
});

/// If true, a warning will be printed when leak tracking is
/// requested for a non-supported platform.
static bool warnForNonSupportedPlatforms = true;

/// When to collect stack trace information.
///
/// Knowing call stack may help to troubleshoot memory leaks.
Expand Down Expand Up @@ -192,4 +204,10 @@ class LeakTrackingTestConfig {
///
/// If number of instances is [null], any number of instances is allowed.
final Map<String, int?> notDisposedAllowList;

/// If true, all notDisposed leaks will be allowed.
final bool allowAllNotDisposed;

/// If true, all notGCed leaks will be allowed.
final bool allowAllNotGCed;
}
2 changes: 1 addition & 1 deletion pkgs/leak_tracker/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: leak_tracker
version: 7.0.8
version: 8.0.0
description: A framework for memory leak tracking for Dart and Flutter applications.
repository: https://github.com/dart-lang/leak_tracker/tree/main/pkgs/leak_tracker

Expand Down
2 changes: 1 addition & 1 deletion pkgs/leak_tracker_flutter_test/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dev_dependencies:
sdk: flutter
integration_test:
sdk: flutter
leak_tracker: ^7.0.4
leak_tracker: # Version is not specified here, because it is overridden below.
leak_tracker_testing:
path: ../leak_tracker_testing
layerlens:
Expand Down
2 changes: 1 addition & 1 deletion pkgs/leak_tracker_flutter_test/test/end_to_end_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ void main() {
late Leaks leaks;

setUp(
() => collectDebugInformationForLeaks = true,
() => LeakTrackerGlobalSettings.collectDebugInformationForLeaks = true,
);

testWidgetsWithLeakTracking(
Expand Down
6 changes: 3 additions & 3 deletions pkgs/leak_tracker_flutter_test/test/test_infra/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void testWidgetsWithLeakTracking(
LeakTrackingTestConfig? leakTrackingTestConfig,
}) {
final config = leakTrackingTestConfig ??
(collectDebugInformationForLeaks
(LeakTrackerGlobalSettings.collectDebugInformationForLeaks
? LeakTrackingTestConfig.debug()
: const LeakTrackingTestConfig());

Expand Down Expand Up @@ -83,11 +83,11 @@ Future<void> withFlutterLeakTracking(
// Leak tracker does not work for web platform.
if (kIsWeb) {
final bool shouldPrintWarning = !_webWarningPrinted &&
LeakTrackingTestConfig.warnForNonSupportedPlatforms;
LeakTrackerGlobalSettings.warnForNonSupportedPlatforms;
if (shouldPrintWarning) {
_webWarningPrinted = true;
debugPrint(
'Leak tracking is not supported on web platform.\nTo turn off this message, set `LeakTrackingTestConfig.warnForNonSupportedPlatforms` to false.',
'Leak tracking is not supported on web platform.\nTo turn off this message, set `LeakTrackerGlobalFlags.warnForNonSupportedPlatforms` to false.',
);
}
await callback();
Expand Down
2 changes: 1 addition & 1 deletion pkgs/leak_tracker_testing/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ environment:
sdk: '>=3.0.0 <4.0.0'

dependencies:
leak_tracker: ^7.0.4
leak_tracker: # Version is not specified here, because it is overridden below.
test: ^1.16.0

dev_dependencies:
Expand Down

0 comments on commit 3fe4ca0

Please sign in to comment.