diff options
author | 2014-02-10 14:15:56 -0800 | |
---|---|---|
committer | 2014-02-10 15:03:50 -0800 | |
commit | cbaf9872beda2c2b5b0db122b0619bb28e27efff (patch) | |
tree | d1a62d3204461c751a7d8b5eb5bb5a8f27140661 | |
parent | 35d7e414134bd9f3d39e018a756617b21d49c877 (diff) |
Add regression test for Thread.interrupted
Bug: 12929305
Change-Id: Ic896e164da0f67babdd423d5adb6e84aaa80cab2
-rw-r--r-- | test/051-thread/expected.txt | 15 | ||||
-rw-r--r-- | test/051-thread/src/Main.java | 98 |
2 files changed, 64 insertions, 49 deletions
diff --git a/test/051-thread/expected.txt b/test/051-thread/expected.txt index 8e6b153a03..7139b7f027 100644 --- a/test/051-thread/expected.txt +++ b/test/051-thread/expected.txt @@ -1,8 +1,9 @@ -Initializing System.out... -Thread count: 512 -Starting thread 'Thready' -@ Thread running -@ Got expected setDaemon exception -@ Thread bailing -Thread starter returning +thread test starting +testThreadCapacity thread count: 512 +testThreadDaemons starting thread 'TestDaemonThread' +testThreadDaemons @ Thread running +testThreadDaemons @ Got expected setDaemon exception +testThreadDaemons @ Thread bailing +testThreadDaemons finished +testSleepZero finished thread test done diff --git a/test/051-thread/src/Main.java b/test/051-thread/src/Main.java index 911c739d92..608b7e0878 100644 --- a/test/051-thread/src/Main.java +++ b/test/051-thread/src/Main.java @@ -21,32 +21,51 @@ import java.util.ArrayList; */ public class Main { public static void main(String[] args) throws Exception { - System.out.println("Initializing System.out..."); + System.out.println("thread test starting"); + testThreadCapacity(); + testThreadDaemons(); + testSleepZero(); + System.out.println("thread test done"); + } - MyThread[] threads = new MyThread[512]; + /* + * Simple thread capacity test. + */ + private static void testThreadCapacity() throws Exception { + TestCapacityThread[] threads = new TestCapacityThread[512]; for (int i = 0; i < 512; i++) { - threads[i] = new MyThread(); + threads[i] = new TestCapacityThread(); } - for (MyThread thread : threads) { + for (TestCapacityThread thread : threads) { thread.start(); } - for (MyThread thread : threads) { + for (TestCapacityThread thread : threads) { thread.join(); } - System.out.println("Thread count: " + MyThread.mCount); + System.out.println("testThreadCapacity thread count: " + TestCapacityThread.mCount); + } - go(); - System.out.println("thread test done"); + private static class TestCapacityThread extends Thread { + static int mCount = 0; + public void run() { + synchronized (TestCapacityThread.class) { + ++mCount; + } + try { + sleep(1000); + } catch (Exception ex) { + } + } } - public static void go() { - Thread t = new Thread(null, new ThreadTestSub(), "Thready", 7168); + private static void testThreadDaemons() { + Thread t = new Thread(null, new TestDaemonThread(), "TestDaemonThread", 7168); t.setDaemon(false); - System.out.print("Starting thread '" + t.getName() + "'\n"); + System.out.print("testThreadDaemons starting thread '" + t.getName() + "'\n"); t.start(); try { @@ -55,47 +74,42 @@ public class Main { ex.printStackTrace(); } - System.out.print("Thread starter returning\n"); + System.out.print("testThreadDaemons finished\n"); } - /* - * Simple thread capacity test. - */ - static class MyThread extends Thread { - static int mCount = 0; + private static class TestDaemonThread implements Runnable { public void run() { - synchronized (MyThread.class) { - ++mCount; + System.out.print("testThreadDaemons @ Thread running\n"); + + try { + Thread.currentThread().setDaemon(true); + System.out.print("testThreadDaemons @ FAILED: setDaemon() succeeded\n"); + } catch (IllegalThreadStateException itse) { + System.out.print("testThreadDaemons @ Got expected setDaemon exception\n"); } + try { - sleep(1000); - } catch (Exception ex) { + Thread.sleep(2000); + } + catch (InterruptedException ie) { + System.out.print("testThreadDaemons @ Interrupted!\n"); + } + finally { + System.out.print("testThreadDaemons @ Thread bailing\n"); } } } -} - -class ThreadTestSub implements Runnable { - public void run() { - System.out.print("@ Thread running\n"); + private static void testSleepZero() throws Exception { + Thread.currentThread().interrupt(); try { - Thread.currentThread().setDaemon(true); - System.out.print("@ FAILED: setDaemon() succeeded\n"); - } catch (IllegalThreadStateException itse) { - System.out.print("@ Got expected setDaemon exception\n"); - } - - //if (true) - // throw new NullPointerException(); - try { - Thread.sleep(2000); - } - catch (InterruptedException ie) { - System.out.print("@ Interrupted!\n"); - } - finally { - System.out.print("@ Thread bailing\n"); + Thread.sleep(0); + throw new AssertionError("unreachable"); + } catch (InterruptedException e) { + if (Thread.currentThread().isInterrupted()) { + throw new AssertionError("thread is interrupted"); + } } + System.out.print("testSleepZero finished\n"); } } |