KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdf > model > test > TestConcurrency


1 /*
2  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package com.hp.hpl.jena.rdf.model.test ;
7 import com.hp.hpl.jena.rdf.model.* ;
8
9 import junit.framework.*;
10 /**
11  * @author Andy Seaborne
12  * @version $Id: TestConcurrency.java,v 1.6 2005/02/21 12:15:00 andy_seaborne Exp $
13  */

14 public class TestConcurrency extends TestSuite
15 {
16
17     /** Creates new RDQLTestSuite */
18         static public TestSuite suite() {
19             return new TestConcurrency();
20         }
21
22     // Test suite to exercise the locking
23
static long SLEEP = 100 ;
24     static int threadCount = 0;
25
26     // Note : reuse the model across tests.
27
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             // Same model: inner and outer
37
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             // Different model: inner and outer
47
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             // Crude test
59
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         // Same model
73
Nesting(String JavaDoc testName, Model model,
74                 boolean lock1, boolean lock2, boolean exExpected)
75         {
76             this(testName, model, lock1, model, lock2, exExpected) ;
77         }
78
79         // Potetnially different models
80
Nesting(String JavaDoc 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 JavaDoc
93         {
94             boolean gotException = false ;
95             try {
96                 outerModel.enterCriticalSection(outerLock) ;
97                 
98                 try {
99                     try {
100                         // Should fail if outerLock is READ and innerLock is WRITE
101
// and its on the same model, inner and outer.
102
innerModel.enterCriticalSection(innerLock) ;
103                         
104                     } finally { innerModel.leaveCriticalSection() ; }
105                 } catch (Exception JavaDoc 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 JavaDoc testName)
128         {
129             super(testName) ;
130         }
131         
132         protected void runTest() throws Throwable JavaDoc
133         {
134             Model model = ModelFactory.createDefaultModel() ;
135             Thread JavaDoc threads[] = new Thread JavaDoc[threadTotal] ;
136
137             boolean getReadLock = ModelLock.READ ;
138             for (int i = 0; i < threadTotal; i++)
139             {
140                 String JavaDoc 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 JavaDoc intEx) {}
152             }
153
154             // Try again for any we missed.
155
for ( int i = 0; i < threadTotal; i++)
156             {
157                 if ( threads[i].isAlive() )
158                     try { threads[i].join(200*SLEEP) ; } catch (InterruptedException JavaDoc 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 JavaDoc
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         // Operations ----------------------------------------------
196

197         volatile int writers = 0 ;
198     
199         // The example model operations
200
void doStuff(String JavaDoc label, boolean doThrow)
201         {
202             String JavaDoc id = Thread.currentThread().getName() ;
203             // Puase a while to cause other threads to (try to) enter the region.
204
try { Thread.sleep(SLEEP) ; } catch (InterruptedException JavaDoc intEx){}
205             if ( doThrow )
206                 throw new RuntimeException JavaDoc(label) ;
207         }
208     
209         // Example operations
210

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 /*
232  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
233  * All rights reserved.
234  *
235  * Redistribution and use in source and binary forms, with or without
236  * modification, are permitted provided that the following conditions
237  * are met:
238  * 1. Redistributions of source code must retain the above copyright
239  * notice, this list of conditions and the following disclaimer.
240  * 2. Redistributions in binary form must reproduce the above copyright
241  * notice, this list of conditions and the following disclaimer in the
242  * documentation and/or other materials provided with the distribution.
243  * 3. The name of the author may not be used to endorse or promote products
244  * derived from this software without specific prior written permission.
245  *
246  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
247  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
248  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
249  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
250  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
251  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
252  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
253  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
254  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
255  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
256  */

257
Popular Tags