aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Tests for the Data class/locking mechanism Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Tests for the Data class/locking mechanism" Watch "Tests for the Data class/locking mechanism" New topic
Author

Tests for the Data class/locking mechanism

Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Howdy y'all, what y'all doin'?!

Guys, here are the tests I created to test the main functionalities of my implementation of the Data class and the locking mechanism. It also contains a header I created for each class. Some customization of this code will be necessary, according to the implementation of each developer. For instance, my project was the URLyBird 1.3.1, and my lock method does not return any value, and there are some cases where the lock method returns a long value, so this would be a case of customization. Throughout the code, you guys will notice that I used a class called Room and a class called RoomRetriever. The Room class is a representation of a database record, and the RoomRetriever class has 2 methods: one to transform a String array to a Room object, and a Room object to a String array.
This test is supposed to be executed in less than 1 second, so if it doesn't finish in less than 1 second, then the deadlock occurred. It is a very simple and objective test, nothing fancy, but it really helped me getting 80/80 for my locking mechanism and 40/40 for my data storage implementation.
I really hope this helps everyone still working on the assignment!!!



Cheers, Bob "John Lennon" Perillo
SCJP, SCWCD, SCJD, SCBCD - Daileon: A Tool for Enabling Domain Annotations
Mahesh Kumaraguru
Ranch Hand

Joined: Jun 01, 2005
Posts: 95
Roberto Perillo wrote:

I really hope this helps everyone still working on the assignment!!!




Hi Roberto Perillo,

The above two statements seem to contradict each other.



Am I missing something?
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

It just says that this class should only be used in the context of the evaluation of the assignment, and used only by Sun.
This is the header I used in the classes of the assignment, and I just took the opportunity to show it here, and suggest to people still working on the assignment how the header of the classes could be.
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

Nice testing class

I will certainly use them and pray to finish it in less than 1 second


SCJA, SCJP (1.4 | 5.0 | 6.0), SCJD
http://www.javaroe.be/
Payal Shah
Ranch Hand

Joined: Jul 10, 2006
Posts: 67
Thanks Roberto for posting it. I will test it out..
Payal Shah
Ranch Hand

Joined: Jul 10, 2006
Posts: 67
mine runs forever. I guess i have deadlock.. not sure..I am using Eclipse to run the test.

Is it possible that the test is done but main thread is running. do i need to set that as daemon thread.

if i do have deadlock,, how would go for that..


Thanks

Payal Shah
Ranch Hand

Joined: Jul 10, 2006
Posts: 67
Also I notice this comment in the startTests method..

* Practically, it is not necessary to execute this loop more than 1
* time, but if you want, you can increase the controller variable,
* so it is executed as many times as you want


When i changed the loop to run only one instead of 10 times, I exit out in less than 1 sec.


But when i run it i<10, i am stuck..
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

Hi Payal,

I would guess you're having a problem in your locking section if it's running for ever. it seems you've deadlock.

Because i just finished the reading/writing to the db, so no locking yet (so no use of calling this test). but i would investigate it a little bit more.

and to be really sure your locking/synchronization code is ok, i would even increment the loop-number to 10000. because if your code deadlocks, you'll get a 0 for locking (i assume) and it would be very difficult to pass the exam

Good luck,
Roel
Payal Shah
Ranch Hand

Joined: Jul 10, 2006
Posts: 67
thank you.

I am very confuse

I just ran it again with the loop to run for 10. It works. It is very weird.. It works some time..I have tried with various loop count( 2,6,8,10) it work some times.

I am not sure..I am getting little nervous and scare now..

Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

and if you try with 100, 500 or 1000
Payal Shah
Ranch Hand

Joined: Jul 10, 2006
Posts: 67
Hi Roberto,

this is what i found. When i run your test case it work some times but then I notice that your unlock method was not in finally section. If i move it to finally section, it works and I can run 1000 to 2000 threads..

Let me know what you think about that..this is what i did where i see unlock method.

Jeffry Kristianto Yanuar
Ranch Hand

