Skip to content

Commit 91b18d1

Browse files
authored
fix: mvn test error (#32)
1 parent 6bdf705 commit 91b18d1

File tree

5 files changed

+70
-74
lines changed

5 files changed

+70
-74
lines changed

src/test/java/io/growing/sdk/java/test/GrowingAPITest.java

Lines changed: 47 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,15 @@
22

33
import io.growing.sdk.java.GrowingAPI;
44
import io.growing.sdk.java.dto.GIOEventMessage;
5-
import io.growing.sdk.java.dto.GIOMessage;
6-
import io.growing.sdk.java.sender.FixThreadPoolSender;
7-
import io.growing.sdk.java.sender.MessageSender;
8-
import io.growing.sdk.java.utils.ConfigUtils;
9-
import org.junit.BeforeClass;
10-
import org.junit.Test;
5+
import io.growing.sdk.java.test.stub.StubStreamHandlerFactory;
6+
import org.json.JSONArray;
7+
import org.junit.*;
118
import org.junit.runner.RunWith;
129
import org.junit.runners.JUnit4;
10+
import org.xerial.snappy.Snappy;
1311

14-
import java.util.ArrayList;
15-
import java.util.List;
16-
import java.util.concurrent.TimeUnit;
12+
import java.net.URL;
13+
import java.util.concurrent.CountDownLatch;
1714

1815
/**
1916
* @author : tong.wang
@@ -22,77 +19,63 @@
2219
*/
2320
@RunWith(JUnit4.class)
2421
public class GrowingAPITest {
25-
private final static String projectId = ConfigUtils.getStringValue("project.id", "");
22+
private static StubStreamHandlerFactory factory;
23+
private volatile Exception mException;
2624

2725
@BeforeClass
2826
public static void before() {
29-
System.setProperty("java.util.logging.config.file","src/test/resources/logging.properties");
27+
factory = StubStreamHandlerFactory.get();
28+
try {
29+
URL.setURLStreamHandlerFactory(factory);
30+
} catch (Error ignored) {
31+
}
32+
33+
System.setProperty("java.util.logging.config.file", "src/test/resources/logging.properties");
34+
}
35+
36+
@Before
37+
public void beforeTest() {
38+
mException = null;
39+
}
40+
41+
@After
42+
public void afterTest() {
43+
if (mException != null) {
44+
Assert.fail(mException.getMessage());
45+
}
3046
}
3147

3248
@Test
33-
public void apiSendEventTest() {
49+
public void apiSendEventTest() throws InterruptedException {
50+
final CountDownLatch countDownLatch = new CountDownLatch(498);
51+
factory.setStubHttpURLConnectionListener(new StubStreamHandlerFactory.StubHttpURLConnectionListener() {
52+
@Override
53+
public void onSend(URL url, byte[] msg) {
54+
try {
55+
String originMessage = new String(Snappy.uncompress(msg));
56+
JSONArray jsonArray = new JSONArray(originMessage);
57+
int length = jsonArray.length();
58+
while (length-- != 0) {
59+
countDownLatch.countDown();
60+
}
61+
} catch (Exception e) {
62+
mException = e;
63+
}
64+
}
65+
});
3466
for (int i = 0; i < 500; i++) {
3567
GIOEventMessage msg = new GIOEventMessage.Builder()
36-
.eventKey(""+i)
68+
.eventKey("" + i)
3769
.eventNumValue(i)
38-
.loginUserId(i+"")
70+
.loginUserId(i + "")
3971
.addEventVariable("product_name", "苹果")
4072
.addEventVariable("product_classify", "水果")
4173
.addEventVariable("product_classify", "水果")
4274
.addEventVariable("product_price", 14)
4375
.build();
4476
GrowingAPI.send(msg);
45-
try {
46-
TimeUnit.MILLISECONDS.sleep(100);
47-
} catch (InterruptedException e) {
48-
e.printStackTrace();
49-
}
5077
}
51-
52-
try {
53-
TimeUnit.SECONDS.sleep(10);
54-
} catch (InterruptedException e) {
55-
e.printStackTrace();
56-
}
57-
}
58-
59-
@Test
60-
public void senderTest() throws InterruptedException {
61-
MessageSender sender = new FixThreadPoolSender();
62-
for (int i = 0; i < 200; i++){
63-
List<GIOMessage> list = new ArrayList<GIOMessage>();
64-
GIOMessage msg = new GIOEventMessage.Builder()
65-
.eventKey("3")
66-
.eventNumValue(3)
67-
.addEventVariable("product_name", "苹果")
68-
.addEventVariable("product_classify", "水果")
69-
.addEventVariable("product_price", 14)
70-
.build();
71-
72-
list.add(msg);
73-
sender.sendMsg(projectId, list);
74-
}
75-
TimeUnit.SECONDS.sleep(10);
76-
}
77-
78-
@Test
79-
public void sendWithProxy() throws InterruptedException {
80-
MessageSender sender = new FixThreadPoolSender();
81-
82-
GIOMessage msg = new GIOEventMessage.Builder()
83-
.eventKey("3")
84-
.eventNumValue(3)
85-
.addEventVariable("product_name", "苹果")
86-
.addEventVariable("product_classify", "水果")
87-
.addEventVariable("product_price", 14)
88-
.build();
89-
90-
List list = new ArrayList();
91-
list.add(msg);
92-
93-
sender.sendMsg(projectId, list);
94-
95-
TimeUnit.SECONDS.sleep(15);
78+
countDownLatch.await();
9679
}
9780

9881
}

src/test/java/io/growing/sdk/java/test/MultiProjectIdTest.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ public class MultiProjectIdTest {
2323

2424
@BeforeClass
2525
public static void before() {
26-
factory = new StubStreamHandlerFactory();
27-
URL.setURLStreamHandlerFactory(factory);
26+
factory = StubStreamHandlerFactory.get();
27+
try {
28+
URL.setURLStreamHandlerFactory(factory);
29+
} catch (Error ignored) {
30+
}
2831

2932
Properties properties = new Properties();
3033
properties.setProperty("run.mode", "production");

src/test/java/io/growing/sdk/java/test/stub/StubStreamHandlerFactory.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@
66
public class StubStreamHandlerFactory implements URLStreamHandlerFactory {
77
private StubHttpURLConnectionListener mStubHttpURLConnectionListener;
88

9+
private StubStreamHandlerFactory() {
10+
super();
11+
}
12+
13+
public static StubStreamHandlerFactory get() {
14+
return StubStreamHandlerFactory.SingleInstance.INSTANCE;
15+
}
16+
917
@Override
1018
public URLStreamHandler createURLStreamHandler(String protocol) {
1119
return new StubHttpURLStreamHandler();
@@ -15,6 +23,14 @@ public void setStubHttpURLConnectionListener(StubHttpURLConnectionListener httpU
1523
this.mStubHttpURLConnectionListener = httpURLConnectionListener;
1624
}
1725

26+
public interface StubHttpURLConnectionListener {
27+
void onSend(URL url, byte[] msg);
28+
}
29+
30+
private static class SingleInstance {
31+
private static final StubStreamHandlerFactory INSTANCE = new StubStreamHandlerFactory();
32+
}
33+
1834
private class StubHttpURLStreamHandler extends URLStreamHandler {
1935
@Override
2036
protected URLConnection openConnection(URL url) {
@@ -66,8 +82,4 @@ public int getResponseCode() throws IOException {
6682
return 204;
6783
}
6884
}
69-
70-
public interface StubHttpURLConnectionListener {
71-
void onSend(URL url, byte[] msg);
72-
}
7385
}

src/test/resources/gio.properties

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
#proxy.port=3128
33
#proxy.user=demo
44
#proxy.password=demo
5-
65
project.id=xxx
76
#run.mode=test
8-
97
connection.timeout=5000
108
read.timeout=5000
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
handlers= java.util.logging.ConsoleHandler
2-
java.util.logging.ConsoleHandler.level = ALL
1+
handlers=java.util.logging.ConsoleHandler
2+
java.util.logging.ConsoleHandler.level=ALL
33
sun.net.www.protocol.http.HttpURLConnection.level=ALL

0 commit comments

Comments
 (0)