0

Supposing that we've got a web app which manages some kind of devices in some domain.
Each device can be added only once, obviously -- the limited test resources.
There are several automating testers working on this app at the same time. Two testers might add the same device, in which case one tester will get exception in his/her automation tests.
how to avoid this kind of conflicts on such limited resources?

I've thought out the following methods, none seems satisfying:
1) Add some class with static Map<String device, String isAddedAlready> which stores all devices and their states -- works only for one tester.
2) Assign each tester some proprietary devices -- makes the limited resources more limited, and what if there were more testers than available devices?
-  -  -  -  -  -  -  -  -  -  -  - 
The confilcting situation is like this : Tester A and tester B both have a test method(Java code) that add a device, modify the device's name, and then delete the device. If A and B use the same IP at the same time, which is inevitable as the number of tests and testers grows, one test would fail.
  Since neither A nor B have the idea of which IP the other tester would use in his/her test method, it's sensible to set up a querying-and-locking-and-freeing service(a register center, a resource center?).
  The app stores data in a database(oracle, mysql or postgresql).
  Besides, it's possible that we run several apps at the same time, which makes the situation more complicated.

user2432405
  • 1,406
  • 2
  • 14
  • 29

2 Answers2

1

If it is only one resource, use a mutex lock and create a parallel process..

If it is multiple resources but you still want to distribute them , use a number of Semaphores. Before someone uses a resource, they need to lock on a semaphore and use it. Once they have used it they can give up the lock on the semaphore and some other thread will use the semaphore.

Also, In both of the cases above , if a tester cannot add a device it is better to encapsulate the resource in a singleton/ resource pool. In that way testers can "request" a resource from the resource pool instead of creating a new resource every time. It also saves time taken to "initialize" those resources

You can use multiple mechanisms if your resources are external and ,say, shared across environments . Mainly its either taking a lock on a file in the filesystem or lock on a row in the database when you are using an object.

For unix, you can take a lock on a file using lockfil which will be placed in a common dir. Processes will take a lock on the filenames before they start using the file

If you store the data in a database, you can also implement locking via stored proc or row level locking to ensure that only one external application can use a resource at a time

Biswajit_86
  • 3,661
  • 2
  • 22
  • 36
  • Thanks, Biswajit_86. Your suggestion just works for one JVM instance, I think. But we have several testers, so there would be serveral independent JVM instances. The real problem is : how to control the conflicts among different JVM instances? – user2432405 Apr 29 '14 at 06:50
  • @user2432405 I edited my original answer to refelct your scenario – Biswajit_86 Apr 29 '14 at 17:32
  • @Biswajit-86 : Is there any available java api for doing this work(I prefer to store data in a database)? It would take a lot of coding work if I do it from the beginning. – user2432405 Apr 30 '14 at 03:24
  • @Biswajit-86 : I'm a green hand in Java, it's a little complex for me. – user2432405 Apr 30 '14 at 03:35
  • @user2432405 : This is a bit long. I will reply using another answer – Biswajit_86 Apr 30 '14 at 03:41
1

Even I am unexperienced in handling this explicit scenario from java api. If you want to handle lock using only java api , please read this SO link How can I lock a file using java (if possible) . I have never used the nio package but I am sure there are many on this forum who have used it.

We rarely need to lock access across JVM's . When we do so , we need it on the database so we use the database layer in taking a lock.

However , your specific requirement will also come into play in this. How do your testers run this test ? is it manual/automated , is it part of the test or the entire test , what resource do you need to lock ( resource file, data file etc), what OS is your underlying serverside running on ?

Community
  • 1
  • 1
Biswajit_86
  • 3,661
  • 2
  • 22
  • 36