1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.coprocessor;
20
21 import static org.junit.Assert.assertEquals;
22
23 import java.io.IOException;
24
25 import org.apache.hadoop.conf.Configuration;
26 import org.apache.hadoop.hbase.Coprocessor;
27 import org.apache.hadoop.hbase.CoprocessorEnvironment;
28 import org.apache.hadoop.hbase.HBaseTestingUtility;
29 import org.apache.hadoop.hbase.testclassification.MediumTests;
30 import org.apache.hadoop.hbase.ServerName;
31 import org.apache.hadoop.hbase.client.HBaseAdmin;
32 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.DummyRegionServerEndpointProtos;
33 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.DummyRegionServerEndpointProtos.DummyRequest;
34 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.DummyRegionServerEndpointProtos.DummyResponse;
35 import org.apache.hadoop.hbase.coprocessor.protobuf.generated.DummyRegionServerEndpointProtos.DummyService;
36 import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
37 import org.apache.hadoop.hbase.ipc.ServerRpcController;
38 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
39 import org.junit.AfterClass;
40 import org.junit.BeforeClass;
41 import org.junit.Test;
42 import org.junit.experimental.categories.Category;
43 import com.google.protobuf.RpcCallback;
44 import com.google.protobuf.RpcController;
45 import com.google.protobuf.Service;
46
47 @Category(MediumTests.class)
48 public class TestRegionServerCoprocessorEndpoint {
49 private static HBaseTestingUtility TEST_UTIL = null;
50 private static Configuration CONF = null;
51 private static final String DUMMY_VALUE = "val";
52
53 @BeforeClass
54 public static void setupBeforeClass() throws Exception {
55 TEST_UTIL = new HBaseTestingUtility();
56 CONF = TEST_UTIL.getConfiguration();
57 CONF.setStrings(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
58 DummyRegionServerEndpoint.class.getName());
59 TEST_UTIL.startMiniCluster();
60 }
61
62 @AfterClass
63 public static void tearDownAfterClass() throws Exception {
64 TEST_UTIL.shutdownMiniCluster();
65 }
66
67 @Test
68 public void testEndpoint() throws Exception {
69 final ServerName serverName = TEST_UTIL.getHBaseCluster().getRegionServer(0).getServerName();
70 final ServerRpcController controller = new ServerRpcController();
71 final BlockingRpcCallback<DummyRegionServerEndpointProtos.DummyResponse> rpcCallback =
72 new BlockingRpcCallback<DummyRegionServerEndpointProtos.DummyResponse>();
73 DummyRegionServerEndpointProtos.DummyService service =
74 ProtobufUtil.newServiceStub(DummyRegionServerEndpointProtos.DummyService.class,
75 new HBaseAdmin(CONF).coprocessorService(serverName));
76 service.dummyCall(controller,
77 DummyRegionServerEndpointProtos.DummyRequest.getDefaultInstance(), rpcCallback);
78 assertEquals(DUMMY_VALUE, rpcCallback.get().getValue());
79 if (controller.failedOnException()) {
80 throw controller.getFailedOn();
81 }
82 }
83
84 static class DummyRegionServerEndpoint extends DummyService implements Coprocessor, SingletonCoprocessorService {
85
86 @Override
87 public Service getService() {
88 return this;
89 }
90
91 @Override
92 public void start(CoprocessorEnvironment env) throws IOException {
93
94 }
95
96 @Override
97 public void stop(CoprocessorEnvironment env) throws IOException {
98
99 }
100
101 @Override
102 public void dummyCall(RpcController controller, DummyRequest request,
103 RpcCallback<DummyResponse> callback) {
104 callback.run(DummyResponse.newBuilder().setValue(DUMMY_VALUE).build());
105 }
106 }
107 }