Skip to content

Commit

Permalink
#1260: Added tests for Finalizer.attach method.
Browse files Browse the repository at this point in the history
  • Loading branch information
iarkh committed Feb 10, 2022
1 parent a863781 commit f49d43a
Show file tree
Hide file tree
Showing 27 changed files with 456 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ final Finalizer finalizer = Finalizer((token) {
cnt++;
});

main() {
main() async {
{
Object ? obj = Object();
finalizer.attach(obj, "Just a string");
print(obj);
}
triggerGcWithDelay();
await triggerGcWithDelay();
Expect.equals("Just a string", returnedToken);

{
Object? obj = A();
finalizer.attach(obj, 15);
print(obj);
obj = null;
triggerGcWithDelay();
await triggerGcWithDelay();
Expect.equals(15, returnedToken);
}

Expand All @@ -53,7 +53,7 @@ main() {
finalizer.attach(obj, []);
print(obj);
obj = A();
triggerGcWithDelay();
await triggerGcWithDelay();
Expect.equals([], returnedToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ final Finalizer finalizer = Finalizer((token) {
cnt++;
});

main() {
main() async {
A detachToken = A();
{
Object value = Object();
finalizer.attach(value, "Finalization token", detach: detachToken);
finalizer.detach(detachToken);
}
triggerGcWithDelay();
await triggerGcWithDelay();
Expect.equals(0, cnt);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@ final Finalizer<int> finalizer = Finalizer((token) {
cnt++;
});

main() {
main() async {
{
Object ? obj = Object();
Object? obj = Object();
finalizer.attach(obj, 1);
print(obj);
}
triggerGcWithDelay();
await triggerGcWithDelay();
Expect.equals(1, returnedToken);

{
Object? obj = A();
finalizer.attach(obj, 15);
print(obj);
obj = null;
triggerGcWithDelay();
await triggerGcWithDelay();
Expect.equals(15, returnedToken);
}

Expand All @@ -53,7 +53,7 @@ main() {
finalizer.attach(obj, 14);
print(obj);
obj = A();
triggerGcWithDelay();
await triggerGcWithDelay();
Expect.equals([], returnedToken);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ final Finalizer<Object> finalizer = Finalizer((token) {
cnt++;
});

main() {
main() async {
A detachToken = A();
{
Object value = Object();
finalizer.attach(value, "Finalization token", detach: detachToken);
finalizer.detach(detachToken);
}
triggerGcWithDelay();
await triggerGcWithDelay();
Expect.equals(0, cnt);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,29 @@ final Finalizer finalizer = Finalizer((_) {
called = true;
});

void test(Object o) {
triggerGcWithDelay();
void test(Object o) async {
await triggerGcWithDelay();
Expect.isFalse(called);
}

Object test1(Object obj) => obj;

main() {
main() async {
Object value = Object();
finalizer.attach(value, "Finalization token");
triggerGcWithDelay();
await triggerGcWithDelay();
Expect.isFalse(called);
triggerGcWithDelay();
await triggerGcWithDelay();
Expect.isFalse(called);
test(value);

Object value1 = value;
value = 12345;
triggerGcWithDelay();
await triggerGcWithDelay();
Expect.isFalse(called);

var value2 = test1(value1);
value1 = value;
triggerGcWithDelay();
await triggerGcWithDelay();
Expect.isFalse(called);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ final Finalizer finalizer = Finalizer((_) {
});

void test(Object o) {
triggerGcWithDelay();
triggerGc();
}

Object test1(Object obj) => obj;

main() {
main() async {
Object value = Object();
finalizer.attach(value, "Finalization token");

Expand All @@ -34,12 +34,15 @@ main() {
// Do something, call triggerGC several times and check that callback was
// called only once during the execution.
test(value);
triggerGcWithDelay();
await triggerGcWithDelay();

var value1 = test1(value);
triggerGcWithDelay();
triggerGcWithDelay();
triggerGcWithDelay();
await triggerGcWithDelay();
triggerGc();
await triggerGcWithDelay();
triggerGc();
triggerGc();
await triggerGcWithDelay();

Expect.equals(1, called);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void test(Object o) {

Object test1(Object obj) => obj;

main() {
main() async {
Object value = Object();
Object detachToken = A();
finalizer.attach(value, "Finalization token", detach: detachToken);
Expand All @@ -38,10 +38,11 @@ main() {
// Do something, call triggerGC several times and check that callback was
// not called during the execution.
test(value);
triggerGcWithDelay();
await triggerGcWithDelay();

var value1 = test1(value);
triggerGcWithDelay();
triggerGc();
await triggerGcWithDelay();
triggerGcWithDelay();
triggerGcWithDelay();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
/// @assertion The value and detach arguments ... Both must be objects supported
/// as an [Expando] key.
///
/// @description Checks that [value] must be supported as an [Expando] keys.
/// @description Checks that [value] must be supported as an [Expando] key.
/// Test String.
/// @author [email protected]
import "../gc_utils_lib.dart";
import "../../../Utils/expect.dart";

final Finalizer finalizer = Finalizer((_) { throw "Should not reach here"; });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
/// @assertion The value and detach arguments ... Both must be objects supported
/// as an [Expando] key.
///
/// @description Checks that [value] must be supported as an [Expando] keys.
/// @description Checks that [value] must be supported as an [Expando] key.
/// Test numbers.
/// @author [email protected]
import "../gc_utils_lib.dart";
import "../../../Utils/expect.dart";

final Finalizer finalizer = Finalizer((_) { throw "Should not reach here"; });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
/// @assertion The value and detach arguments ... Both must be objects supported
/// as an [Expando] key.
///
/// @description Checks that [value] must be supported as an [Expando] keys.
/// @description Checks that [value] must be supported as an [Expando] key.
/// Test booleans.
/// @author [email protected]
import "../gc_utils_lib.dart";
import "../../../Utils/expect.dart";

final Finalizer finalizer = Finalizer((_) { throw "Should not reach here"; });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@
/// @assertion The value and detach arguments ... Both must be objects supported
/// as an [Expando] key.
///
/// @description Checks that [value] must be supported as an [Expando] keys.
/// @description Checks that [value] must be supported as an [Expando] key.
/// Test [Null].
/// @author [email protected]
import "../gc_utils_lib.dart";
import "../../../Utils/expect.dart";

final Finalizer finalizer = Finalizer((_) { throw "Should not reach here"; });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// @assertion The value and detach arguments ... Both must be objects supported
/// as an [Expando] key.
///
/// @description Checks that [value] must be supported as an [Expando] keys.
/// @description Checks that [value] must be supported as an [Expando] key.
/// Test Test dart:ffi pointers.
/// @author [email protected]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// @assertion The value and detach arguments ... Both must be objects supported
/// as an [Expando] key.
///
/// @description Checks that [value] must be supported as an [Expando] keys.
/// @description Checks that [value] must be supported as an [Expando] key.
/// Test Test dart:ffi struct.
/// @author [email protected]
Expand All @@ -22,8 +22,8 @@ class S extends Struct {

main() {
Pointer<S> p = calloc<S>();
S s = p.ref;
try {
S s = p.ref;
Expect.throws(() {
finalizer.attach(p, "Finalization token");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// @assertion The value and detach arguments ... Both must be objects supported
/// as an [Expando] key.
///
/// @description Checks that [value] must be supported as an [Expando] keys.
/// @description Checks that [value] must be supported as an [Expando] key.
/// Test Test dart:ffi union.
/// @author [email protected]
Expand All @@ -24,8 +24,8 @@ class U extends Union {

main() {
Pointer<U> p = calloc<U>();
U u = p.ref;
try {
U u = p.ref;
Expect.throws(() {
finalizer.attach(u, "Finalization token");
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// 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.

/// @assertion The value and detach arguments ... Both must be objects supported
/// as an [Expando] key.
///
/// @description Checks that detach token must be supported as an [Expando] key.
/// Test String.
/// @author [email protected]
import "../../../Utils/expect.dart";

final Finalizer finalizer = Finalizer((_) { throw "Should not reach here"; });
Object object = Object();

main() {
Expect.throws(() {
finalizer.attach(object, "Finalization token", detach: "detachMe");
});

String s = "Just a string";
Expect.throws(() {
finalizer.attach(object, "Finalization token", detach: s);
});

dynamic d = s;
Expect.throws(() {
finalizer.attach(object, "Finalization token", detach: d);
});
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
// 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.

/// @assertion The value and detach arguments ... Both must be objects supported
/// as an [Expando] key.
///
/// @description Checks that detach token must be supported as an [Expando] key.
/// Test numbers.
/// @author [email protected]
import "../../../Utils/expect.dart";

final Finalizer finalizer = Finalizer((_) { throw "Should not reach here"; });
Object object = Object();

main() {
Expect.throws(() {
finalizer.attach(object, "Finalization token", detach: 12345);
});

var i = 3.14;
Expect.throws(() {
finalizer.attach(object, "Finalization token", detach: i);
});

int j = -12934;
Expect.throws(() {
finalizer.attach(object, "Finalization token", detach: j);
});

dynamic d = -12.376;
Expect.throws(() {
finalizer.attach(object, "Finalization token", detach: d);
});
}
Loading

0 comments on commit f49d43a

Please sign in to comment.