You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by cl...@apache.org on 2022/07/11 20:04:34 UTC

[activemq-artemis-native] 02/02: Using slf4j and Fixing Assume clauses on testsuite

This is an automated email from the ASF dual-hosted git repository.

clebertsuconic pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/activemq-artemis-native.git

commit 72d3877a419ee41128ab96c4a59c6e1b4266ea36
Author: Clebert Suconic <cl...@apache.org>
AuthorDate: Mon Jul 11 14:19:07 2022 -0400

    Using slf4j and Fixing Assume clauses on testsuite
---
 pom.xml                                            | 17 ++++++++
 .../artemis/nativo/jlibaio/LibaioContext.java      | 14 ++++--
 .../artemis/nativo/jlibaio/LibaioFile.java         |  7 ++-
 .../artemis/nativo/jlibaio/NativeLogger.java       | 33 +++-----------
 .../artemis/nativo/jlibaio/SystemCallback.java     | 50 ----------------------
 .../nativo/jlibaio/test/LibaioStressTest.java      | 14 +++++-
 .../artemis/nativo/jlibaio/test/LibaioTest.java    | 13 +++---
 .../artemis/nativo/jlibaio/test/LoadedTest.java    |  4 ++
 .../nativo/jlibaio/test/OpenCloseContextTest.java  | 14 +++---
 9 files changed, 71 insertions(+), 95 deletions(-)

diff --git a/pom.xml b/pom.xml
index 89ceeb8..ba2c5c3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -33,6 +33,7 @@
     <url />
 
     <properties>
+        <slf4j.version>1.7.36</slf4j.version>
         <version.org.jacoco>0.8.6</version.org.jacoco>
         <version.org.jacoco.plugin>0.8.6</version.org.jacoco.plugin>
         <surefire.version>2.22.2</surefire.version>
@@ -104,6 +105,19 @@
           <version>${version.org.jacoco}</version>
            <scope>test</scope>
        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+            <version>${slf4j.version}</version>
+            <!-- License: MIT -->
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-simple</artifactId>
+            <version>${slf4j.version}</version>
+            <scope>test</scope>
+            <!-- License: MIT -->
+        </dependency>
     </dependencies>
 
     <profiles>
@@ -267,6 +281,9 @@
                         <runOrder>alphabetical</runOrder>
                         <redirectTestOutputToFile>${maven.test.redirectTestOutputToFile}</redirectTestOutputToFile>
                         <argLine>${activemq-surefire-argline}</argLine>
+                        <systemPropertyVariables>
+                            <org.slf4j.simpleLogger.defaultLogLevel>TRACE</org.slf4j.simpleLogger.defaultLogLevel>
+                        </systemPropertyVariables>
                     </configuration>
                 </plugin>
                 <plugin>
diff --git a/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/LibaioContext.java b/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/LibaioContext.java
index d91fdbe..f0d6a2e 100644
--- a/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/LibaioContext.java
+++ b/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/LibaioContext.java
@@ -26,6 +26,9 @@ import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
 import java.util.concurrent.atomic.AtomicLong;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * This class is used as an aggregator for the {@link LibaioFile}.
  * <br>
