Your server must be capable of handling multiple concurrent requests, and as part of this capability, must provide locking functionality as specified in the interface provided above. You may assume that at any moment, at most one program is accessing the database file; therefore your locking system only needs to be concerned with multiple concurrent clients of your server. Any attempt to lock a resource that is already locked should cause the current thread to give up the CPU, consuming no CPU cycles until the desired resource becomes available
To test this, I instantiate the class Data:
Then I instantiate a class (DBRead) that is derived from class Thread that has the following run method. During instantiation, I specify the Thread's name and what record number I want to read:
Then I instantiate another class (DBCheck) that is derived from class Thread that has the following run method:
I put both these classes in a loop:
Then start 'em up:
If I create enough threads I see records initially locked, then eventually these records get read.
Is this a valid way to simulate many "nearly-concurrent" requests to the database for the same record, in order to validate the record locking algorithm?
Any comments are welcomed.
The best thing you can do is prove to yourself logically that your locking code is correct. Or even better: have some person who is not involved in this assignment review your lock and unlock methods and prove logically that they work. The testing is then just icing on the cake.
The main things I recommend you look for:
But to continue with the idea of testing ...
One of the tests that was regularly recommended in the Fly By Night Services assignment was decrementing the number of seats in a particular flight. If there are 89 seats available on a flight, and we start 10 threads, each removing 3 seats from the flight, then at the end of the run there should be 2 seats remaining, and one thread complaining that it could not book the three seats. Any other result and there was guaranteed to be a problem somewhere
You could do something similar with your assignment (even if it is one of the new assignments you can just use the customer field as a temporary holder of numbers).