Skip to content

Commit e044fe2

Browse files
flycashwu-sheng
authored andcommitted
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
1 parent 9c9bae8 commit e044fe2

File tree

3 files changed

+127
-1
lines changed

3 files changed

+127
-1
lines changed

apm-commons/apm-datacarrier/src/main/java/org/apache/skywalking/apm/commons/datacarrier/EnvUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static long getLong(String envName, long defaultValue) {
4242
String envValue = System.getenv(envName);
4343
if (envValue != null) {
4444
try {
45-
value = Integer.parseInt(envValue);
45+
value = Long.parseLong(envValue);
4646
} catch (NumberFormatException e) {
4747

4848
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.commons.datacarrier;
20+
21+
import org.junit.After;
22+
import org.junit.Before;
23+
import org.junit.Test;
24+
25+
import java.lang.reflect.Field;
26+
import java.util.Map;
27+
28+
import static org.junit.Assert.*;
29+
30+
/**
31+
* @author dengming
32+
* 2019-04-20
33+
*/
34+
public class EnvUtilTest {
35+
36+
private Map<String, String> writableEnv;
37+
@Before
38+
public void before() {
39+
try {
40+
Map<String, String> env = System.getenv();
41+
Class<?> cl = env.getClass();
42+
Field field = cl.getDeclaredField("m");
43+
field.setAccessible(true);
44+
writableEnv = (Map<String, String>) field.get(env);
45+
writableEnv.put("myInt", "123");
46+
writableEnv.put("wrongInt", "wrong123");
47+
writableEnv.put("myLong", "12345678901234567");
48+
writableEnv.put("wrongLong", "wrong123");
49+
} catch (Exception e) {
50+
throw new IllegalStateException("Failed to set environment variable", e);
51+
}
52+
}
53+
54+
@Test
55+
public void getInt() {
56+
assertEquals(123, EnvUtil.getInt("myInt", 234));
57+
assertEquals(234, EnvUtil.getLong("wrongInt", 234));
58+
}
59+
60+
@Test
61+
public void getLong() {
62+
assertEquals(12345678901234567L, EnvUtil.getLong("myLong", 123L));
63+
assertEquals(987654321987654321L, EnvUtil.getLong("wrongLong", 987654321987654321L));
64+
}
65+
66+
@After
67+
public void after() {
68+
writableEnv.remove("myInt");
69+
writableEnv.remove("wrongInt");
70+
writableEnv.remove("myLong");
71+
writableEnv.remove("wrongLong");
72+
assertNull(System.getenv("myInt"));
73+
}
74+
75+
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
package org.apache.skywalking.apm.commons.datacarrier.consumer;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
24+
import static org.junit.Assert.*;
25+
26+
/**
27+
* @author dengming
28+
* 2019-04-20
29+
*/
30+
public class ConsumerPoolFactoryTest {
31+
32+
@Before
33+
public void createIfAbsent() throws Exception {
34+
BulkConsumePool.Creator creator = new BulkConsumePool.Creator("my-test-pool", 10, 20);
35+
boolean firstCreated = ConsumerPoolFactory.INSTANCE.createIfAbsent("my-test-pool", creator);
36+
assertTrue(firstCreated);
37+
38+
boolean secondCreated = ConsumerPoolFactory.INSTANCE.createIfAbsent("my-test-pool", creator);
39+
assertTrue(!secondCreated);
40+
}
41+
42+
@Test
43+
public void get() {
44+
ConsumerPool consumerPool = ConsumerPoolFactory.INSTANCE.get("my-test-pool");
45+
assertNotNull(consumerPool);
46+
47+
ConsumerPool notExist = ConsumerPoolFactory.INSTANCE.get("not-exists-pool");
48+
assertNull(notExist);
49+
}
50+
}

0 commit comments

Comments
 (0)