/
BaseTriWrapConsumerTest.java
138 lines (115 loc) · 4.06 KB
/
BaseTriWrapConsumerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package org.apache.dubbo.sample.tri;
import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.rpc.RpcException;
import org.apache.dubbo.sample.tri.service.WrapGreeter;
import org.junit.Assert;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class BaseTriWrapConsumerTest {
protected static WrapGreeter delegate;
// @BeforeClass
// public static void initStub() {
// ReferenceConfig<WrapGreeter> ref = new ReferenceConfig<>();
// ref.setInterface(WrapGreeter.class);
// ref.setCheck(false);
// ref.setTimeout(3000);
// ref.setProtocol(CommonConstants.TRIPLE);
// ref.setLazy(true);
//
// DubboBootstrap bootstrap = DubboBootstrap.getInstance();
// bootstrap.application(new ApplicationConfig("demo-consumer"))
// .registry(new RegistryConfig(TriSampleConstants.ZK_ADDRESS))
// .reference(ref)
// .start();
// delegate = ref.get();
// }
@Test
public void sayHelloUnaryRequestVoid() {
Assert.assertEquals("hello!void", delegate.sayHelloRequestVoid());
}
@Test
public void sayHelloUnaryResponseVoid() {
delegate.sayHelloResponseVoid("void");
}
@Test
public void sayHelloUnary() {
Assert.assertEquals("hello,unary", delegate.sayHello("unary"));
}
@Test(expected = RpcException.class)
public void sayHelloException() {
delegate.sayHelloException("exception");
}
@Test
public void sayHelloServerStream() throws InterruptedException {
int n = 10;
CountDownLatch latch = new CountDownLatch(n);
delegate.sayHelloServerStream("server stream", new StdoutStreamObserver<String>("sayHelloServerStream") {
@Override
public void onNext(String data) {
super.onNext(data);
latch.countDown();
}
});
Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));
delegate.sayHelloServerStream("server stream", new StreamObserver<String>() {
@Override
public void onNext(String data) {
System.out.println(data);
}
@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
}
@Override
public void onCompleted() {
System.out.println("onCompleted");
}
});
StreamObserver<String> request = delegate.sayHelloStream(new StreamObserver<String>() {
@Override
public void onNext(String data) {
System.out.println(data);
}
@Override
public void onError(Throwable throwable) {
throwable.printStackTrace();
}
@Override
public void onCompleted() {
System.out.println("onCompleted");
}
});
for (int i = 0; i < n; i++) {
request.onNext("stream request" + i);
}
request.onCompleted();
}
@Test
public void sayHelloStream() throws InterruptedException {
int n = 10;
CountDownLatch latch = new CountDownLatch(n);
final StreamObserver<String> request = delegate.sayHelloStream(new StdoutStreamObserver<String>("sayHelloStream") {
@Override
public void onNext(String data) {
super.onNext(data);
latch.countDown();
}
});
for (int i = 0; i < n; i++) {
request.onNext("stream request");
}
request.onCompleted();
Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));
}
@Test
public void sayHelloLong() {
int power = 25;
for (int i = 0; i < power; i++) {
final int len = (1 << i);
final String response = delegate.sayHelloLong(len);
System.out.println("Response len:" + response.length());
Assert.assertEquals(len, response.length());
}
}
}