diff options
author | Brian Carlstrom <bdc@google.com> | 2014-02-20 13:50:05 -0800 |
---|---|---|
committer | Brian Carlstrom <bdc@google.com> | 2014-02-20 14:29:12 -0800 |
commit | c849445e6f1c336a7e8024310d9abfc8891244b8 (patch) | |
tree | 1d4ddad3179bd9f05b96b9e87ae9290246a555ec /test | |
parent | 24322544817fe9643a463313cb7c14a55e95012f (diff) | |
download | art-c849445e6f1c336a7e8024310d9abfc8891244b8.zip art-c849445e6f1c336a7e8024310d9abfc8891244b8.tar.gz art-c849445e6f1c336a7e8024310d9abfc8891244b8.tar.bz2 |
Try to make 036-finalizer less flaky by moving output to one thread
Bug: 13108062
Change-Id: I946044d007dda4efa85794e2361ecb2ce102be64
Diffstat (limited to 'test')
-rw-r--r-- | test/036-finalizer/expected.txt | 7 | ||||
-rw-r--r-- | test/036-finalizer/src/FinalizerTest.java | 37 | ||||
-rw-r--r-- | test/036-finalizer/src/Main.java | 39 |
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; + } + } } |