summaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorBrian Carlstrom <bdc@google.com>2014-02-20 22:49:58 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2014-02-20 22:49:58 +0000
commitf28072fdb6904ff6f715b1518a07e8efb897697f (patch)
tree32c5da0804483a09e1973d0dccf53a6b407e5ac2 /test
parentb4a4c6ae486c416e5187b257347912f541c9c50e (diff)
parentc849445e6f1c336a7e8024310d9abfc8891244b8 (diff)
downloadart-f28072fdb6904ff6f715b1518a07e8efb897697f.zip
art-f28072fdb6904ff6f715b1518a07e8efb897697f.tar.gz
art-f28072fdb6904ff6f715b1518a07e8efb897697f.tar.bz2
Merge "Try to make 036-finalizer less flaky by moving output to one thread"
Diffstat (limited to 'test')
-rw-r--r--test/036-finalizer/expected.txt7
-rw-r--r--test/036-finalizer/src/FinalizerTest.java37
-rw-r--r--test/036-finalizer/src/Main.java39
3 files changed, 35 insertions, 48 deletions
diff --git a/test/036-finalizer/expected.txt b/test/036-finalizer/expected.txt
index f9b29b0..a2a74fc 100644
--- a/test/036-finalizer/expected.txt
+++ b/test/036-finalizer/expected.txt
@@ -1,14 +1,13 @@
-wimp: wahoo
+wimp: [FinalizerTest message=wahoo, finalized=false]
gc
-finalizer executed: wahoo
wimp: null
finalize
wimp: null
sleep
-reborn: wahoo
+reborn: [FinalizerTest message=wahoo, finalized=true]
wimp: null
reset reborn
gc + finalize
sleep
-reborn: nothing
+reborn: [FinalizerTest message=nothing, finalized=false]
wimp: null
diff --git a/test/036-finalizer/src/FinalizerTest.java b/test/036-finalizer/src/FinalizerTest.java
deleted file mode 100644
index b0d014d..0000000
--- a/test/036-finalizer/src/FinalizerTest.java
+++ /dev/null
@@ -1,37 +0,0 @@
-/*
- * Copyright (C) 2008 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-import java.lang.ref.WeakReference;
-
-public class FinalizerTest {
- public static FinalizerTest mNothing = new FinalizerTest("nothing");
- public static FinalizerTest mReborn = mNothing;
-
- public String mMsg = "default";
-
- public FinalizerTest(String msg) {
- mMsg = msg;
- }
-
- public String toString() {
- return mMsg;
- }
-
- protected void finalize() {
- System.out.println("finalizer executed: " + mMsg);
- mReborn = this;
- }
-}
diff --git a/test/036-finalizer/src/Main.java b/test/036-finalizer/src/Main.java
index 6195aff..328425f 100644
--- a/test/036-finalizer/src/Main.java
+++ b/test/036-finalizer/src/Main.java
@@ -15,6 +15,8 @@
*/
import java.lang.ref.WeakReference;
+import java.util.ArrayList;
+import java.util.List;
/**
* Some finalizer tests.
@@ -31,18 +33,19 @@ public class Main {
}
}
- public static WeakReference makeRef() {
+ public static WeakReference<FinalizerTest> makeRef() {
/*
* Make ft in another thread, so there is no danger of
* a conservative reference leaking onto the main thread's
* stack.
*/
- final WeakReference[] wimp = new WeakReference[1];
+ final List<WeakReference<FinalizerTest>> wimp =
+ new ArrayList<WeakReference<FinalizerTest>>();
Thread t = new Thread() {
public void run() {
FinalizerTest ft = new FinalizerTest("wahoo");
- wimp[0] = new WeakReference(ft);
+ wimp.add(new WeakReference<FinalizerTest>(ft));
ft = null;
}
};
@@ -55,10 +58,10 @@ public class Main {
throw new RuntimeException(ie);
}
- return wimp[0];
+ return wimp.get(0);
}
- public static String wimpString(final WeakReference wimp) {
+ public static String wimpString(final WeakReference<FinalizerTest> wimp) {
/*
* Do the work in another thread, so there is no danger of a
* conservative reference to ft leaking onto the main thread's
@@ -68,7 +71,7 @@ public class Main {
final String[] s = new String[1];
Thread t = new Thread() {
public void run() {
- Object ref = wimp.get();
+ FinalizerTest ref = wimp.get();
if (ref != null) {
s[0] = ref.toString();
}
@@ -87,7 +90,7 @@ public class Main {
}
public static void main(String[] args) {
- WeakReference wimp = makeRef();
+ WeakReference<FinalizerTest> wimp = makeRef();
System.out.println("wimp: " + wimpString(wimp));
@@ -118,4 +121,26 @@ public class Main {
System.out.println("reborn: " + FinalizerTest.mReborn);
System.out.println("wimp: " + wimpString(wimp));
}
+
+ public static class FinalizerTest {
+ public static FinalizerTest mNothing = new FinalizerTest("nothing");
+ public static FinalizerTest mReborn = mNothing;
+
+ private final String message;
+ private boolean finalized = false;
+
+ public FinalizerTest(String message) {
+ this.message = message;
+ }
+
+ public String toString() {
+ return "[FinalizerTest message=" + message +
+ ", finalized=" + finalized + "]";
+ }
+
+ protected void finalize() {
+ finalized = true;
+ mReborn = this;
+ }
+ }
}