Skip to content

Commit

Permalink
Update _object_tracker_test.dart
Browse files Browse the repository at this point in the history
  • Loading branch information
polina-c committed Sep 20, 2023
1 parent 8268041 commit a1a6fec
Showing 1 changed file with 22 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import 'package:clock/clock.dart';
import 'package:leak_tracker/leak_tracker.dart';
import 'package:leak_tracker/src/leak_tracking/_object_record.dart';
import 'package:leak_tracker/src/leak_tracking/_object_tracker.dart';
import 'package:leak_tracker/src/leak_tracking/_primitives/_finalizer.dart';
import 'package:leak_tracker/src/leak_tracking/_primitives/_gc_counter.dart';
Expand Down Expand Up @@ -157,7 +158,7 @@ void main() {
});

test('uses finalizer.', () {
const theObject = '-';
const theObject = [];
tracker.startTracking(
theObject,
context: null,
Expand All @@ -172,7 +173,7 @@ void main() {

test('does not false positive.', () {
// Define object and time.
const theObject = '-';
const theObject = [];
var time = DateTime(2000);

// Start tracking.
Expand All @@ -197,7 +198,7 @@ void main() {

test('tracks ${LeakType.notDisposed}.', () async {
// Define object.
const theObject = '-';
const theObject = [];

// Start tracking and GC.
tracker.startTracking(
Expand All @@ -214,7 +215,7 @@ void main() {

test('tracks ${LeakType.notGCed}.', () async {
// Define object and time.
const theObject = '-';
const theObject = [];
var time = DateTime(2000);

// Start tracking and dispose.
Expand All @@ -240,7 +241,7 @@ void main() {

test('tracks ${LeakType.gcedLate}.', () async {
// Define object and time.
const theObject = '-';
const theObject = [];
var time = DateTime(2000);

// Start tracking and dispose.
Expand All @@ -267,7 +268,7 @@ void main() {

test('tracks ${LeakType.gcedLate} lifecycle accurately.', () async {
// Define object and time.
const theObject = '-';
const theObject = [];
var time = DateTime(2000);

// Start tracking and dispose.
Expand Down Expand Up @@ -297,7 +298,7 @@ void main() {

test('collects context accurately.', () async {
// Define object and time.
const theObject = '-';
const theObject = [];
var time = DateTime(2000);

// Start tracking and dispose.
Expand Down Expand Up @@ -347,7 +348,7 @@ void main() {

test('collects stack traces.', () async {
// Define object and time.
const theObject = '-';
const theObject = [];
var time = DateTime(2000);

// Start tracking and dispose.
Expand Down Expand Up @@ -598,27 +599,31 @@ class _MockFinalizerWrapper implements FinalizerWrapper {
_MockFinalizerWrapper(this.onGc);

final ObjectGcCallback onGc;
final attached = <Object>{};
final attached = <Object, Object>{};

@override
void attach(Object object, Object finalizationToken, {Object? detach}) {
final int code = identityHashCode(object);
if (attached.contains(code)) throw '`attach` should not be invoked twice';
attached.add(code);
if (attached.containsValue(object)) {
throw '`attach` should not be invoked twice';
}
if (attached.containsKey(finalizationToken)) {
throw 'tokens should not duplicate';
}
attached[finalizationToken] = object;
}

void finalize(Object code) {
if (!attached.contains(code)) return;
onGc(code);
attached.remove(code);
void finalize(Object finalizationToken) {
if (!attached.containsKey(finalizationToken)) return;
onGc(finalizationToken);
attached.remove(finalizationToken);
}
}

class _MockFinalizerBuilder {
late final _MockFinalizerWrapper finalizer;

void gc(Object object) {
finalizer.finalize(identityHashCode(object));
finalizer.finalize(object);
}

_MockFinalizerWrapper build(ObjectGcCallback onGc) {
Expand Down

0 comments on commit a1a6fec

Please sign in to comment.