1   package net.sourceforge.domian.util.concurrent.locks;
2   
3   
4   import java.util.concurrent.Callable;
5   
6   import org.junit.Test;
7   
8   
9   public class SemaphoreSynchronizerTest {
10  
11      Synchronizer synchronizer = new SemaphoreSynchronizer();
12  
13      Synchronizer getSynchronizer() {
14          return this.synchronizer;
15      }
16  
17  
18      @Test(timeout = 100)
19      public void shouldNotDeadlockWhenInvokingAnConcurrentMethodFromAnotherConcurrentMethod() {
20          this.getSynchronizer().callConcurrently((Callable) new Callable<Boolean>() {
21              public Boolean call() {
22                  return concurrentMethod();
23              }
24          });
25      }
26  
27      @Test(timeout = 100)
28      public void shouldNotDeadlockWhenInvokingAnExclusiveMethodFromAConcurrentMethod() {
29          this.getSynchronizer().runConcurrently(new Runnable() {
30              public void run() {
31                  exclusiveMethod();
32              }
33          });
34      }
35  
36      @Test(timeout = 100)
37      public void shouldNotDeadlockWhenInvokingAConcurrentMethodFromAnExclusiveMethod() {
38          this.getSynchronizer().callExclusively((Callable) new Callable<Boolean>() {
39              public Boolean call() {
40                  return concurrentMethod();
41              }
42          });
43      }
44  
45      @Test(timeout = 100)
46      public void shouldNotDeadlockWhenInvokingAnExclusiveMethodFromAnotherExclusiveMethod() {
47          this.getSynchronizer().callExclusively((Callable) new Callable<Void>() {
48              public Void call() {
49                  exclusiveMethod();
50                  return null;
51              }
52          });
53      }
54  
55      Boolean concurrentMethod() {
56          return this.getSynchronizer().callConcurrently(new Callable<Boolean>() {
57              public Boolean call() {
58                  return true;
59              }
60          });
61      }
62  
63      Long exclusiveMethod() {
64          return this.getSynchronizer().callExclusively(new Callable<Long>() {
65              public Long call() {
66                  return 123L;
67              }
68          });
69      }
70  }