You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@skywalking.apache.org by wu...@apache.org on 2019/04/20 03:31:20 UTC

[skywalking] branch master updated: Add unit test for ConsumerPoolFactory, EnvUtil (#2497)

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

wusheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/skywalking.git


The following commit(s) were added to refs/heads/master by this push:
     new e044fe2  Add unit test for ConsumerPoolFactory, EnvUtil (#2497)
e044fe2 is described below

commit e044fe26cd50e312ddbfb99ce7eb4c307608f916
Author: Ming Deng <mi...@qq.com>
AuthorDate: Sat Apr 20 11:31:15 2019 +0800

    Add unit test for ConsumerPoolFactory, EnvUtil (#2497)
    
    * 1. add unit test for ConsumerPoolFactory, EnvUtil;
    2. fix the bug that Env.getLong invoking the Integer.parseInt;
    
    * Add file header
---
 .../apm/commons/datacarrier/EnvUtil.java           |  2 +-
 .../apm/commons/datacarrier/EnvUtilTest.java       | 76 ++++++++++++++++++++++
 .../consumer/ConsumerPoolFactoryTest.java          | 50 ++++++++++++++
 3 files changed, 127 insertions(+), 1 deletion(-)

diff --git a/apm-commons/apm-datacarrier/src/main/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtil.java b/apm-commons/apm-datacarrier/src/main/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtil.java
index 15e8743..bdcdf18 100644
--- a/apm-commons/apm-datacarrier/src/main/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtil.java
+++ b/apm-commons/apm-datacarrier/src/main/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtil.java
@@ -42,7 +42,7 @@ public class EnvUtil {
         String envValue = System.getenv(envName);
         if (envValue != null) {
             try {
-                value = Integer.parseInt(envValue);
+                value = Long.parseLong(envValue);
             } catch (NumberFormatException e) {
 
             }
diff --git a/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtilTest.java b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtilTest.java
new file mode 100644
index 0000000..b95e862
--- /dev/null
+++ b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtilTest.java
@@ -0,0 +1,76 @@
+/*
+ * 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.skywalking.apm.commons.datacarrier;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+import java.lang.reflect.Field;
+import java.util.Map;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author dengming
+ * 2019-04-20
+ */
+public class EnvUtilTest {
+
+    private Map<String, String> writableEnv;
+    @Before
+    public void before() {
+        try {
+            Map<String, String> env = System.getenv();
+            Class<?> cl = env.getClass();
+            Field field = cl.getDeclaredField("m");
+            field.setAccessible(true);
+            writableEnv = (Map<String, String>) field.get(env);
+            writableEnv.put("myInt", "123");
+            writableEnv.put("wrongInt", "wrong123");
+            writableEnv.put("myLong", "12345678901234567");
+            writableEnv.put("wrongLong", "wrong123");
+        } catch (Exception e) {
+            throw new IllegalStateException("Failed to set environment variable", e);
+        }
+    }
+
+    @Test
+    public void getInt() {
+        assertEquals(123, EnvUtil.getInt("myInt", 234));
+        assertEquals(234, EnvUtil.getLong("wrongInt", 234));
+    }
+
+    @Test
+    public void getLong() {
+        assertEquals(12345678901234567L, EnvUtil.getLong("myLong", 123L));
+        assertEquals(987654321987654321L, EnvUtil.getLong("wrongLong", 987654321987654321L));
+    }
+
+    @After
+    public void after() {
+        writableEnv.remove("myInt");
+        writableEnv.remove("wrongInt");
+        writableEnv.remove("myLong");
+        writableEnv.remove("wrongLong");
+        assertNull(System.getenv("myInt"));
+    }
+
+
+}
\ No newline at end of file
diff --git a/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/consumer/ConsumerPoolFactoryTest.java b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/consumer/ConsumerPoolFactoryTest.java
new file mode 100644
index 0000000..737eb23
--- /dev/null
+++ b/apm-commons/apm-datacarrier/src/test/java/org/apache/skywalking/apm/commons/datacarrier/consumer/ConsumerPoolFactoryTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.skywalking.apm.commons.datacarrier.consumer;
+
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+/**
+ * @author dengming
+ * 2019-04-20
+ */
+public class ConsumerPoolFactoryTest {
+
+    @Before
+    public void createIfAbsent() throws Exception {
+        BulkConsumePool.Creator creator = new BulkConsumePool.Creator("my-test-pool", 10, 20);
+        boolean firstCreated = ConsumerPoolFactory.INSTANCE.createIfAbsent("my-test-pool", creator);
+        assertTrue(firstCreated);
+
+        boolean secondCreated = ConsumerPoolFactory.INSTANCE.createIfAbsent("my-test-pool", creator);
+        assertTrue(!secondCreated);
+    }
+
+    @Test
+    public void get() {
+        ConsumerPool consumerPool = ConsumerPoolFactory.INSTANCE.get("my-test-pool");
+        assertNotNull(consumerPool);
+
+        ConsumerPool notExist = ConsumerPoolFactory.INSTANCE.get("not-exists-pool");
+        assertNull(notExist);
+    }
+}
\ No newline at end of file