1 19 20 package org.netbeans.api.registry; 21 22 import junit.textui.TestRunner; 23 import org.netbeans.junit.NbTestCase; 24 import org.netbeans.junit.NbTestSuite; 25 import org.openide.modules.ModuleInfo; 26 import org.openide.util.Lookup; 27 28 public class ThreadingTest extends NbTestCase { 29 private boolean ok = false; 30 31 public ThreadingTest(String name) { 32 super (name); 33 } 34 35 public static void main(String [] args) { 36 TestRunner.run(new NbTestSuite(ThreadingTest.class)); 37 } 38 39 protected void setUp () throws Exception { 40 Lookup.getDefault().lookup(ModuleInfo.class); 41 } 42 43 public void testThreading() throws Exception { 44 Context subctx = getContext().createSubcontext ("threading"); 45 Context subctx2 = subctx.createSubcontext ("hoy"); 46 47 ReadThread1 rt1 = new ReadThread1(); 53 rt1.start(); 54 55 try { 57 Thread.sleep(4000); 58 } catch(InterruptedException ie) { 59 assertTrue("Some error"+ ie.toString(), false); 60 } 61 62 assertTrue("Something failed", ok); 63 64 getContext().destroySubcontext("threading"); 65 } 66 67 protected Context getContext() { 68 return Context.getDefault(); 69 } 70 71 private class ReadThread1 extends Thread { 72 public void run() { 73 final Context ctx = Context.getDefault().getSubcontext("threading"); 74 ctx.getMutex().readAccess(new Runnable () { 75 public void run() { 76 77 WriteThread wt = new WriteThread(); 79 wt.start(); 80 81 try { 82 sleep(1500); 83 } catch(InterruptedException ie) { 84 assertTrue("Some error"+ ie.toString(), false); 85 } 86 87 Context c = ctx.getSubcontext("hoy"); 88 assertTrue("The 'hoy' context must exist.", c != null); 89 } 90 }); 91 } 92 } 93 94 private class WriteThread extends Thread { 95 public void run() { 96 final Context ctx = Context.getDefault(); 97 ctx.getMutex().writeAccess(new Runnable () { 98 public void run() { 99 100 ReadThread2 rt2 = new ReadThread2(); 102 rt2.start(); 103 104 try { 105 sleep(1500); 106 } catch(InterruptedException ie) { 107 assertTrue("Some error"+ ie.toString(), false); 108 } 109 110 try { 111 Context ctx2 = ctx.getSubcontext("threading"); 112 ctx2.destroySubcontext("hoy"); 113 } catch (ContextException ce) { 114 assertTrue("Cannot delete subcontext "+ ce.toString(), false); 115 } 116 } 117 }); 118 } 119 } 120 121 private class ReadThread2 extends Thread { 122 public void run() { 123 final Context ctx = Context.getDefault().getSubcontext("threading"); 124 ctx.getMutex().readAccess(new Runnable () { 125 public void run() { 126 Context c = ctx.getSubcontext("hoy"); 127 assertTrue("The 'hoy' context cannot exist.", c == null); 128 ThreadingTest.this.ok = true; 129 } 130 }); 131 } 132 } 133 134 135 } 136 | Popular Tags |