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