Joined: Oct 01, 2007
Posts: 759
Hi Roberto, your testing method is better and smarter than mine.

I created some kind of "brute-force" test method that create 500 threads that access (Create Read Update Delete) the same record number at the same time to find out if my locking mechanism would not break.


Jeffry Kristianto Yanuar (Java Instructor) SCJP 5.0, SCJA, SCJD (UrlyBird 1.3.2)
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Hey, Jeffry! Long time no talk! I really hope you're doin' well.

Payal, I believe the problem that was happening in your case was that the record was being locked, but the unlock instruction was not being reached, so I believe some Exception was occurring in the update/delete methods (since it worked when you moved the unlock instruction to a finally block). Please verify if this was the case.
Payal Shah
Ranch Hand

Joined: Jul 10, 2006
Posts: 67
Hi Roberto

that was the case. by putting unlock in the finally section i make sure that record release the lock incase where there is an excpetion. I did the same thing when i did my test cases.

So is that ok if I put your unlock in finally section? I just want to make sure i do not have deadlock..

Thank you so much for sharing your test case..

regards.

Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Oh, absolutely. In fact, when I call these methods from my services layer, I do it inside a try/finally block. The important thing to remember is that, if Thread A tries to lock a record that was previously locked, then Thread A has to block (wait()). Also, you have to make sure that your lock method is able to work properly when a client tries to lock a record that is already locked, and when the lock is released, the record was deleted.
Payal Shah
Ranch Hand

Joined: Jul 10, 2006
Posts: 67
that's what i do.

thank you..

Regards,
david boureau
Greenhorn

Joined: Oct 01, 2008
Posts: 17
Hi Everybody, Thanks to Roberto for this interesting post.



Just to be sure : you don't handle the deadlock on the server side, don't you? If, on the client side, the client crashes (shut down for example) right after having call the lock method, which is equivalent in your test to comment the data.unlock as shown above. This implies that any other client that call the lock method on this record is lock indefinetly,
am I right?

best regards,

david.


SCJP 5, SCWCD "5", SCBCD 5, SCJD with java SE 6, SCDJWS 5

Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Hey, partner!

Well, this is something to be handled in each particular implementation. For instance, depending on your implementation, you'll have to put the call to the unlock method in a finally block. In my case, when performing these tests, I didn't have to because I use memory caching, so an exception can never occur when calling the update method, only when calling the lock method. Now, you can choose to handle the deadlock with some monitoring mechanism that verifies how long a record has been locked. In my case, I didn't implement such mechanism, because it isn't really required, but you can certainly implement one. The tests I suggested are very generic, so they should work with different implementations.
Maksym Odintsov
Greenhorn

Joined: Jan 22, 2009
Posts: 11
Hi guys. How do you think is it ok if I run the test with the loop for (int i = 0; i < 1000; i++) and it is working for about 2 minutes, or is it too long ?
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

Maksym,

If your program finishes, it's ok. if it doesn't, you have deadlock.

you can simulate a deadlock situation, by uncommenting both lines containing in UpdatingRecord1Thread and DeletingRecord1Thread. It will deadlock for sure, even if your counter of the loop is set to 1 instead of 1000.

Kind regards,
Roel
Matheus Mendes
Ranch Hand

Joined: May 15, 2007
Posts: 66
What should be the output guys ?

I ran it and only got 2 exceptions caused by recordNotFound, I think my lock systems is good = )


About locks, I try to get a thin client ... I'm using lock and unlock on server side, so if a client crashes there is not a problem because the operation will be done.

best regards and thanks again Perillo for the support and share this tool.


The Death of one is a tragedy, but the Death of a million is just a statistic. Joseph Stalin

SCJP 6.0, SCJD
Roel De Nijs
Bartender

Joined: Jul 19, 2004
Posts: 5216
    
  12

Matheus Mendes wrote:What should be the output guys ?


There is no expected output. The program hangs (deadlock) or runs fine (no deadlock). As easy as that.

