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