Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create adjustable test settings. #152

Merged
merged 64 commits into from
Oct 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
64 commits
Select commit Hold shift + click to select a range
ffacf0b
-
polina-c Oct 7, 2023
ce6a55a
-
polina-c Oct 10, 2023
39ba16b
Update model_test.dart
polina-c Oct 10, 2023
37ddc09
Merge branch 'main' of github.com:dart-lang/leak_tracker into test-se…
polina-c Oct 10, 2023
5c6561f
-
polina-c Oct 10, 2023
db12a55
Update model_test.dart
polina-c Oct 10, 2023
258995d
-
polina-c Oct 10, 2023
699b339
-
polina-c Oct 10, 2023
e89ebe4
-
polina-c Oct 10, 2023
764e922
-
polina-c Oct 11, 2023
ddca95c
-
polina-c Oct 11, 2023
0efe5ca
-
polina-c Oct 11, 2023
a0fa898
-
polina-c Oct 12, 2023
b363876
-
polina-c Oct 12, 2023
6f3a173
Update leak_tracking_for_tests_test.dart
polina-c Oct 12, 2023
16d8e3b
-
polina-c Oct 12, 2023
5329e65
-
polina-c Oct 13, 2023
4fc7a6d
-
polina-c Oct 13, 2023
f1624dd
-
polina-c Oct 13, 2023
027f501
Update model.dart
polina-c Oct 13, 2023
28355eb
-
polina-c Oct 13, 2023
73983bb
-
polina-c Oct 13, 2023
22635a5
-
polina-c Oct 13, 2023
765d717
-
polina-c Oct 13, 2023
159609a
Update leak_tracking_for_tests_test.dart
polina-c Oct 13, 2023
cd3d03e
-
polina-c Oct 13, 2023
ab00a68
-
polina-c Oct 13, 2023
fd8007b
Update leak_tracking_for_tests.dart
polina-c Oct 13, 2023
872d4be
Update leak_tracking_for_tests.dart
polina-c Oct 13, 2023
e86a80a
Update leak_tracking_for_tests.dart
polina-c Oct 13, 2023
df2d1e2
Update leak_tracking_for_tests.dart
polina-c Oct 13, 2023
cfe5bc9
-
polina-c Oct 14, 2023
955dd61
Update leak_tracking_for_tests.dart
polina-c Oct 14, 2023
4344ca6
-
polina-c Oct 14, 2023
bd57aa5
Update leak_tracking_for_tests_test.dart
polina-c Oct 15, 2023
1907462
Update leak_tracking_for_tests.dart
polina-c Oct 16, 2023
72a881a
Update model.dart
polina-c Oct 16, 2023
5a05216
-
polina-c Oct 16, 2023
844c5cf
Update leak_tracking_for_tests.dart
polina-c Oct 17, 2023
85553f6
Update leak_tracking_for_tests.dart
polina-c Oct 17, 2023
c92c50e
Update leak_tracking_for_tests.dart
polina-c Oct 17, 2023
820a42d
-
polina-c Oct 20, 2023
f585c4d
-
polina-c Oct 20, 2023
3edd53a
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
cbeaf3b
-
polina-c Oct 20, 2023
4c01b6f
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
9f8d2b0
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
b966164
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
b789134
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
496aab2
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
0e1cfcb
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
1a70e7a
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
816996a
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
901b1cc
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
a8d2710
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
ee39afb
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
81f8f0e
Update leak_tracking_for_tests_test.dart
polina-c Oct 20, 2023
46956ad
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
f499c8b
-
polina-c Oct 20, 2023
cfaf8de
Update leak_tracking_for_tests.dart
polina-c Oct 20, 2023
d8abda1
-
polina-c Oct 20, 2023
93a953b
Update leak_testing.dart
polina-c Oct 21, 2023
450d5b8
Update leak_testing.dart
polina-c Oct 21, 2023
09bb054
-
polina-c Oct 21, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
# 9.0.9

* Define `IgnoredLeaks`.
* Add item `none` to BaseliningMode.

# 9.0.8

* Enable declaring all not disposed objects as leaks.
Expand Down
1 change: 1 addition & 0 deletions pkgs/leak_tracker/lib/src/leak_tracking/_baseliner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Baseliner {
throw UnimplementedError(
'Regression testing for memory consumption is not implemented yet. '
'Upvote the following issue if interested: https://github.com/dart-lang/leak_tracker/issues/120');
case BaseliningMode.none:
}
}