And you can simulate the deadlock situation by commenting the lines where you unlock the record in UpdatingRecord1Thread and DeletingRecord1Thread

Kind regards,
Roel
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Fala ai Matheus!

So, the output should be the output of these tests (i.e. "88 trying to lock record #1 on UpdatingRecord1Thread") + the output of your Data class (so, technically, there's no final output, like, "Passed =)" or "Failed =("). It is normal to eventually get a RecordNotFoundException when running these tests. For instance, the UpdatingRandomRecordThread tries to update a record whose number may not exist, so your lock method should indeed throw a RecordNotFoundException.
Matheus Mendes
Ranch Hand

Joined: May 15, 2007
Posts: 66
Well what figures the dead lock ?

When I ran it on eclipse, the console shows the outputs but the program doesn't get terminated status, I must click on stop button, is there a deadlock ?

thanks
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Hummm... apparently, yes
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

Hi all, it's time for me to do some real testing with my project. I physically try testing (not using threading) by loading up 2 clients on different computers and simultaneously clicking the book button, etc.

I used a service layer to interact with the Data class. So the GUI test really doing the service layer methods.

According to my GUI test I got, given A and B represent remote clients:

Search Action
A then B = A gets results then B gets results
B then A = B gets results then A gets results
A and B simultaneously with same parameters = A and B both get respective results
A and B simultaneously with different parameters = A and B both get respective results

Book Action (my book method checks if there is a duplicate customer ID already used before from a set)
A then B = A updates then B updates
B then A = B updates then A updates
A and B simultaneously same record same ID = A updates then B gets customer ID already used
A and B simultaneously same record different ID = A updates then B gets room already booked
A and B simultaneously different record same ID = A updates then B gets customer ID already used
A and B simultaneously different record different ID = A updates then B updates

Check out Action
A then B = A updates then B updates
B then A = B updates then A updates
A and B simultaneously same record = A updates then B gets already checked out
A and B simultaneously different record = A updates then B updates

Delete Action
A then B = A deletes then B deletes
B then A = B deletes then A deletes
A and B simultaneously same record = A deletes then B gets already deleted
A and B simultaneously different record = A deletes then B deletes

Insert Action (I uses a Room object when client gets alredy created, it's really mean the Room object's equals method returns true - name+location+size are all same will return true)
A then B = A inserts then B inserts
B then A = B inserts then A inserts
A and B simultaneously same room = A inserts then B gets already created
A and B simultaneously different room = A inserts then B inserts


If A and B doing different methods eg A insert and B book - the expected results are returned such as A inserts then B updates or B updates then A inserts.

I shall use this test to do further testing cos my GUI test only using 2 threads.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 175
Is this a staic test that you do in this order K?

Bob Perillo's test case scenario fires up the threads andf then they run randomly. I found a lot of errors (mainly wrapping exceptions)
using his scenario and adapted this for my assignment. Just be sure that your wrapping, when not critical, allows the application
to continue and advise the user of the problem. Maybe use an alert window and allow the user to continue.


Java5 Tutorials | Java6 Tutorials | HTML/CSS Tutorials | JavaScript and jQuery Tutorials
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Hey, Kevin!

I found a lot of errors (mainly wrapping exceptions) using his scenario and adapted this for my assignment.


Just a curiosity, what were the errors you found? Most likely you found them due to the differences between the interfaces provided by Sun.
Matheus Mendes
Ranch Hand

Joined: May 15, 2007
Posts: 66
I found my deadlock problem. ... I was trying to use te conception of thin client but I made a mistake when I put my lock and unlock methods in the class's update method. So I fix it and the test runs without problems.

I got some RecNotFound exceptions but it was foreseen......

Thanks again perillo and all folks....
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

I just tested my Data class using a test class similar to Rob's version. And I get major problems. So back to locking.

The good thing is I at least know where the problem is cos when the program just about to hang (deadlock) it prints out the parms I keep track of read/write locks.

Kevin, you can say my GUI test is somewhat static. Loading 2 computers next to each other clicking same time may not be the best test but at least I try to simulate the concurrency. Of course using Threads is better which is what I'm testing now LOL.
Matheus Mendes
Ranch Hand

Joined: May 15, 2007
Posts: 66
Before use this test class I wrote one that create rmi connections and use the data, was a good experience too.


I'm about to send my project but I must get the the cash to buy the other voucher...

Thanks guys... good lucky all
K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2379
    
    7

Hi Rob further thought to your data test class. I see that in each thread you simply call lock/update/unlock as is without synchronization block inside the thread's run method.

Now suppose the lock/update/unlock in the Data class are all synchronized. After Thread1 calls lock and Thread2 comes along and calls lock. Now ideally people do this to ensure atomicity stuff:


Without synchronization inside run, what happens if Thread2 gets the object's lock flag from Thread1 after Thread1 finish calling lock before update. It looks to me this will deadlock even thought Data class lock/update/unlock methods are synchronized. Am I right?

Another way of asking is will Thread1 lose its object lock flag in between synchronized method calls, if Thread2 comes in and calls lock method?
Kevin Florish
Ranch Hand

Joined: Jan 06, 2009
Posts: 175
Hi K

There is no need to wrap the lock/process/unlock within a synchronized try block if you are utilizing a locking map.

The first thread to hit the lock() method will add it to the locking map and therefore block all other threads until the record is
removed. The process (update/delete etc) is then processed and then the unlock() method called which signals awaiting threads.
It's a design decision but I just Synched on the file when updating it, all other processes were done using a cache map.

And for Robert Perillo:


Just a curiosity, what were the errors you found? Most likely you found them due to the differences between the interfaces provided by Sun.


Yes the errors were only errors for me as I had a different interface from Sun
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11431
    
  85

Hey Roberto

I meant to ask you some time ago - do you feel like adding a link to this in the JavaRanch SCJD FAQ? (The FAQ is just a wiki page - you can edit it yourself).

Regards, Andrew


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Howdy, Andrew!

do you feel like adding a link to this in the JavaRanch SCJD FAQ?


Certainly! And... done!
Jari Timonen
Ranch Hand

Joined: Jan 16, 2004
Posts: 230
Thanks for the test. Everything went fine until i noticed one thing:

lock
delete
unlock //Exception since record already deleted

AND my assingment says:

Any methods that throw RecordNotFoundException should do so if a specified record does not exist or is marked as deleted in the database file.



How did tackle this issue? (there is a word should, so it's not mandatory!)

puuh. already doing my submission package... some final tests



Oh: Date's give funny year


SCJP 5.0, SCJD URLyBird 1.3.3, OCMJEA 5 (SCEA) Factory Homes
Christian Kindler
Greenhorn

Joined: Apr 16, 2009
Posts: 18
A helpful thing when having deadlocks is: calling "kill -3 PID" on the command line, where "PID" is the process id of your java process. This will give you a detailed dump where you can see at which point your code is waiting for acquiring a lock. This works on unix systems, I don't know if you have a similar possibility on windows.
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Hi Roberto
I customised your test class for my URLybird 1.2.3 and it executed less than 1 second and finished well.
so....It's mean that not occur dead lock ?
I'm realy lost in this massive logs...loll

Fernando Franzini - Java Blog
Roberto Perillo
Bartender

Joined: Dec 28, 2007
Posts: 2265
    
    3

Fala nobre Fernando!

Well, the idea of the locking mechanism is to make sure that your application is able to work concurrently properly. It really depends on your particular implementation. But I'd say that, if you used the wait()/notifyAll() schema, then no deadlocks occurred!

In the UpdatingRecord1Thread, try to comment the data.unlock(1); instruction to see if you get a deadlock.
Fernando Franzini
Ranch Hand

Joined: Jan 09, 2009
Posts: 486
    
    2

Fala grande Roberto.

I tested and the dead lock really happened when I comment unlock method...
Thanks for the class, tips and help.
Best Regards.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Tests for the Data class/locking mechanism