#1260: Added tests for Finalizer.
diff --git a/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t01.dart b/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t01.dart
index 3b00cfc..9396afc 100644
--- a/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t01.dart
+++ b/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t01.dart
@@ -19,7 +19,8 @@
class A {}
int called = 0;
-final Finalizer finalizer = Finalizer((_) {
+final Finalizer finalizer = Finalizer((token) {
+ Expect.isNull(token);
called++;
});
diff --git a/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t02.dart b/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t02.dart
index 9067d6a..6d6fdd9 100644
--- a/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t02.dart
+++ b/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t02.dart
@@ -18,12 +18,13 @@
int called = 0;
-final Finalizer finalizer = Finalizer((_) {
+final Finalizer finalizer = Finalizer((token) {
+ Expect.equals("abc", token);
called++;
});
main() {
- finalizer.attach(Object(), null);
- triggerGcWithDelay();
- Expect.equals(1, called);
+ finalizer.attach(Object(), "abc");
+ triggerGcWithDelay(); // Call FullGC 3 times
+ Expect.equals(1, called); // Callback function should be called only once
}
diff --git a/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t03.dart b/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t03.dart
index e9e6087..6024dee 100644
--- a/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t03.dart
+++ b/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t03.dart
@@ -18,15 +18,16 @@
int called = 0;
-final Finalizer finalizer = Finalizer((_) {
+final Finalizer finalizer = Finalizer((token) {
+ Expect.equals("Testme", token);
called++;
});
main() {
Object? obj = Object();
- finalizer.attach(obj, null);
+ finalizer.attach(obj, "Testme");
print(obj);
obj = null;
- triggerGcWithDelay();
- Expect.equals(1, called);
+ triggerGcWithDelay(); // Call FullGC 3 times
+ Expect.equals(1, called); // Finalizer should be called only once
}
diff --git a/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t04.dart b/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t04.dart
index b7a4271..577af5b 100644
--- a/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t04.dart
+++ b/LanguageFeatures/FinalizationRegistry/Finalizer/Finalizer_A01_t04.dart
@@ -18,19 +18,20 @@
int called = 0;
-final Finalizer finalizer = Finalizer((_) {
+final Finalizer finalizer = Finalizer((token) {
+ Expect.equals(123, token);
called++;
});
test() {
Object obj = Object();
- finalizer.attach(obj, null);
+ finalizer.attach(obj, 123);
triggerGcWithDelay();
Expect.equals(0, called);
}
main() {
test();
- triggerGcWithDelay();
- Expect.equals(1, called);
+ triggerGcWithDelay(); // Call FullGC 3 times
+ Expect.equals(1, called); // Finalizer should be called only once
}
diff --git a/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A01_t01.dart b/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A01_t01.dart
new file mode 100644
index 0000000..7eb59ed
--- /dev/null
+++ b/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A01_t01.dart
@@ -0,0 +1,61 @@
+// 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 void attach(
+/// Object value,
+/// T finalizationToken,
+/// {Object? detach}
+/// )
+/// Attaches this finalizer to [value].
+///
+/// When [value] is longer accessible to the program, while still having an
+/// attachement to this finalizer, the [callback] of this finalizer may be
+/// called with [finalizationToken] as argument.
+///
+/// @description Checks that finalizer can be attached to the different objects
+/// and token is passed as a callback function parameter when object becomes
+/// inaccessible.
+/// @author iarkh@unipro.ru
+
+import "../gc_utils_lib.dart";
+import "../../../Utils/expect.dart";
+
+class A{}
+Object? returnedToken;
+int cnt = 0;
+
+final Finalizer finalizer = Finalizer((token) {
+ returnedToken = token;
+ cnt++;
+});
+
+main() {
+ {
+ Object ? obj = Object();
+ finalizer.attach(obj, "Just a string");
+ print(obj);
+ }
+ triggerGcWithDelay();
+ Expect.equals("Just a string", returnedToken);
+
+ {
+ Object? obj = A();
+ finalizer.attach(obj, 15);
+ print(obj);
+ obj = null;
+ triggerGcWithDelay();
+ Expect.equals(15, returnedToken);
+ }
+
+ {
+ Object obj = List.filled(100, 1);
+ finalizer.attach(obj, []);
+ print(obj);
+ obj = A();
+ triggerGcWithDelay();
+ Expect.equals([], returnedToken);
+ }
+
+ Expect.equals(3, cnt);
+}
diff --git a/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A01_t02.dart b/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A01_t02.dart
new file mode 100644
index 0000000..a0051bb
--- /dev/null
+++ b/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A01_t02.dart
@@ -0,0 +1,39 @@
+// 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 void attach(
+/// Object value,
+/// T finalizationToken,
+/// {Object? detach}
+/// )
+/// Attaches this finalizer to [value].
+///
+/// When [value] is longer accessible to the program, while still having an
+/// attachement to this finalizer, the [callback] of this finalizer may be
+/// called with [finalizationToken] as argument.
+///
+/// @description Checks that finalizer with the given [detach] token can be
+/// detached from the object.
+/// @author iarkh@unipro.ru
+
+import "../gc_utils_lib.dart";
+import "../../../Utils/expect.dart";
+
+class A{}
+
+int cnt = 0;
+final Finalizer finalizer = Finalizer((token) {
+ cnt++;
+});
+
+main() {
+ A detachToken = A();
+ {
+ Object value = Object();
+ finalizer.attach(value, "Finalization token", detach: detachToken);
+ finalizer.detach(detachToken);
+ }
+ triggerGcWithDelay();
+ Expect.equals(0, cnt);
+}
diff --git a/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A01_t03.dart b/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A01_t03.dart
new file mode 100644
index 0000000..7c943a8
--- /dev/null
+++ b/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A01_t03.dart
@@ -0,0 +1,61 @@
+// 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 void attach(
+/// Object value,
+/// T finalizationToken,
+/// {Object? detach}
+/// )
+/// Attaches this finalizer to [value].
+///
+/// When [value] is longer accessible to the program, while still having an
+/// attachement to this finalizer, the [callback] of this finalizer may be
+/// called with [finalizationToken] as argument.
+///
+/// @description Checks that generic finalizer can be attached to the different
+/// objects and token is passed as a callback function parameter when object
+/// becomes inaccessible.
+/// @author iarkh@unipro.ru
+
+import "../gc_utils_lib.dart";
+import "../../../Utils/expect.dart";
+
+class A{}
+Object? returnedToken;
+int cnt = 0;
+
+final Finalizer<int> finalizer = Finalizer((token) {
+ returnedToken = token;
+ cnt++;
+});
+
+main() {
+ {
+ Object ? obj = Object();
+ finalizer.attach(obj, 1);
+ print(obj);
+ }
+ triggerGcWithDelay();
+ Expect.equals(1, returnedToken);
+
+ {
+ Object? obj = A();
+ finalizer.attach(obj, 15);
+ print(obj);
+ obj = null;
+ triggerGcWithDelay();
+ Expect.equals(15, returnedToken);
+ }
+
+ {
+ Object obj = List.filled(100, 1);
+ finalizer.attach(obj, 14);
+ print(obj);
+ obj = A();
+ triggerGcWithDelay();
+ Expect.equals([], returnedToken);
+ }
+
+ Expect.equals(3, cnt);
+}
diff --git a/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A01_t04.dart b/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A01_t04.dart
new file mode 100644
index 0000000..c1460d4
--- /dev/null
+++ b/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A01_t04.dart
@@ -0,0 +1,39 @@
+// 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 void attach(
+/// Object value,
+/// T finalizationToken,
+/// {Object? detach}
+/// )
+/// Attaches this finalizer to [value].
+///
+/// When [value] is longer accessible to the program, while still having an
+/// attachement to this finalizer, the [callback] of this finalizer may be
+/// called with [finalizationToken] as argument.
+///
+/// @description Checks that generic finalizer with the given [detach] token can
+/// be detached from the object.
+/// @author iarkh@unipro.ru
+
+import "../gc_utils_lib.dart";
+import "../../../Utils/expect.dart";
+
+class A{}
+
+int cnt = 0;
+final Finalizer<Object> finalizer = Finalizer((token) {
+ cnt++;
+});
+
+main() {
+ A detachToken = A();
+ {
+ Object value = Object();
+ finalizer.attach(value, "Finalization token", detach: detachToken);
+ finalizer.detach(detachToken);
+ }
+ triggerGcWithDelay();
+ Expect.equals(0, cnt);
+}
diff --git a/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A02_t01.dart b/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A02_t01.dart
new file mode 100644
index 0000000..8c88ee2
--- /dev/null
+++ b/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A02_t01.dart
@@ -0,0 +1,55 @@
+// 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 void attach(
+/// Object value,
+/// T finalizationToken,
+/// {Object? detach}
+/// )
+/// Attaches this finalizer to [value].
+///
+/// When [value] is longer accessible to the program, while still having an
+/// attachement to this finalizer, the [callback] of this finalizer may be
+/// called with [finalizationToken] as argument.
+///
+/// @description Checks that callback function cannot be called while
+/// finalization token is still accessible.
+/// @author iarkh@unipro.ru
+
+import "../gc_utils_lib.dart";
+import "../../../Utils/expect.dart";
+
+class A{}
+
+bool called = false;
+final Finalizer finalizer = Finalizer((_) {
+ called = true;
+});
+
+void test(Object o) {
+ triggerGcWithDelay();
+ Expect.isFalse(called);
+}
+
+Object test1(Object obj) => obj;
+
+main() {
+ Object value = Object();
+ finalizer.attach(value, "Finalization token");
+ triggerGcWithDelay();
+ Expect.isFalse(called);
+ triggerGcWithDelay();
+ Expect.isFalse(called);
+ test(value);
+
+ Object value1 = value;
+ value = 12345;
+ triggerGcWithDelay();
+ Expect.isFalse(called);
+
+ var value2 = test1(value1);
+ value1 = value;
+ triggerGcWithDelay();
+ Expect.isFalse(called);
+}
diff --git a/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A02_t02.dart b/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A02_t02.dart
new file mode 100644
index 0000000..b3d2585
--- /dev/null
+++ b/LanguageFeatures/FinalizationRegistry/Finalizer/attach_A02_t02.dart
@@ -0,0 +1,54 @@
+// 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 void attach(
+/// Object value,
+/// T finalizationToken,
+/// {Object? detach}
+/// )
+/// Attaches this finalizer to [value].
+///
+/// When [value] is longer accessible to the program, while still having an
+/// attachement to this finalizer, the [callback] of this finalizer may be
+/// called with [finalizationToken] as argument.
+///
+/// @description Checks that callback function cannot be called only once when
+/// finalization token is unaccessible.
+/// @author iarkh@unipro.ru
+
+import "../gc_utils_lib.dart";
+import "../../../Utils/expect.dart";
+
+class A{}
+
+int called = 0;
+final Finalizer finalizer = Finalizer((_) {
+ called++;
+});
+
+void test(Object o) {
+ triggerGcWithDelay();
+}
+
+Object test1(Object obj) => obj;
+
+main() {
+ Object value = Object();
+ finalizer.attach(value, "Finalization token");
+
+ value = 12345;
+
+ // Initial object is not accessible anymore.
+ // Do something, call triggerGC sevewral times and check that callback is
+ // called only once during the execution.
+ test(value);
+ triggerGcWithDelay();
+
+ var value1 = test1(value);
+ triggerGcWithDelay();
+ triggerGcWithDelay();
+ triggerGcWithDelay();
+
+ Expect.equals(1, called);
+}