1 5 6 package com.hp.hpl.jena.rdf.model.test ; 7 import com.hp.hpl.jena.rdf.model.* ; 8 9 import junit.framework.*; 10 14 public class TestConcurrency extends TestSuite 15 { 16 17 18 static public TestSuite suite() { 19 return new TestConcurrency(); 20 } 21 22 static long SLEEP = 100 ; 24 static int threadCount = 0; 25 26 final static Model model1 = ModelFactory.createDefaultModel() ; 28 final static Model model2 = ModelFactory.createDefaultModel() ; 29 30 public TestConcurrency() 31 { 32 super("Model concurrency control") ; 33 34 if ( true ) 35 { 36 addTest(new Nesting("Lock nesting 1 - same model", 38 model1, ModelLock.READ, ModelLock.READ, false)) ; 39 addTest(new Nesting("Lock nesting 2 - same model", 40 model1, ModelLock.WRITE, ModelLock.WRITE, false)) ; 41 addTest(new Nesting("Lock nesting 3 - same model", 42 model1, ModelLock.READ, ModelLock.WRITE, true)) ; 43 addTest(new Nesting("Lock nesting 4 - same model", 44 model1, ModelLock.WRITE, ModelLock.READ, false)) ; 45 46 addTest(new Nesting("Lock nesting 1 - defifferent models", 48 model1, ModelLock.READ, model2, ModelLock.READ, false)) ; 49 addTest(new Nesting("Lock nesting 2 - defifferent models", 50 model1, ModelLock.WRITE, model2, ModelLock.WRITE, false)) ; 51 addTest(new Nesting("Lock nesting 3 - defifferent models", 52 model1, ModelLock.READ, model2, ModelLock.WRITE, false)) ; 53 addTest(new Nesting("Lock nesting 4 - defifferent models", 54 model1, ModelLock.WRITE, model2, ModelLock.READ, false)) ; 55 } 56 if ( true ) 57 { 58 addTest(new Parallel("Parallel concurrency test")) ; 60 } 61 62 } 63 64 static class Nesting extends TestCase 65 { 66 Model outerModel ; 67 Model innerModel ; 68 boolean outerLock ; 69 boolean innerLock ; 70 boolean exceptionExpected ; 71 72 Nesting(String testName, Model model, 74 boolean lock1, boolean lock2, boolean exExpected) 75 { 76 this(testName, model, lock1, model, lock2, exExpected) ; 77 } 78 79 Nesting(String testName, 81 Model model1, boolean lock1, 82 Model model2, boolean lock2, boolean exExpected) 83 { 84 super(testName); 85 outerModel = model1 ; 86 outerLock = lock1 ; 87 innerModel = model2 ; 88 innerLock = lock2 ; 89 exceptionExpected = exExpected ; 90 } 91 92 protected void runTest() throws Throwable 93 { 94 boolean gotException = false ; 95 try { 96 outerModel.enterCriticalSection(outerLock) ; 97 98 try { 99 try { 100 innerModel.enterCriticalSection(innerLock) ; 103 104 } finally { innerModel.leaveCriticalSection() ; } 105 } catch (Exception ex) 106 { 107 gotException = true ; 108 } 109 110 } finally { 111 outerModel.leaveCriticalSection() ; 112 } 113 114 if ( exceptionExpected ) 115 assertTrue("Failed to get expected lock promotion error", gotException) ; 116 else 117 assertTrue("Got unexpected lock promotion error", !gotException) ; 118 } 119 } 120 121 122 static class Parallel extends TestCase 123 { 124 int threadTotal = 10 ; 125 126 127 Parallel(String testName) 128 { 129 super(testName) ; 130 } 131 132 protected void runTest() throws Throwable 133 { 134 Model model = ModelFactory.createDefaultModel() ; 135 Thread threads[] = new Thread [threadTotal] ; 136 137 boolean getReadLock = ModelLock.READ ; 138 for (int i = 0; i < threadTotal; i++) 139 { 140 String nextId = "T"+Integer.toString(++threadCount); 141 threads[i] = new Operation(model, getReadLock) ; 142 threads[i].setName(nextId) ; 143 threads[i].start() ; 144 145 getReadLock = ! getReadLock ; 146 } 147 148 boolean problems = false ; 149 for ( int i = 0; i < threadTotal; i++) 150 { 151 try { threads[i].join(200*SLEEP) ; } catch (InterruptedException intEx) {} 152 } 153 154 for ( int i = 0; i < threadTotal; i++) 156 { 157 if ( threads[i].isAlive() ) 158 try { threads[i].join(200*SLEEP) ; } catch (InterruptedException intEx) {} 159 if ( threads[i].isAlive()) 160 { 161 System.out.println("Thread "+threads[i].getName()+" failed to finish") ; 162 problems = true ; 163 } 164 } 165 166 167 168 assertTrue("Some thread failed to finish", !problems) ; 169 } 170 171 class Operation extends Thread 172 { 173 Model model ; 174 boolean readLock ; 175 176 Operation(Model m, boolean withReadLock) 177 { 178 model = m ; readLock = withReadLock ; 179 } 180 181 public void run() 182 { 183 for ( int i = 0 ; i < 2 ; i++ ) 184 { 185 try { 186 model.enterCriticalSection(readLock) ; 187 if ( readLock ) 188 readOperation(false) ; 189 else 190 writeOperation(false) ; 191 } finally { model.leaveCriticalSection() ; } 192 } 193 } 194 } 195 197 volatile int writers = 0 ; 198 199 void doStuff(String label, boolean doThrow) 201 { 202 String id = Thread.currentThread().getName() ; 203 try { Thread.sleep(SLEEP) ; } catch (InterruptedException intEx){} 205 if ( doThrow ) 206 throw new RuntimeException (label) ; 207 } 208 209 211 public void readOperation(boolean doThrow) 212 { 213 if ( writers > 0 ) 214 System.err.println("Concurrency error: writers around!") ; 215 doStuff("read operation", false) ; 216 if ( writers > 0 ) 217 System.err.println("Concurrency error: writers around!") ; 218 } 219 220 public void writeOperation(boolean doThrow) 221 { 222 writers++ ; 223 doStuff("write operation", false) ; 224 writers-- ; 225 226 } 227 } 228 } 229 230 231 257 | Popular Tags |