@@ -42,6 +45,8 @@ import java.util.concurrent.atomic.AtomicLong;
  */
 public class LibaioContext<Callback extends SubmitInfo> implements Closeable {
 
+   private static final Logger logger = LoggerFactory.getLogger(LibaioContext.class);
+
    private static final AtomicLong totalMaxIO = new AtomicLong(0);
 
    /**
@@ -61,6 +66,7 @@ public class LibaioContext<Callback extends SubmitInfo> implements Closeable {
 
    private static boolean loadLibrary(final String name) {
       try {
+         logger.debug("Loading {}", name);
          System.loadLibrary(name);
          if (getNativeVersion() != EXPECTED_NATIVE_VERSION) {
             NativeLogger.incompatibleNativeLibrary();
@@ -69,7 +75,7 @@ public class LibaioContext<Callback extends SubmitInfo> implements Closeable {
             return true;
          }
       } catch (Throwable e) {
-         NativeLogger.debug(name + " -> error loading the native library", e);
+         logger.debug(name + " -> not possible to load native library", e);
          return false;
       }
 
@@ -93,12 +99,12 @@ public class LibaioContext<Callback extends SubmitInfo> implements Closeable {
             });
             break;
          } else {
-            NativeLogger.debug("Library " + library + " not found!");
+            logger.debug("Library {} not found!", library);
          }
       }
 
       if (!loaded) {
-         NativeLogger.debug("Couldn't locate LibAIO Wrapper");
+         logger.debug("Couldn't locate LibAIO Wrapper");
       }
    }
 
@@ -242,7 +248,7 @@ public class LibaioContext<Callback extends SubmitInfo> implements Closeable {
             try {
                ioSpace.tryAcquire(queueSize, 10, TimeUnit.SECONDS);
             } catch (Exception e) {
-               NativeLogger.warn(e.getMessage(), e);
+               logger.warn(e.getMessage(), e);
             }
          }
          totalMaxIO.addAndGet(-queueSize);
diff --git a/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/LibaioFile.java b/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/LibaioFile.java
index f03ed87..7b6967b 100644
--- a/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/LibaioFile.java
+++ b/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/LibaioFile.java
@@ -19,11 +19,16 @@ package org.apache.activemq.artemis.nativo.jlibaio;
 import java.io.IOException;
 import java.nio.ByteBuffer;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /**
  * This is an extension to use libaio.
  */
 public final class LibaioFile<Callback extends SubmitInfo> implements AutoCloseable {
 
+   private static final Logger logger = LoggerFactory.getLogger(LibaioFile.class);
+
    protected boolean open;
    /**
     * This represents a structure allocated on the native
@@ -120,7 +125,7 @@ public final class LibaioFile<Callback extends SubmitInfo> implements AutoClosea
       try {
          LibaioContext.fill(fd, alignment, size);
       } catch (OutOfMemoryError e) {
-         NativeLogger.warn("Did not have enough memory to allocate " + size + " bytes in memory while filling the file, using simple fallocate");
+         logger.warn("Did not have enough memory to allocate " + size + " bytes in memory while filling the file, using simple fallocate");
          LibaioContext.fallocate(fd, size);
       }
    }
diff --git a/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/NativeLogger.java b/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/NativeLogger.java
index ad14bdc..be487ba 100644
--- a/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/NativeLogger.java
+++ b/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/NativeLogger.java
@@ -16,43 +16,22 @@
  */
 package org.apache.activemq.artemis.nativo.jlibaio;
 
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /** Notice: After making changes to the native interface, you have to use mvn install at least once to generate the .h
  *          This is because the maven compiler plugin is the one generating org_apache_activemq_artemis_native_jlibaio_LibaioContext.h
  *          So that file needs to be updated before Cmake comes along to compile the module. */
 public class NativeLogger {
 
-   public static final String PROJECT_PREFIX = "jlibaio";
-
-   private static LoggerCallback loggerCallback = new SystemCallback();
+   private static final Logger logger = LoggerFactory.getLogger(NativeLogger.class);
 
-   public static void setLoggerCallback(LoggerCallback callback) {
-      loggerCallback = callback;
-   }
+   public static final String PROJECT_PREFIX = "jlibaio";
 
    private static final int DIFFERENT_VERSION_ID = 163002;
    private static final String DIFFERENT_VERSION = PROJECT_PREFIX + DIFFERENT_VERSION_ID + " You have a native library with a different version than expected";
 
    public final static void incompatibleNativeLibrary() {
-       warn(DIFFERENT_VERSION);
-   }
-
-   public final static void info(String message) {
-      loggerCallback.info(message);
-   }
-
-   public final static void warn(String message) {
-      loggerCallback.warn(message);
-   }
-
-   public final static void warn(String message, Throwable e) {
-      loggerCallback.warn(message, e);
-   }
-
-   public final static void debug(String message) {
-      loggerCallback.debug(message);
-   }
-
-   public final static void debug(String message, Throwable e) {
-      loggerCallback.debug(message, e);
+       logger.warn(DIFFERENT_VERSION);
    }
 }
diff --git a/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/SystemCallback.java b/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/SystemCallback.java
deleted file mode 100644
index b50c209..0000000
--- a/src/main/java/org/apache/activemq/artemis/nativo/jlibaio/SystemCallback.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * 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.activemq.artemis.nativo.jlibaio;
-
-/** This will use System.err for warn and System.out for info */
-public class SystemCallback implements LoggerCallback {
-
-   private static final boolean DEBUG = Boolean.parseBoolean(System.getProperty(SystemCallback.class.getName() + ".DEBUG", "false"));
-
-   public void debug(String message, Throwable e) {
-      if (DEBUG) {
-         System.out.println("Debug from ArtemisNative: " + message);
-         e.printStackTrace(System.out);
-      }
-   }
-
-   public void debug(String message) {
-      if (DEBUG) {
-         System.out.println("Debug from ArtemisNative: " + message);
-      }
-   }
-
-   public void info(String message) {
-      System.out.println("Information from ArtemisNative: " + message);
-   }
-
-   public void warn(String message) {
-      System.err.println("Warning from ArtemisNative: " + message);
-   }
-
-   public void warn(String message, Throwable e) {
-      System.err.println("Warning from ArtemisNative: " + message);
-      e.printStackTrace(System.err);
-   }
-}
diff --git a/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/LibaioStressTest.java b/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/LibaioStressTest.java
index 4fc1d0c..aeb7344 100644
--- a/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/LibaioStressTest.java
+++ b/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/LibaioStressTest.java
@@ -30,9 +30,12 @@ import org.junit.After;
 import org.junit.Assert;
 import org.junit.Assume;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * This test is using a different package from {@link LibaioFile}
@@ -40,10 +43,17 @@ import org.junit.rules.TemporaryFolder;
  */
 public class LibaioStressTest {
 
+    private static final Logger logger = LoggerFactory.getLogger(LibaioStressTest.class);
+
     private static final int STRESS_TIME = Integer.parseInt(System.getProperty("test.stress.time", "5000"));
 
     static {
-        System.out.println("LibaioStressTest:: -Dtest.stress.time=" + STRESS_TIME);
+        logger.debug("LibaioStressTest:: -Dtest.stress.time=" + STRESS_TIME);
+    }
+
+    @BeforeClass
+    public static void testAssume() {
+        Assume.assumeTrue(LibaioContext.isLoaded());
     }
 
     /**
@@ -231,7 +241,7 @@ public class LibaioStressTest {
             }
 
             if (count % (sleeps ? 1_000 : 100_000) == 0) {
-                System.out.println("Writen "  + count + " buffers at " + fileName);
+                logger.debug("Writen "  + count + " buffers at " + fileName);
             }
             MyClass myClass = callbackCache.get();
 
diff --git a/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/LibaioTest.java b/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/LibaioTest.java
index ec8b73d..7318412 100644
--- a/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/LibaioTest.java
+++ b/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/LibaioTest.java
@@ -37,12 +37,15 @@ import org.junit.BeforeClass;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 /**
  * This test is using a different package from {@link LibaioFile}
  * as I need to validate public methods on the API
  */
 public class LibaioTest {
+   private static final Logger logger = LoggerFactory.getLogger(LibaioTest.class);
 
    @BeforeClass
    public static void testAIO() {
@@ -174,8 +177,8 @@ public class LibaioTest {
       Assert.assertEquals(fileDescriptor[0].getBlockSize(), fileDescriptor[1].getBlockSize());
       Assert.assertEquals(LibaioContext.getBlockSize(temporaryFolder.getRoot()), LibaioContext.getBlockSize(file1));
       Assert.assertEquals(LibaioContext.getBlockSize(file1), LibaioContext.getBlockSize(file2));
-      System.out.println("blockSize = " + fileDescriptor[0].getBlockSize());
-      System.out.println("blockSize /tmp= " + LibaioContext.getBlockSize("/tmp"));
+      logger.debug("blockSize = " + fileDescriptor[0].getBlockSize());
+      logger.debug("blockSize /tmp= " + LibaioContext.getBlockSize("/tmp"));
 
       ByteBuffer buffer = LibaioContext.newAlignedBuffer(4096, 4096);
 
@@ -302,7 +305,7 @@ public class LibaioTest {
          fileDescriptor.write(0, BUFFER_SIZE, buffer, callback);
 
          int retValue = control.poll(callbacks, 1, LIBAIO_QUEUE_SIZE);
-         System.out.println("Return from poll::" + retValue);
+         logger.debug("Return from poll::" + retValue);
          Assert.assertEquals(1, retValue);
 
          Assert.assertSame(callback, callbacks[0]);
@@ -401,7 +404,7 @@ public class LibaioTest {
          // Error condition
          Assert.assertSame(callbacks[0], callback);
 
-         System.out.println("Error:" + callbacks[0]);
+         logger.debug("Error:" + callbacks[0]);
 
          buffer = fileDescriptor.newBuffer(4096);
          for (int i = 0; i < 4096; i++) {
@@ -682,7 +685,7 @@ public class LibaioTest {
 
       latch.await();
 
-      System.out.println("time = " + (end - start) + " writes/second=" + NUMBER_OF_BLOCKS * 1000L / (end - start));
+      logger.debug("time = " + (end - start) + " writes/second=" + NUMBER_OF_BLOCKS * 1000L / (end - start));
 
       blockedContext.close();
       t.join();
diff --git a/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/LoadedTest.java b/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/LoadedTest.java
index 6a5a05d..6e803ef 100644
--- a/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/LoadedTest.java
+++ b/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/LoadedTest.java
@@ -19,12 +19,16 @@ package org.apache.activemq.artemis.nativo.jlibaio.test;
 
 import org.apache.activemq.artemis.nativo.jlibaio.LibaioContext;
 import org.junit.Assert;
+import org.junit.Assume;
 import org.junit.Test;
 
 public class LoadedTest {
+   private static final String OS = System.getProperty("os.name").toLowerCase();
+   private static final boolean IS_LINUX = OS.startsWith("linux");
 
    @Test
    public void testValidateIsLoaded() {
+      Assume.assumeTrue(IS_LINUX);
       Assert.assertTrue(LibaioContext.isLoaded());
    }
 
diff --git a/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/OpenCloseContextTest.java b/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/OpenCloseContextTest.java
index e6c27f2..1d8775e 100644
--- a/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/OpenCloseContextTest.java
+++ b/src/test/java/org/apache/activemq/artemis/nativo/jlibaio/test/OpenCloseContextTest.java
@@ -30,9 +30,13 @@ import org.junit.BeforeClass;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.TemporaryFolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class OpenCloseContextTest {
 
+   Logger logger = LoggerFactory.getLogger(OpenCloseContextTest.class);
+
    @BeforeClass
    public static void testAIO() {
       Assume.assumeTrue(LibaioContext.isLoaded());
@@ -52,7 +56,7 @@ public class OpenCloseContextTest {
          buffer.put((byte) 'x');
 
       for (int i = 0; i < 10; i++) {
-         System.out.println("#test " + i);
+         logger.debug("#test " + i);
          final LibaioContext control = new LibaioContext<>(5, true, true);
          Thread t = new Thread() {
             @Override
@@ -79,7 +83,7 @@ public class OpenCloseContextTest {
                } catch (Throwable e) {
                   e.printStackTrace();
                }
-               System.out.println("done");
+               logger.debug("done");
             }
          });
 
@@ -110,7 +114,7 @@ public class OpenCloseContextTest {
          buffer.put((byte) 'x');
 
       for (int i = 0; i < 10; i++) {
-         System.out.println("#test " + i);
+         logger.debug("#test " + i);
          final LibaioContext control = new LibaioContext<>(5, true, true);
          Thread t = new Thread() {
             @Override
@@ -137,7 +141,7 @@ public class OpenCloseContextTest {
                } catch (Throwable e) {
                   e.printStackTrace();
                }
-               System.out.println("done");
+               logger.debug("done");
             }
          });
 
@@ -172,8 +176,6 @@ public class OpenCloseContextTest {
 
       control2.close();
       control2.poll();
-
-      System.out.println("Hello!!");
    }
 
 }