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
|
/**
* Copyright 2012 Kjetil Ørbekk <kjetil.orbekk@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.orbekk.protobuf;
import java.util.concurrent.CountDownLatch;
import org.junit.Before;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import com.google.protobuf.RpcCallback;
import com.google.protobuf.RpcController;
import com.orbekk.protobuf.Test.Type1;
import com.orbekk.protobuf.Test.Type2;
public class ProtobufFunctionalTest {
CountDownLatch returnC = new CountDownLatch(1);
SimpleProtobufServer server = SimpleProtobufServer.create(0);
int serverport = server.getPort();
RpcChannel channel = RpcChannel.create("localhost", serverport);
TestService directService = new TestService();
Test.Service service = Test.Service.newStub(channel);
@Before public void setUp() {
server.start();
server.registerService(directService);
}
public class TestService extends Test.Service {
@Override
public void testA(RpcController controller, Type1 request,
RpcCallback<Type2> done) {
Type2 response = Type2.newBuilder()
.setMessage("TestA")
.build();
done.run(response);
}
@Override
public void testB(RpcController controller, Type1 request,
RpcCallback<Type2> done) {
controller.setFailed("error");
done.run(null);
}
@Override
public void testC(RpcController controller, Type1 request,
RpcCallback<Type2> done) {
try {
returnC.await();
} catch (InterruptedException e) {
}
Type2 response = Type2.newBuilder()
.setResult(request.getA() + request.getB())
.setMessage("TestC result")
.build();
done.run(response);
}
}
@org.junit.Test public void respondsNormally() throws Exception {
Test.Type1 request = Test.Type1.newBuilder().build();
int count = 10;
final CountDownLatch stop = new CountDownLatch(count);
for (int i = 0; i < count; i++) {
final Rpc rpc = new Rpc();
service.testA(rpc, request, new RpcCallback<Type2>() {
@Override public void run(Type2 result) {
assertThat(result.getMessage(), is("TestA"));
assertThat(rpc.isOk(), is(true));
stop.countDown();
}
});
}
stop.await();
}
@org.junit.Test public void testError() throws Exception {
Test.Type1 request = Test.Type1.newBuilder().build();
final CountDownLatch stop = new CountDownLatch(1);
final Rpc rpc = new Rpc();
service.testB(rpc, request, new RpcCallback<Type2>() {
@Override public void run(Type2 result) {
assertThat(rpc.isOk(), is(false));
assertThat(rpc.failed(), is(true));
assertThat(rpc.errorText(), is("error"));
stop.countDown();
}
});
stop.await();
}
@org.junit.Test public void failsWhenServerDies() throws Exception {
Test.Type1 request = Test.Type1.newBuilder().build();
final CountDownLatch stop = new CountDownLatch(1);
final Rpc rpc = new Rpc();
service.testC(rpc, request, new RpcCallback<Type2>() {
@Override public void run(Type2 result) {
assertThat(rpc.failed(), is(true));
assertThat(rpc.errorText(), is("connection closed"));
stop.countDown();
}
});
server.interrupt();
stop.await();
}
}
|