summaryrefslogtreecommitdiff
path: root/src/test/java/com/orbekk/protobuf/ProtobufFunctionalTest.java
blob: 1605b28b5b688fabbc942a02c87dd462bdcd2215 (plain)
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
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();
    }
}