summaryrefslogtreecommitdiffstats
path: root/test/050-sync-test
diff options
context:
space:
mode:
authorjeffhao <jeffhao@google.com>2011-09-29 17:41:15 -0700
committerjeffhao <jeffhao@google.com>2011-09-29 17:41:15 -0700
commit5d1ac920fdaef5d4ec8f66bb734488cd9660b024 (patch)
treedd372f306ab70f4c86759869b1f74eca62ff6f2b /test/050-sync-test
parentc31664f3d82e6cd68275a529a8a73f067a52e8be (diff)
downloadandroid_art-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.tar.gz
android_art-5d1ac920fdaef5d4ec8f66bb734488cd9660b024.tar.bz2
android_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/050-sync-test')
-rw-r--r--test/050-sync-test/expected.txt34
-rw-r--r--test/050-sync-test/info.txt6
-rw-r--r--test/050-sync-test/src/Main.java179
-rw-r--r--test/050-sync-test/src/ThreadDeathHandler.java19
4 files changed, 238 insertions, 0 deletions
diff --git a/test/050-sync-test/expected.txt b/test/050-sync-test/expected.txt
new file mode 100644
index 0000000000..c2a70318bd
--- /dev/null
+++ b/test/050-sync-test/expected.txt
@@ -0,0 +1,34 @@
+Sleep Test
+GOING
+GONE
+
+Count Test
+going: 1
+going: 1
+going: 1
+going: 1
+going: 1
+going: 1
+going: 1
+going: 1
+going: 1
+going: 1
+Final result: 10
+going: 2
+going: 2
+going: 2
+going: 2
+going: 2
+going: 2
+going: 2
+going: 2
+going: 2
+going: 2
+Final result: 20
+main: all done
+
+Interrupt Test
+SleepyThread.run starting
+SleepyThread.run starting
+interrupting other (isAlive=true)
+thread#0 interrupted, flag=false
diff --git a/test/050-sync-test/info.txt b/test/050-sync-test/info.txt
new file mode 100644
index 0000000000..08127da231
--- /dev/null
+++ b/test/050-sync-test/info.txt
@@ -0,0 +1,6 @@
+This is a miscellaneous test that was imported into the new-at-the-time
+runtime test framework. The test is intended to exercise basic features,
+and as such cannot be build on top of junit, since failure of such basic
+features might disrupt junit.
+
+TODO: Real description goes here.
diff --git a/test/050-sync-test/src/Main.java b/test/050-sync-test/src/Main.java
new file mode 100644
index 0000000000..c2ea192e85
--- /dev/null
+++ b/test/050-sync-test/src/Main.java
@@ -0,0 +1,179 @@
+// Copyright 2006 The Android Open Source Project
+
+/**
+ * Test synchronization primitives.
+ *
+ * TODO: this should be re-written to be a little more rigorous and/or
+ * useful. Also, the ThreadDeathHandler stuff should be exposed or
+ * split out.
+ */
+public class Main {
+ public static void main(String[] args) {
+ System.out.println("Sleep Test");
+ sleepTest();
+
+ System.out.println("\nCount Test");
+ countTest();
+
+ System.out.println("\nInterrupt Test");
+ interruptTest();
+ }
+
+ static void sleepTest() {
+ System.out.println("GOING");
+ try {
+ Thread.sleep(1000);
+ }
+ catch (InterruptedException ie) {
+ System.out.println("INTERRUPT!");
+ ie.printStackTrace();
+ }
+ System.out.println("GONE");
+ }
+
+ static void countTest() {
+ CpuThread one, two;
+
+ one = new CpuThread(1);
+ two = new CpuThread(2);
+
+ one.start();
+ two.start();
+
+ try {
+ Thread.sleep(100);
+ }
+ catch (InterruptedException ie) {
+ System.out.println("INTERRUPT!");
+ ie.printStackTrace();
+ }
+
+ //System.out.println("main: off and running");
+
+ try {
+ one.join();
+ two.join();
+ }
+ catch (InterruptedException ie) {
+ System.out.println("INTERRUPT!");
+ ie.printStackTrace();
+ }
+ System.out.println("main: all done");
+ }
+
+ static void interruptTest() {
+ SleepyThread sleepy, pesky;
+
+ sleepy = new SleepyThread(null);
+ pesky = new SleepyThread(sleepy);
+
+ sleepy.setPriority(4);
+ sleepy.start();
+ pesky.start();
+ pesky.setPriority(3);
+ }
+}
+
+class CpuThread extends Thread {
+ static Object mSyncable = new Object();
+ static int mCount = 0;
+ int mNumber;
+
+ CpuThread(int num) {
+ super("CpuThread " + num);
+ mNumber = num;
+ }
+
+ public void run() {
+ //System.out.print("thread running -- ");
+ //System.out.println(Thread.currentThread().getName());
+
+ for (int i = 0; i < 10; i++) {
+ output(mNumber);
+ }
+
+ System.out.print("Final result: ");
+ System.out.println(mCount);
+ }
+
+ void output(int num) {
+ /*
+ * Delete the next line; last "final result" should != 20.
+ */
+ synchronized (mSyncable)
+ {
+ int i, count;
+
+ count = mCount;
+
+ System.out.print("going: ");
+ System.out.println(num);
+
+ /* burn CPU; adjust end value so we exceed scheduler quantum */
+ for (int j = 0; j < 5000; j++)
+ ;
+
+ count++;
+ mCount = count;
+ }
+ }
+}
+
+class SleepyThread extends Thread {
+ private SleepyThread mOther;
+ private Integer[] mWaitOnMe; // any type of object will do
+
+ private static int count = 0;
+
+ SleepyThread(SleepyThread other) {
+ mOther = other;
+ mWaitOnMe = new Integer[] { 1, 2 };
+
+ setName("thread#" + count);
+ count++;
+ }
+
+ public void run() {
+ System.out.println("SleepyThread.run starting");
+
+ if (false) {
+ ThreadDeathHandler threadHandler =
+ new ThreadDeathHandler("SYNC THREAD");
+ Thread.currentThread().setUncaughtExceptionHandler(threadHandler);
+ throw new NullPointerException("die");
+ }
+
+ if (mOther == null) {
+ boolean intr = false;
+
+ try {
+ synchronized (mWaitOnMe) {
+ mWaitOnMe.wait(9000);
+ }
+ }
+ catch (InterruptedException ie) {
+ // Expecting this; interrupted should be false.
+ System.out.println(Thread.currentThread().getName() +
+ " interrupted, flag=" + Thread.interrupted());
+ intr = true;
+ }
+ catch (Exception ex) {
+ ex.printStackTrace();
+ }
+
+ if (!intr)
+ System.out.println("NOT INTERRUPTED");
+ } else {
+ try {
+ Thread.sleep(2000);
+ }
+ catch (InterruptedException ie) {
+ System.out.println("PESKY INTERRUPTED?");
+ }
+
+ System.out.println("interrupting other (isAlive="
+ + mOther.isAlive() + ")");
+ mOther.interrupt();
+ }
+ }
+}
diff --git a/test/050-sync-test/src/ThreadDeathHandler.java b/test/050-sync-test/src/ThreadDeathHandler.java
new file mode 100644
index 0000000000..5ea61a52d0
--- /dev/null
+++ b/test/050-sync-test/src/ThreadDeathHandler.java
@@ -0,0 +1,19 @@
+// Copyright 2007 The Android Open Source Project
+
+import java.lang.Thread.UncaughtExceptionHandler;
+
+/**
+ * Report death-by-uncaught-exception.
+ */
+public class ThreadDeathHandler implements Thread.UncaughtExceptionHandler {
+ private String mMyMessage;
+
+ public ThreadDeathHandler(String msg) {
+ mMyMessage = msg;
+ }
+
+ public void uncaughtException(Thread t, Throwable e) {
+ System.err.println("Uncaught exception " + mMyMessage + "!");
+ e.printStackTrace();
+ }
+}