dd01848dd8d33968dcac39afbfe3bb794c132968
[debian/openrocket] / test / net / sf / openrocket / util / TestMutex.java
1 package net.sf.openrocket.util;
2
3 import static org.junit.Assert.*;
4
5 import org.junit.Test;
6
7 public class TestMutex {
8         
9         @Test
10         public void testSingleLocking() {
11                 SafetyMutex m = new SafetyMutex();
12                 
13                 // Test single locking
14                 assertNull(m.lockingThread);
15                 m.verify();
16                 m.lock("here");
17                 assertNotNull(m.lockingThread);
18                 assertTrue(m.unlock("here"));
19                 
20         }
21         
22         @Test
23         public void testDoubleLocking() {
24                 SafetyMutex m = new SafetyMutex();
25                 
26                 // Test double locking
27                 m.verify();
28                 m.lock("foobar");
29                 m.verify();
30                 m.lock("bazqux");
31                 m.verify();
32                 assertTrue(m.unlock("bazqux"));
33                 m.verify();
34                 assertTrue(m.unlock("foobar"));
35                 m.verify();
36         }
37         
38         @Test
39         public void testDoubleUnlocking() {
40                 SafetyMutex m = new SafetyMutex();
41                 // Mark error reported to not init exception handler
42                 SafetyMutex.errorReported = true;
43                 
44                 m.lock("here");
45                 assertTrue(m.unlock("here"));
46                 assertFalse(m.unlock("here"));
47         }
48         
49         
50
51         private volatile int testState = 0;
52         private volatile String failure = null;
53         
54         @Test(timeout = 1000)
55         public void testThreadingErrors() {
56                 final SafetyMutex m = new SafetyMutex();
57                 
58                 // Initialize and start the thread
59                 Thread thread = new Thread() {
60                         @Override
61                         public void run() {
62                                 try {
63                                         
64                                         // Test locking a locked mutex
65                                         waitFor(1);
66                                         try {
67                                                 m.lock("in thread one");
68                                                 failure = "Succeeded in locking a mutex locked by a different thread";
69                                                 return;
70                                         } catch (ConcurrencyException e) {
71                                                 // OK
72                                         }
73                                         
74                                         // Test unlocking a mutex locked by a different thread
75                                         if (m.unlock("in thread two")) {
76                                                 failure = "Succeeded in unlocking a mutex locked by a different thread";
77                                                 return;
78                                         }
79                                         
80                                         // Test verifying a locked mutex that already has an error
81                                         try {
82                                                 m.verify();
83                                                 failure = "Succeeded in verifying a mutex locked by a different thread";
84                                                 return;
85                                         } catch (ConcurrencyException e) {
86                                                 // OK
87                                         }
88                                         
89                                         // Test locking a mutex after it's been unlocked
90                                         testState = 2;
91                                         waitFor(3);
92                                         m.lock("in thread three");
93                                         m.verify();
94                                         
95                                         // Wait for other side to test
96                                         testState = 4;
97                                         waitFor(5);
98                                         
99                                         // Exit code
100                                         testState = 6;
101                                         
102                                 } catch (Exception e) {
103                                         failure = "Exception occurred in thread: " + e;
104                                         return;
105                                 }
106                                 
107                         }
108                 };
109                 thread.setDaemon(true);
110                 thread.start();
111                 
112                 m.lock("one");
113                 testState = 1;
114                 
115                 waitFor(2);
116                 assertNull("Thread error: " + failure, failure);
117                 
118                 m.verify();
119                 m.unlock("one");
120                 testState = 3;
121                 
122                 waitFor(4);
123                 assertNull("Thread error: " + failure, failure);
124                 
125                 try {
126                         m.lock("two");
127                         fail("Succeeded in locking a locked mutex in main thread");
128                 } catch (ConcurrencyException e) {
129                         // OK
130                 }
131                 
132                 // Test unlocking a mutex locked by a different thread
133                 assertFalse(m.unlock("here"));
134                 
135                 try {
136                         m.verify();
137                         fail("Succeeded in verifying a locked mutex in main thread");
138                 } catch (ConcurrencyException e) {
139                         // OK
140                 }
141                 
142                 testState = 5;
143                 waitFor(6);
144                 assertNull("Thread error: " + failure, failure);
145         }
146         
147         private void waitFor(int state) {
148                 while (testState != state && failure == null) {
149                         try {
150                                 Thread.sleep(1);
151                         } catch (InterruptedException e) {
152                         }
153                 }
154         }
155         
156 }