You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2009/08/19 17:27:41 UTC

svn commit: r805837 - in /commons/sandbox/runtime/trunk/src: main/native/os/unix/ main/native/test/ test/org/apache/commons/runtime/

Author: mturk
Date: Wed Aug 19 15:27:40 2009
New Revision: 805837

URL: http://svn.apache.org/viewvc?rev=805837&view=rev
Log:
Add semaphore test

Added:
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSemaphore.java   (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c
    commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
    commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestChild.java

Modified: commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c?rev=805837&r1=805836&r2=805837&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/os/unix/psema.c Wed Aug 19 15:27:40 2009
@@ -81,8 +81,11 @@
     if (!name)
         sprintf(s->name, "/AcS.%06x%02x", ((unsigned int)getpid() & 0xFFFFFF),
                                           _sem_counter++);
-    else
-        strncpy(s->name, name, 63);
+    else {
+        if (*name != '/')
+            s->name[0] = '/';
+        strlcat(s->name, name, 62);
+    }
     do {
         s->sem = sem_open(s->name, O_CREAT | O_EXCL, 0644, value);
         if (s->sem == (sem_t *)SEM_FAILED) {
@@ -142,12 +145,16 @@
     s = ACR_Calloc(_E, THROW_FMARK, sizeof(acr_semaphore_t));
     if (!s)
         return -1;
+    if (*name != '/')
+        s->name[0] = '/';
+    strlcat(s->name, name, 62);
     s->sem = sem_open(s->name, O_RDWR);
     if (s->sem == (sem_t *)SEM_FAILED) {
         rc = ACR_GET_OS_ERROR();
         goto finally;
     }
-    s->locked = 0;
+    s->name[0] = '\0';
+    s->locked  = 0;
 finally:
     if (rc) {
         x_free(s);

Modified: commons/sandbox/runtime/trunk/src/main/native/test/testcase.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/test/testcase.c?rev=805837&r1=805836&r2=805837&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/test/testcase.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/test/testcase.c Wed Aug 19 15:27:40 2009
@@ -30,6 +30,7 @@
 #include "acr_xdr.h"
 #include "acr_shm.h"
 #include "acr_procmutex.h"
+#include "acr_semaphore.h"
 
 #if defined (WIN32)
 #include <io.h>
@@ -770,3 +771,37 @@
     return ACR_ProcMutexRelease(_E, m);
 }
 
+#if defined(WIN32)
+static const wchar_t *sem_name = L"TestSemaphore";
+#else
+static const char    *sem_name = "TestSemaphore";
+#endif
+
+ACR_JNI_EXPORT_DECLARE(jint, TestSemaphore, test001)(ACR_JNISTDARGS, jint v)
+{
+
+    return ACR_SemaphoreCreate(_E, sem_name, v);
+}
+
+ACR_JNI_EXPORT_DECLARE(jint, TestSemaphore, test002)(ACR_JNISTDARGS, jint d)
+{
+
+    return ACR_SemaphoreAttach(_E, sem_name);
+}
+
+ACR_JNI_EXPORT_DECLARE(jint, TestSemaphore, test003)(ACR_JNISTDARGS, jint s)
+{
+
+    return ACR_SemaphoreClose(_E, s);
+}
+
+ACR_JNI_EXPORT_DECLARE(jint, TestSemaphore, test004)(ACR_JNISTDARGS, jint s)
+{
+    return ACR_SemaphoreWait(_E, s);
+}
+
+ACR_JNI_EXPORT_DECLARE(jint, TestSemaphore, test005)(ACR_JNISTDARGS, jint s)
+{
+    return ACR_SemaphoreRelease(_E, s);
+}
+

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java?rev=805837&r1=805836&r2=805837&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestAll.java Wed Aug 19 15:27:40 2009
@@ -44,6 +44,7 @@
         suite.addTest(TestPrivate.suite());
         suite.addTest(TestFile.suite());
         suite.addTest(TestStrings.suite());
+        suite.addTest(TestSemaphore.suite());
         return suite;
     }
 

Modified: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestChild.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestChild.java?rev=805837&r1=805836&r2=805837&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestChild.java (original)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestChild.java Wed Aug 19 15:27:40 2009
@@ -34,6 +34,7 @@
         // Fundamentals
         suite.addTest(TestChildMain.suite());
         suite.addTest(TestOS.suite());
+        suite.addTest(TestSemaphore.suite());
         return suite;
     }
 

Added: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSemaphore.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSemaphore.java?rev=805837&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSemaphore.java (added)
+++ commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSemaphore.java Wed Aug 19 15:27:40 2009
@@ -0,0 +1,78 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime;
+
+import java.lang.System;
+import junit.framework.*;
+
+/**
+ * Semaphore Test.
+ *
+ */
+public class TestSemaphore extends TestCase
+{
+
+    private static native int      test001(int v);  // Create
+    private static native int      test002(int v);  // Attach
+    private static native int      test003(int v);  // Close
+    private static native int      test004(int v);  // Wait
+    private static native int      test005(int v);  // Release
+
+    public static Test suite() {
+        TestSuite suite = new TestSuite(TestSemaphore.class);
+        return suite;
+    }
+
+    protected void setUp()
+        throws Exception
+    {
+        System.loadLibrary("acr");
+    }
+
+    public void testTestSemaphoreCreate()
+        throws Throwable
+    {
+        boolean owner = true;
+        int s = -1;
+        System.out.println();
+        try {
+            s = test002(0);
+            System.out.println("Semaphore attached");
+            owner = false;
+        } catch (Exception e) {
+            // Ignore
+        }
+        if (owner) {
+            s = test001(0);
+            System.out.println("Semaphore created");
+        }
+        assertTrue("Create Semaphore", s > 0);
+        if (owner) {
+            // Wait until child attaches
+            System.out.println("Waiting ....");
+            test004(s);
+            System.out.println("Done waiting");
+            test005(s);
+        }
+        else {
+            test005(s);
+            System.out.println("Released");
+        }
+        test003(s);
+    }
+}
+

Propchange: commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestSemaphore.java
------------------------------------------------------------------------------
    svn:eol-style = native