View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one 
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and 
16   * limitations under the License.
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        // TODO Auto-generated method stub
94      }
95  
96      @Override
97      public void stop(CoprocessorEnvironment env) throws IOException {
98        // TODO Auto-generated method stub
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 }