|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun * Microsystems, Inc. All Rights Reserved. */ package org.openide.util; import java.lang.ref.*; import java.util.*; import org.openide.ErrorManager; import junit.framework.*; import org.netbeans.junit.*; public class MutexTest extends NbTestCase { private Mutex.Privileged p; private Mutex m; public MutexTest(java.lang.String testName) { super(testName); } public static void main(java.lang.String[] args) { junit.textui.TestRunner.run(suite()); } public static Test suite() { NbTestSuite suite = new NbTestSuite(MutexTest.class); return suite; } /** Sets up the test. */ protected void setUp () { p = new Mutex.Privileged (); m = new Mutex (p); } public void testReadWriteRead() throws Exception { final Object lock = new Object(); final Mutex.Privileged mPriv = new Mutex.Privileged(); final Mutex m = new Mutex( mPriv ); synchronized ( lock ) { mPriv.enterReadAccess(); new Thread() { public void run () { synchronized( lock ) { lock.notifyAll(); } mPriv.enterWriteAccess(); synchronized ( lock ) { lock.notifyAll(); mPriv.exitWriteAccess(); } } }.start(); lock.wait(); } Thread.sleep (100); mPriv.enterReadAccess(); mPriv.exitReadAccess(); synchronized ( lock ) { mPriv.exitReadAccess(); lock.wait(); } } /** Simple test to execute read access and write access imediatelly. */ public void testPostImmediatelly () { State s = new State (); m.postReadRequest(s); if (s.state != 1) { fail ("Read request not started immediatelly"); } m.postWriteRequest (s); if (s.state != 2) { fail ("Write request not started immediately"); } } /** Behaviour of postWriteRequest is defined by this test. */ public void testPostWriteRequest () { State s = new State (); // first enter p.enterWriteAccess (); p.enterReadAccess (); m.postWriteRequest(s); if (s.state != 0) { fail ("Write request started when we are in read access"); } p.exitReadAccess (); if (s.state != 1) { fail ("Write request not run when leaving read access: " + s.state); } // exiting p.exitWriteAccess (); if (s.state != 1) { fail ("Run more times?: " + s.state); } } /** Behaviour of postReadRequest is defined by this test. */ public void testPostReadRequest () { State s = new State (); // first enter p.enterWriteAccess (); m.postReadRequest(s); if (s.state != 0) { fail ("Read request started when we are in write access"); } p.exitWriteAccess (); if (s.state != 1) { fail ("Read request not run when leaving write access: " + s.state); } if (s.state != 1) { fail ("Run more times?: " + s.state); } } /** Test enter from S mode to X mode * public void testXtoS() { State s = new State (); p.enterReadAccess (); p.enterWriteAccess (); s.run(); p.exitWriteAccess(); p.exitReadAccess(); if (s.state != 1) { fail ("Run more times?: " + s.state); } } */ /** Tests posting write and read requests while the Mutex is held * in X mode and was entered in S mode as well */ public void testPostWriteReadRequests() { State s = new State (); // first enter p.enterWriteAccess (); p.enterReadAccess (); m.postWriteRequest(s); if (s.state != 0) { fail ("Write request started when we are in read access"); } m.postReadRequest(s); if (s.state != 0) { fail ("Read request started when we are in write access"); } p.exitReadAccess (); if (s.state != 1) { fail ("Write request not run when leaving read access: " + s.state); } // exiting p.exitWriteAccess (); if (s.state != 2) { fail ("Read request not run when leaving write access: " + s.state); } consistencyCheck(); } /** Tests simple postWriteRequest */ public void testSimplePostWriteRequest() { State s = new State (); m.postWriteRequest(s); if (s.state != 1) { fail ("Write request not run: " + s.state); } consistencyCheck(); } /** Tests simple postReadRequest */ public void testSimplePostReadRequest() { State s = new State (); m.postReadRequest(s); if (s.state != 1) { fail ("Read request not run: " + s.state); } consistencyCheck(); } // starts a new thread, after return the thread will hold lock "p" in // mode X for timeout milliseconds private static void asyncEnter(final Mutex.Privileged p, final boolean X, final long timeout) throws InterruptedException { asyncEnter(p, X, timeout, null); } // starts a new thread, after return the thread will hold lock "p" in // mode X for timeout milliseconds, the new thread execs "run" first private static void asyncEnter(final Mutex.Privileged p, final boolean X, final long timeout, final Runnable run) throws InterruptedException { final Object lock = new Object(); synchronized (lock) { new Thread(new Runnable() { public void run() { if (X) { p.enterWriteAccess(); } else { p.enterReadAccess(); } synchronized (lock) { lock.notify(); } if (run != null) { run.run(); } try { Thread.sleep(timeout); } catch (InterruptedException e) { e.printStackTrace(); } if (X) { p.exitWriteAccess(); } else { p.exitReadAccess(); } } }).start(); lock.wait(); } } /** Tests enterWriteAccess while the Mutex is contended in X mode by * another thread */ public void testXContendedX() throws InterruptedException { asyncEnter(p, true, 2000); // first enter p.enterWriteAccess(); p.exitWriteAccess(); consistencyCheck(); } /** Tests enterReadAccess while the Mutex is contended in X mode by * another thread */ public void testXContendedS() throws InterruptedException { asyncEnter(p, true, 2000); // first enter p.enterReadAccess(); p.exitReadAccess(); consistencyCheck(); } /** Tests enterWriteAccess while the Mutex is contended in S mode by * another thread */ public void testSContendedX() throws InterruptedException { asyncEnter(p, false, 2000); // first enter p.enterWriteAccess(); p.exitWriteAccess(); consistencyCheck(); } /** Tests enterReadAccess while the Mutex is contended in S mode by * another thread */ public void testSContendedS() throws InterruptedException { asyncEnter(p, false, 2000); // first enter p.enterReadAccess(); p.exitReadAccess(); consistencyCheck(); } /** Tests postWriteRequest while the Mutex is contended in X mode by * another thread */ public void testXContendedPx() throws InterruptedException { asyncEnter(p, true, 2000); State s = new State (); m.postWriteRequest(s); if (s.state != 1) { fail ("Write request not run: " + s.state); } consistencyCheck(); } /** Tests postReadRequest while the Mutex is contended in X mode by * another thread */ public void testXContendedPs() throws InterruptedException { asyncEnter(p, true, 2000); State s = new State (); m.postReadRequest(s); if (s.state != 1) { fail ("Read request not run: " + s.state); } consistencyCheck(); } /** Tests postWriteRequest while the Mutex is contended in S mode by * another thread */ public void testSContendedPx() throws InterruptedException { asyncEnter(p, false, 2000); State s = new State (); m.postWriteRequest(s); if (s.state != 1) { fail ("Write request not run: " + s.state); } consistencyCheck(); } /** Tests postReadRequest while the Mutex is contended in S mode by * another thread */ public void testSContendedPs() throws InterruptedException { asyncEnter(p, false, 2000); State s = new State (); m.postReadRequest(s); if (s.state != 1) { fail ("Write request not run: " + s.state); } consistencyCheck(); } /** Tests postWriteRequest and postReadRequest while the Mutex is contended in S mode by * another thread as well as this thread. */ public void testSContendedSPsPx() throws InterruptedException { asyncEnter(p, false, 2000); State s = new State (); p.enterReadAccess(); m.postReadRequest(s); if (s.state != 1) { fail ("Read request not run: " + s.state); } m.postWriteRequest(s); if (s.state != 1) { fail ("Write request run: " + s.state); } p.exitReadAccess(); if (s.state != 2) { fail ("Write request not run: " + s.state); } consistencyCheck(); } /** The Mutex is held in S mode by a thread which also posted a * write request. Another thread tries enterWriteAccess. */ public void testSPxContendedX() throws Exception { final State s = new State (); asyncEnter(p, false, 2000, new Runnable() { public void run() { try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } m.postWriteRequest(s); if (s.state == 1) { fail ("Write request run: " + s.state); } } }); p.enterWriteAccess(); if (s.state != 1) { fail ("Write request not run: " + s.state); } p.exitWriteAccess(); consistencyCheck(); } /** * Test case for #16577. Grab X,S and post X request, * the second thread waits for X, causing the mutex to be * in CHAINED. */ public void testXSPxContendedX() throws Exception { final State s = new State (); asyncEnter(p, true, 2000, new Runnable() { public void run() { p.enterReadAccess(); try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } m.postWriteRequest(s); p.exitReadAccess(); if (s.state != 1) { fail ("Write request not run: " + s.state); } } }); p.enterWriteAccess(); p.exitWriteAccess(); consistencyCheck(); } /** * Grab X and post S request, * the second thread waits for X, causing the mutex to be * in CHAINED. */ public void testXPsContendedX() throws Exception { final State s = new State (); asyncEnter(p, true, 2000, new Runnable() { public void run() { try { Thread.sleep(1000); } catch (Exception e) { e.printStackTrace(); } m.postReadRequest(s); if (s.state == 1) { fail ("Read request run: " + s.state); } } }); p.enterWriteAccess(); Thread.sleep(4000); p.exitWriteAccess(); consistencyCheck(); } /** Checks the Mutex is in the consistent state, i.e. enterWriteAccess must pass */ private void consistencyCheck() { p.enterWriteAccess(); p.exitWriteAccess(); } private static class State implements Runnable { public int state; public void run () { state++; } } // end of State } |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.