*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Testing Harness Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Testing Harness" Watch "Testing Harness" New topic
Author

Testing Harness

GD Deepz
Ranch Hand

Joined: Sep 29, 2004
Posts: 55
Does anybody have a good test harness to test the lock/unlock sequence. What excatly are we looking for in the test? Is it that for every thread that goes into a wait mode, it must eventually get a lock otherwise its deadlocked. I wrote a simple test harness. There are five threads but some of the threads are getting deadlocked meaning, they never acquire a lock.

public class LockTesting extends Thread {
private static Data data1;
private static Data data2;

public static void main(String[] args) {
data1 = new Data(); //Is two data Data objects valid

// data2 = new Data();
LockTesting test1 = new LockTesting();
LockTesting test2 = new LockTesting();

LockTesting test3 = new LockTesting();

LockTesting test4 = new LockTesting();
LockTesting test5 = new LockTesting();

test1.start();
test2.start();

test3.start();

test4.start();
test5.start();


/* (non-Javadoc)
* @see java.lang.Runnable#run()
*/
public void run() {
long cookie1 = data1.lockRecord(1);
data1.updateRecord(1, null, cookie1);

}
}
Anton Golovin
Ranch Hand

Joined: Jul 02, 2004
Posts: 476
Hi, GD! In the run method, the last line should be the unlock method. What is happening now is that some records never get unlocked, and the threads waiting on them wait forever.


Anton Golovin (anton.golovin@gmail.com) SCJP, SCJD, SCBCD, SCWCD, OCEJWSD, SCEA/OCMJEA [JEE certs from Sun/Oracle]
GD Deepz
Ranch Hand

Joined: Sep 29, 2004
Posts: 55
Hi Anton, tks for your reply. In fact I do have a unlock method that is invoked as such:

updataRecord(long recNo, String[] data, long lockCookie){
try { dao.updateDatabase(contractor); //persist to database
} finally {
unlock(recNo, lockCookie); // this unlocks the record
}
}

It goes like this; lock/updateRecord/unlock

Not sure why it is caught in a waiting mode.
Mike Vess
Ranch Hand

Joined: Aug 25, 2004
Posts: 41
First of all i think you should update you test-program so that every thread do several updates of the record not just one.



Why are you calling unlock in the updateRecord method?

Now to your question of why some threads are deadlocked. The problem must be in the lock/unlock methods. These methods should be synchronized in some way and whenever you unlock a record you must notify other threads that may be waiting in the lock method.


What you thought was right today may need a refactoring tomorrow...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Testing Harness
 
Similar Threads
Testing - Max's Book
Help needed JVM crashes while running the following program
Testing
bookFlight
Preliminary test fails! Threads problem