Skip to content

Commit

Permalink
fix: mvn test error (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
styluo authored Dec 5, 2022
1 parent 6bdf705 commit 91b18d1
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 74 deletions.
111 changes: 47 additions & 64 deletions src/test/java/io/growing/sdk/java/test/GrowingAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<GIOMessage> list = new ArrayList<GIOMessage>();
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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -66,8 +82,4 @@ public int getResponseCode() throws IOException {
return 204;
}
}

public interface StubHttpURLConnectionListener {
void onSend(URL url, byte[] msg);
}
}
2 changes: 0 additions & 2 deletions src/test/resources/gio.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions src/test/resources/logging.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 91b18d1

Please sign in to comment.