22
33import io.growing.sdk.java.GrowingAPI;
44import 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.*;
118import org.junit.runner.RunWith;
129import 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
2219 */
2320@RunWith(JUnit4.class)
2421public 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}
0 commit comments