diff --git a/src/test/java/io/growing/sdk/java/test/GrowingAPITest.java b/src/test/java/io/growing/sdk/java/test/GrowingAPITest.java index 14777fe..e68b92d 100644 --- a/src/test/java/io/growing/sdk/java/test/GrowingAPITest.java +++ b/src/test/java/io/growing/sdk/java/test/GrowingAPITest.java @@ -2,18 +2,15 @@ import io.growing.sdk.java.GrowingAPI; import io.growing.sdk.java.dto.GIOEventMessage; -import io.growing.sdk.java.dto.GIOMessage; -import io.growing.sdk.java.sender.FixThreadPoolSender; -import io.growing.sdk.java.sender.MessageSender; -import io.growing.sdk.java.utils.ConfigUtils; -import org.junit.BeforeClass; -import org.junit.Test; +import io.growing.sdk.java.test.stub.StubStreamHandlerFactory; +import org.json.JSONArray; +import org.junit.*; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; +import org.xerial.snappy.Snappy; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.TimeUnit; +import java.net.URL; +import java.util.concurrent.CountDownLatch; /** * @author : tong.wang @@ -22,77 +19,63 @@ */ @RunWith(JUnit4.class) public class GrowingAPITest { - private final static String projectId = ConfigUtils.getStringValue("project.id", ""); + private static StubStreamHandlerFactory factory; + private volatile Exception mException; @BeforeClass public static void before() { - System.setProperty("java.util.logging.config.file","src/test/resources/logging.properties"); + factory = StubStreamHandlerFactory.get(); + try { + URL.setURLStreamHandlerFactory(factory); + } catch (Error ignored) { + } + + System.setProperty("java.util.logging.config.file", "src/test/resources/logging.properties"); + } + + @Before + public void beforeTest() { + mException = null; + } + + @After + public void afterTest() { + if (mException != null) { + Assert.fail(mException.getMessage()); + } } @Test - public void apiSendEventTest() { + public void apiSendEventTest() throws InterruptedException { + final CountDownLatch countDownLatch = new CountDownLatch(498); + factory.setStubHttpURLConnectionListener(new StubStreamHandlerFactory.StubHttpURLConnectionListener() { + @Override + public void onSend(URL url, byte[] msg) { + try { + String originMessage = new String(Snappy.uncompress(msg)); + JSONArray jsonArray = new JSONArray(originMessage); + int length = jsonArray.length(); + while (length-- != 0) { + countDownLatch.countDown(); + } + } catch (Exception e) { + mException = e; + } + } + }); for (int i = 0; i < 500; i++) { GIOEventMessage msg = new GIOEventMessage.Builder() - .eventKey(""+i) + .eventKey("" + i) .eventNumValue(i) - .loginUserId(i+"") + .loginUserId(i + "") .addEventVariable("product_name", "苹果") .addEventVariable("product_classify", "水果") .addEventVariable("product_classify", "水果") .addEventVariable("product_price", 14) .build(); GrowingAPI.send(msg); - try { - TimeUnit.MILLISECONDS.sleep(100); - } catch (InterruptedException e) { - e.printStackTrace(); - } } - - try { - TimeUnit.SECONDS.sleep(10); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - @Test - public void senderTest() throws InterruptedException { - MessageSender sender = new FixThreadPoolSender(); - for (int i = 0; i < 200; i++){ - List list = new ArrayList(); - GIOMessage msg = new GIOEventMessage.Builder() - .eventKey("3") - .eventNumValue(3) - .addEventVariable("product_name", "苹果") - .addEventVariable("product_classify", "水果") - .addEventVariable("product_price", 14) - .build(); - - list.add(msg); - sender.sendMsg(projectId, list); - } - TimeUnit.SECONDS.sleep(10); - } - - @Test - public void sendWithProxy() throws InterruptedException { - MessageSender sender = new FixThreadPoolSender(); - - GIOMessage msg = new GIOEventMessage.Builder() - .eventKey("3") - .eventNumValue(3) - .addEventVariable("product_name", "苹果") - .addEventVariable("product_classify", "水果") - .addEventVariable("product_price", 14) - .build(); - - List list = new ArrayList(); - list.add(msg); - - sender.sendMsg(projectId, list); - - TimeUnit.SECONDS.sleep(15); + countDownLatch.await(); } } \ No newline at end of file diff --git a/src/test/java/io/growing/sdk/java/test/MultiProjectIdTest.java b/src/test/java/io/growing/sdk/java/test/MultiProjectIdTest.java index 13c6135..a2ca3ca 100644 --- a/src/test/java/io/growing/sdk/java/test/MultiProjectIdTest.java +++ b/src/test/java/io/growing/sdk/java/test/MultiProjectIdTest.java @@ -23,8 +23,11 @@ public class MultiProjectIdTest { @BeforeClass public static void before() { - factory = new StubStreamHandlerFactory(); - URL.setURLStreamHandlerFactory(factory); + factory = StubStreamHandlerFactory.get(); + try { + URL.setURLStreamHandlerFactory(factory); + } catch (Error ignored) { + } Properties properties = new Properties(); properties.setProperty("run.mode", "production"); diff --git a/src/test/java/io/growing/sdk/java/test/stub/StubStreamHandlerFactory.java b/src/test/java/io/growing/sdk/java/test/stub/StubStreamHandlerFactory.java index f81661c..371237d 100644 --- a/src/test/java/io/growing/sdk/java/test/stub/StubStreamHandlerFactory.java +++ b/src/test/java/io/growing/sdk/java/test/stub/StubStreamHandlerFactory.java @@ -6,6 +6,14 @@ public class StubStreamHandlerFactory implements URLStreamHandlerFactory { private StubHttpURLConnectionListener mStubHttpURLConnectionListener; + private StubStreamHandlerFactory() { + super(); + } + + public static StubStreamHandlerFactory get() { + return StubStreamHandlerFactory.SingleInstance.INSTANCE; + } + @Override public URLStreamHandler createURLStreamHandler(String protocol) { return new StubHttpURLStreamHandler(); @@ -15,6 +23,14 @@ public void setStubHttpURLConnectionListener(StubHttpURLConnectionListener httpU this.mStubHttpURLConnectionListener = httpURLConnectionListener; } + public interface StubHttpURLConnectionListener { + void onSend(URL url, byte[] msg); + } + + private static class SingleInstance { + private static final StubStreamHandlerFactory INSTANCE = new StubStreamHandlerFactory(); + } + private class StubHttpURLStreamHandler extends URLStreamHandler { @Override protected URLConnection openConnection(URL url) { @@ -66,8 +82,4 @@ public int getResponseCode() throws IOException { return 204; } } - - public interface StubHttpURLConnectionListener { - void onSend(URL url, byte[] msg); - } } diff --git a/src/test/resources/gio.properties b/src/test/resources/gio.properties index 31d84d0..bb11185 100644 --- a/src/test/resources/gio.properties +++ b/src/test/resources/gio.properties @@ -2,9 +2,7 @@ #proxy.port=3128 #proxy.user=demo #proxy.password=demo - project.id=xxx #run.mode=test - connection.timeout=5000 read.timeout=5000 diff --git a/src/test/resources/logging.properties b/src/test/resources/logging.properties index d762533..e8bf4c0 100644 --- a/src/test/resources/logging.properties +++ b/src/test/resources/logging.properties @@ -1,3 +1,3 @@ -handlers= java.util.logging.ConsoleHandler -java.util.logging.ConsoleHandler.level = ALL +handlers=java.util.logging.ConsoleHandler +java.util.logging.ConsoleHandler.level=ALL sun.net.www.protocol.http.HttpURLConnection.level=ALL \ No newline at end of file