Expand Down
118 changes: 117 additions & 1 deletion pkgs/leak_tracker/lib/src/leak_tracking/_primitives/model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ typedef LeaksCallback = void Function(Leaks leaks);
/// Useable to temporary disable features in case of
/// noisinness or performance regression
/// in applications or tests.
/// TODO(polina-c): delete after migration to [IgnoredLeaks].
/// https://github.com/flutter/devtools/issues/3951
class Switches {
const Switches({
this.disableNotGCed = false,
Expand All @@ -38,6 +40,113 @@ class Switches {
bool get isObjectTrackingDisabled => disableNotDisposed && disableNotGCed;
}

/// Set of classes to ignore during leak tracking.
class IgnoredLeaksSet {
/// Creates instance of [IgnoredLeaksSet].
///
/// Use this constructor to provide both [byClass] and [ignoreAll]
/// in case when you want to preserve list of classes, while temporarily turning off
/// the entire leak tracking, so that when you turn it back on for a subset of tests
/// with `copyWith(ignoreAll: false)`, the list of classes is set to needed value.
const IgnoredLeaksSet({this.byClass = const {}, this.ignoreAll = false});

const IgnoredLeaksSet.ignore() : this(ignoreAll: true, byClass: const {});

const IgnoredLeaksSet.byClass(this.byClass) : ignoreAll = false;

/// Classes to ignore during leak tracking.
///
/// Maps name of the class, as returned by `object.runtimeType.toString()`,
/// to the number of instances of the class that are allowed to leak.
///
/// If number of instances is [null], all leaks are ignored.
final Map<String, int?> byClass;

/// If true, all leaks are ignored, otherwise [byClass] defines what is ignored.
final bool ignoreAll;

/// Creates a copy of this object with the given fields replaced
/// with the new values.
IgnoredLeaksSet copyWith({Map<String, int?>? byClass, bool? ignoreAll}) {
return IgnoredLeaksSet(
ignoreAll: ignoreAll ?? this.ignoreAll,
byClass: byClass ?? this.byClass,
);
}

/// Merges two ignore lists.
///
/// In the result object the ignore limit for a class is maximum of two original ignore limits.
IgnoredLeaksSet merge(IgnoredLeaksSet? other) {
if (other == null) return this;
final map = {...byClass};
for (final theClass in other.byClass.keys) {
if (!map.containsKey(theClass)) {
map[theClass] = other.byClass[theClass];
continue;
}
final int? otherCount = other.byClass[theClass];
final int? thisCount = byClass[theClass];
if (thisCount == null || otherCount == null) {
map[theClass] = null;
continue;
}
map[theClass] = max(thisCount, otherCount);
}
return IgnoredLeaksSet(
byClass: map,
ignoreAll: ignoreAll || other.ignoreAll,
);
}

/// Removes the classes from ignore lists.
IgnoredLeaksSet track(List<String> list) {
if (list.isEmpty) return this;
final map = {...byClass};
list.forEach(map.remove);
return copyWith(byClass: map);
}

/// Returns true if the class should be ignored.
///
/// Returns false if limited number of leaks is set to be ignored, that is
/// `byClass[className] != null`.
bool isIgnored(String className) {
polina-c marked this conversation as resolved.
Show resolved Hide resolved
if (ignoreAll) return true;
return byClass.containsKey(className) && byClass[className] == null;
}
}

/// The total set of ignored leaks for both [notGCed] and [notDisposed] leaks.
class IgnoredLeaks {
const IgnoredLeaks({
this.notGCed = const IgnoredLeaksSet(),
this.notDisposed = const IgnoredLeaksSet(),
});

/// Ignore list for notGCed leaks.
final IgnoredLeaksSet notGCed;

/// Ignore list for notDisposed leaks.
final IgnoredLeaksSet notDisposed;

/// Returns true if the class is ignored.
///
/// If [leakType] is null, returns true if the class is ignored for all
/// leak types.
bool isIgnored(String className, {LeakType? leakType}) {
switch (leakType) {
case null:
return notGCed.isIgnored(className) && notDisposed.isIgnored(className);
case LeakType.notDisposed:
return notDisposed.isIgnored(className);
case LeakType.notGCed:
case LeakType.gcedLate:
return notGCed.isIgnored(className);
}
}
}

/// Configuration for diagnostics.
///
/// Stacktrace and retaining path collection can seriously affect performance and memory footprint.
Expand All @@ -58,7 +167,7 @@ class LeakDiagnosticConfig {

/// If true, retaining path will be collected for non-GCed objects.
///
/// The collection of retaining path a blocking asyncronous call.
/// The collection of retaining path is a blocking asyncronous call.
/// In release mode this flag does not work.
final bool collectRetainingPathForNotGCed;

Expand Down Expand Up @@ -259,6 +368,10 @@ class MemoryBaselining {
this.baseline,
}) : assert(!(mode == BaseliningMode.regression && baseline == null));

const MemoryBaselining.none()
: mode = BaseliningMode.none,
baseline = null;

final BaseliningMode mode;

final MemoryBaseline? baseline;
Expand All @@ -278,6 +391,9 @@ class MemoryBaselining {
}

enum BaseliningMode {
/// No baselining.
none,

/// Measure memory footprint and output to console when phase is finished.
measure,

Expand Down
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: 9.0.8
version: 9.0.9
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
21 changes: 21 additions & 0 deletions pkgs/leak_tracker/test/tests/leak_tracking/model_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,25 @@ void main() {
expect(sampler.deltaMax, 1);
});
});

group('$IgnoredLeaksSet', () {
test('merges', () {
const list1 = IgnoredLeaksSet.byClass({'class1': null});
const list2 = IgnoredLeaksSet.byClass({'class2': null});

final result = list1.merge(list2);

expect(result.isIgnored('class1'), true);
expect(result.isIgnored('class2'), true);
});

test('removes', () {
const list = IgnoredLeaksSet.byClass({'class1': null, 'class2': null});

final result = list.track(['class1']);

expect(result.isIgnored('class1'), false);
expect(result.isIgnored('class2'), true);
});
});
}
4 changes: 4 additions & 0 deletions pkgs/leak_tracker_flutter_testing/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# 1.0.7

* Make configuration adjustable.

# 1.0.6

* If an object is not disposed by the end of testing, mark it as notDisposed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

export 'src/leak_testing.dart';
export 'src/matchers.dart';
export 'src/model.dart';
export 'src/test_widgets.dart';
Loading