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

        @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() {
        Test.Type1 request = Test.Type1.newBuilder().build();
        int count = 10;
        final CountDownLatch stop = new CountDownLatch(count);
        service.testA(null, request, new RpcCallback<Type2>() {
            @Override public void run(Type2 result) {
                assertThat(result.getMessage(), is("TestA"));
                stop.countDown();
            }
        });
    }
}