diff options
author | jeffhao <jeffhao@google.com> | 2011-09-29 17:41:15 -0700 |
---|---|---|
committer | jeffhao <jeffhao@google.com> | 2011-09-29 17:41:15 -0700 |
commit | 5d1ac920fdaef5d4ec8f66bb734488cd9660b024 (patch) | |
tree | dd372f306ab70f4c86759869b1f74eca62ff6f2b /test/067-preemptive-unpark | |
parent | c31664f3d82e6cd68275a529a8a73f067a52e8be (diff) | |
download | art-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.tar.gz art-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.tar.bz2 art-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.zip |
Adding old unit tests to test suite.
These tests are copied straight over. They'll still run, but they're
using the old system.
Change-Id: If494519e52ddf858a9febfc55bdae830468cb3c8
Diffstat (limited to 'test/067-preemptive-unpark')
-rw-r--r-- | test/067-preemptive-unpark/expected.txt | 5 | ||||
-rw-r--r-- | test/067-preemptive-unpark/info.txt | 1 | ||||
-rw-r--r-- | test/067-preemptive-unpark/src/Main.java | 107 |
3 files changed, 113 insertions, 0 deletions
diff --git a/test/067-preemptive-unpark/expected.txt b/test/067-preemptive-unpark/expected.txt new file mode 100644 index 0000000000..12bfee059e --- /dev/null +++ b/test/067-preemptive-unpark/expected.txt @@ -0,0 +1,5 @@ +Test starting +GC'ing +Asking thread to park +park() returned quickly +Test succeeded! diff --git a/test/067-preemptive-unpark/info.txt b/test/067-preemptive-unpark/info.txt new file mode 100644 index 0000000000..0bc0c61c13 --- /dev/null +++ b/test/067-preemptive-unpark/info.txt @@ -0,0 +1 @@ +Test that Unsafe.unpark() operates as expected, in particular across a gc. diff --git a/test/067-preemptive-unpark/src/Main.java b/test/067-preemptive-unpark/src/Main.java new file mode 100644 index 0000000000..a16219e60b --- /dev/null +++ b/test/067-preemptive-unpark/src/Main.java @@ -0,0 +1,107 @@ +import sun.misc.Unsafe; + +import java.lang.reflect.Field; + +public class Main { + private static Unsafe UNSAFE; + + public static void main(String[] args) throws Exception { + setUp(); + + ParkTester test = new ParkTester(); + + System.out.println("Test starting"); + + test.start(); + UNSAFE.unpark(test); + clearStack(10); + + System.out.println("GC'ing"); + System.gc(); + System.gc(); + + System.out.println("Asking thread to park"); + test.parkNow = true; + + try { + Thread.sleep(1500); + } catch (InterruptedException ex) { + // Ignore it. + } + + if (test.success) { + System.out.println("Test succeeded!"); + } else { + System.out.println("Test failed."); + } + } + + /** + * Set up {@link #UNSAFE}. + */ + public static void setUp() { + /* + * Subvert the access check to get the unique Unsafe instance. + * We can do this because there's no security manager + * installed when running the test. + */ + try { + Field field = Unsafe.class.getDeclaredField("THE_ONE"); + field.setAccessible(true); + + UNSAFE = (Unsafe) field.get(null); + } catch (NoSuchFieldException ex) { + throw new RuntimeException(ex); + } catch (IllegalAccessException ex) { + throw new RuntimeException(ex); + } + } + + /** + * Scribbles on the stack to help ensure we don't have a fake + * pointer that would keep would-be garbage alive. + */ + private static void clearStack(int depth) { + int a = 0; + int b = 0; + int c = 0; + int d = 0; + int e = 0; + int f = 0; + int g = 0; + int h = 0; + int i = 0; + int j = 0; + + if (depth > 0) { + clearStack(depth - 1); + } + } + + private static class ParkTester extends Thread { + public volatile boolean parkNow = false; + public volatile boolean success = false; + + public void run() { + while (!parkNow) { + try { + Thread.sleep(500); + } catch (InterruptedException ex) { + // Ignore it. + } + } + + long start = System.currentTimeMillis(); + UNSAFE.park(false, 500 * 1000000); // 500 msec + long elapsed = System.currentTimeMillis() - start; + + if (elapsed > 200) { + System.out.println("park()ed for " + elapsed + " msec"); + success = false; + } else { + System.out.println("park() returned quickly"); + success = true; + } + } + } +} |