did/did-sdk/src/test/java/io/github/ehlxr/did/DidSdkTest.java

60 lines
1.6 KiB
Java
Raw Normal View History

2021-01-22 07:40:02 +00:00
package io.github.ehlxr.did;
2018-08-14 07:21:56 +00:00
2021-01-22 07:40:02 +00:00
import io.github.ehlxr.did.client.SdkClient;
import io.github.ehlxr.did.common.SdkProto;
2021-01-20 07:25:58 +00:00
import org.junit.After;
2021-01-19 10:39:42 +00:00
import org.junit.Before;
2018-08-14 07:21:56 +00:00
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
/**
* @author ehlxr
*/
public class DidSdkTest {
2018-08-14 10:37:34 +00:00
private static final int NUM = 10;
2021-01-19 10:39:42 +00:00
SdkClient client;
2018-08-14 07:21:56 +00:00
2021-01-19 10:39:42 +00:00
@Before
public void init() {
2021-01-20 07:25:58 +00:00
client = new SdkClient("127.0.0.1", 16831, 5000);
2018-08-14 07:21:56 +00:00
client.start();
2021-01-19 10:39:42 +00:00
}
2018-08-14 07:21:56 +00:00
2021-01-20 07:25:58 +00:00
@After
public void destroy() {
client.shutdown();
}
2021-01-19 10:39:42 +00:00
@Test
public void didSdkTest() throws Exception {
2018-08-14 07:21:56 +00:00
// 测试同步请求关注rqid是否对应
for (int i = 0; i < NUM; i++) {
2021-01-20 07:25:58 +00:00
SdkProto resultProto = client.invokeSync();
System.out.println(i + " resultProto: " + resultProto);
2018-08-14 07:21:56 +00:00
}
System.out.println("invokeync test finish");
// 测试异步请求关注rqid是否对应
final CountDownLatch countDownLatch = new CountDownLatch(NUM);
for (int i = 0; i < NUM; i++) {
final int finalI = i;
2021-01-20 07:25:58 +00:00
client.invokeAsync(responseFuture -> {
2021-01-19 06:45:49 +00:00
countDownLatch.countDown();
2021-01-20 07:25:58 +00:00
System.out.println(finalI + " resultProto: " + responseFuture.getSdkProto());
2018-08-14 07:21:56 +00:00
});
}
countDownLatch.await(10, TimeUnit.SECONDS);
System.out.println("invokeAsync test finish");
}
2021-01-19 10:39:42 +00:00
@Test
public void testInvoke() throws Exception {
System.out.println(client.invoke());
client.setTimeoutMillis(3000);
System.out.println(client.invoke());
}
2018-08-14 07:21:56 +00:00
}