-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
PbTest.java
109 lines (96 loc) · 3.78 KB
/
PbTest.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
package org.apache.dubbo.sample.tri;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.stream.StreamObserver;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.bootstrap.DubboBootstrap;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
public class PbTest {
private static PbGreeter delegate;
@BeforeClass
public static void init() {
ReferenceConfig<PbGreeter> ref = new ReferenceConfig<>();
ref.setInterface(PbGreeter.class);
ref.setCheck(false);
ref.setInterface(PbGreeter.class);
ref.setCheck(false);
ref.setUrl("tri://127.0.0.1:50051");
ref.setProtocol(CommonConstants.TRIPLE);
ref.setLazy(true);
ref.setTimeout(10000);
DubboBootstrap bootstrap = DubboBootstrap.getInstance();
bootstrap.application(new ApplicationConfig("demo-consumer"))
// .registry(new RegistryConfig("zookeeper://127.0.0.1:2181"))
.reference(ref)
.start();
delegate = ref.get();
}
@Test
public void serverStream() throws InterruptedException {
int n = 10;
CountDownLatch latch = new CountDownLatch(n);
final GreeterRequest request = GreeterRequest.newBuilder()
.setName("request")
.build();
delegate.greetServerStream(request, new StdoutStreamObserver<GreeterReply>("sayGreeterServerStream") {
@Override
public void onNext(GreeterReply data) {
super.onNext(data);
latch.countDown();
}
});
Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));
}
@Test
public void stream() throws InterruptedException {
int n = 10;
CountDownLatch latch = new CountDownLatch(n);
final GreeterRequest request = GreeterRequest.newBuilder()
.setName("stream request")
.build();
final StreamObserver<GreeterRequest> requestObserver = delegate.greetStream(new StdoutStreamObserver<GreeterReply>("sayGreeterStream") {
@Override
public void onNext(GreeterReply data) {
super.onNext(data);
latch.countDown();
}
});
for (int i = 0; i < n; i++) {
requestObserver.onNext(request);
}
requestObserver.onCompleted();
Assert.assertTrue(latch.await(3, TimeUnit.SECONDS));
}
@Test
public void unaryGreeter() {
final GreeterReply reply = delegate.greet(GreeterRequest.newBuilder()
.setName("name")
.build());
Assert.assertNotNull(reply);
}
@Test(expected = RpcException.class)
public void clientSendLargeSizeHeader() throws InterruptedException {
StringBuilder sb = new StringBuilder("a");
for (int j = 0; j < 15; j++) {
sb.append(sb);
}
sb.setLength(8191);
RpcContext.getClientAttachment().setObjectAttachment("large-size-meta", sb.toString());
delegate.greet(GreeterRequest.newBuilder().setName("meta").build());
}
@Test
public void attachmentTest() {
final String key = "user-attachment";
final String value = "attachment-value";
RpcContext.getClientAttachment().setAttachment(key, value);
delegate.greetWithAttachment(GreeterRequest.newBuilder().setName("meta").build());
final String returned = (String) RpcContext.getServiceContext().getObjectAttachment(key);
Assert.assertEquals(value, returned);
}
}