This is the mail archive of the mauve-discuss@sourceware.cygnus.com mailing list for the Mauve project. See the Mauve home page for more information.


[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index] [Subject Index] [Author Index] [Thread Index]

another suggestion for mauve




 Another suggestion/idea.
Right now, there's no guard against a test that calls check repeatedly.

I think a feature that prevents that would be useful.  Take a look at
the test I appended, specifically tests 4/5 (search for "watchdog").  It 
makes use of a watchdog thread.  If an operation times out, check(false) 
would be invoked in the mauve framework, if an operation succeeds, 
check(true), or vice versa, depending on what the desired semantics is.
Ideally, you would want to ignore or prevent the second call.

It is certainly possible to write test cases such that they call check()
only once, no doubt.  However, the test harness might be a place where
tests can share the necessary synchronization code to accomplish this.

For that to work, however, it appears that the tests must pass a
parameter to the check function;  this could for instance be the
testlet itself and an identifier, such as a string or a number.
Identifiers would be determined by the testlet.

So instead of check(true), it would be check(this, 1, true).
The harness would ignore subsequent calls to check(this, 1, _).
For backward compatibility, check(boolean b) could be implemented
as check(null, -1, b); where null and -1 would be out of the range
of valid testcase descriptors and ignored as such.

	- Godmar

/**
 * Tests for Thread.interrupt()
 * This tests only basic functionality, no corner cases.
 *
 * @author Godmar Back <gback@cs.utah.edu>
 */
import java.io.*;
import java.net.ServerSocket;

public class ThreadInterrupt {
    public static void main(String av[]) throws Exception {
	Thread t;
	t = new Thread() {
	    public void run() {
		synchronized(this) {
		    try {
			wait();
			System.out.println("FAIL: returned from wait"); 
		    } catch (InterruptedException e) {
			if (isInterrupted() || interrupted()) {
			    System.out.println(
				"FAIL: interrupted flag has been turned on."); 
			} else {
			    System.out.println("Success 0a.");
			}
		    }
		}
	    }
	};
	ssij(t);
	t = new Thread() {
	    public void run() {
		try {
		    Thread.sleep(5000);
		    System.out.println("FAIL: returned from sleep"); 
		} catch (InterruptedException e) {
		    if (isInterrupted() || interrupted()) {
			System.out.println(
			    "FAIL: interrupted flag has been turned on."); 
		    } else {
			System.out.println("Success 0b.");
		    }
		}
	    }
	};
	ssij(t);
	t = new Thread() {
	    public void run() {
		synchronized (this) {
		    try {
			wait();
		    } catch (InterruptedException e) {
			if (isInterrupted() || interrupted()) {
			    System.out.println(
				"FAIL: interrupted flag has been turned on."); 
			} 
		    }
		    if (isInterrupted() || interrupted()) {
			System.out.println(
			    "FAIL: interrupted flag has been turned on."); 
		    } 
		    try {
			Thread.sleep(1000);
		    } catch (InterruptedException e) {
			System.out.println("FAIL: " + e);
		    }
		}
	    }
	};
	t.start();
	Thread.sleep(1000);
	synchronized(t) {
	    t.interrupt();
	    if (t.isInterrupted()) {
		System.out.println("Success 1.");
	    } else {
		System.out.println("Failure 1.");
	    }
	    // make sure isInterrupted doesn't clear the flag.
	    if (t.isInterrupted()) {
		System.out.println("Success 2.");
	    } else {
		System.out.println("Failure 2.");
	    }
	}
	Thread.sleep(1000);		// let thread finish
	if (t.isInterrupted()) {
	    System.out.println("Failure 3.");
	} else {
	    System.out.println("Success 3.");
	}
	t.join();
	Thread watchdog = new Thread() {
	    public void run() {
		try {
		    Thread.sleep(3000);
		} catch (InterruptedException _) { }
		System.out.println("Failure 4/5.:   Time out.");
		System.exit(-1);
	    }
	};
	watchdog.start();

	Thread me = Thread.currentThread();
	me.interrupt();
	synchronized(me) {
	    try {
		me.wait(4000);
		System.out.println("Failure 4.");
	    } catch (InterruptedException e) {
		System.out.println("Success 4.");
	    }

	    me.interrupt();
	    try {
		Thread.sleep(4000);
		System.out.println("Failure 5.");
	    } catch (InterruptedException e) {
		System.out.println("Success 5.");
	    }
	}
	System.exit(0);
    }

    static void ssij(Thread t) {
	t.start();
	try {
	    Thread.sleep(1000);
	    t.interrupt();
	    t.join();
	} catch (InterruptedException e) { 
	    System.out.println("caught " + e);
	}
    }
}