Author: mturk
Date: Tue Apr 19 18:29:15 2011
New Revision: 1095158

URL: http://svn.apache.org/viewvc?rev=1095158&view=rev
Log:
Use semaphore for syncing test parent/child

Modified:
    
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMutex.java

Modified: 
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMutex.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMutex.java?rev=1095158&r1=1095157&r2=1095158&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMutex.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestMutex.java
 Tue Apr 19 18:29:15 2011
@@ -24,6 +24,7 @@ import org.testng.Assert;
 public class TestMutex extends Assert
 {
 
+    private static final String semname = "acrSemop23";
     private static final String mtxname = "acrMutex23";
 
     @Test(groups = { "mutex.parent" })
@@ -44,16 +45,17 @@ public class TestMutex extends Assert
     public void createMutex()
         throws Exception
     {
-        Mutex s = Mutex.create(mtxname);
+        Semaphore s = Semaphore.create(semname, 0);
         assertNotNull(s);
+        Mutex m = Mutex.create(mtxname);
+        assertNotNull(m);
         System.out.println("[parent] Waiting for a child to attach");
         System.out.flush();
+        m.acquire();
         s.acquire();
-        Thread.sleep(2000);
-        System.out.println("[parent] Done.");
-        System.out.flush();
-        s.release();
-        Thread.sleep(100);
+        
+        m.release();
+        m.close();
         s.close();
     }
 
@@ -63,11 +65,11 @@ public class TestMutex extends Assert
     {
         System.out.println("[child]  Attaching child mutex");
         System.out.flush();
-        Mutex s = null;
+        Semaphore s = null;
         int step = 125;
         while (step <= 2000) {
             try {
-                s = Mutex.open(mtxname);
+                s = Semaphore.open(semname);
                 break;
             } catch (Exception x) {
 
@@ -76,16 +78,14 @@ public class TestMutex extends Assert
             step *= 2;
         }
         assertNotNull(s);
-        System.out.println("[child]  Mutex opened.");
-        System.out.flush();
-        s.acquire();
-        System.out.println("[child]  Mutex acquired.");
-        System.out.flush();
-        s.release();
-        s.close();
+        Mutex m = Mutex.open(mtxname);
+        assertNotNull(m);
+        // Mutex is held by parent
+        assertFalse(m.tryAcquire());
+        m.close();
         System.out.println("[child]  Done.");
         System.out.flush();
-        s = Mutex.open(mtxname);
+        s.release();
         s.close();
     }
 


Reply via email to