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.example;
20  
21  import org.apache.hadoop.conf.Configuration;
22  import org.apache.hadoop.hbase.HBaseTestingUtility;
23  import org.apache.hadoop.hbase.testclassification.MediumTests;
24  import org.apache.hadoop.hbase.TableName;
25  import org.apache.hadoop.hbase.client.HTable;
26  import org.apache.hadoop.hbase.client.Put;
27  import org.apache.hadoop.hbase.client.Table;
28  import org.apache.hadoop.hbase.client.coprocessor.Batch;
29  import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
30  import org.apache.hadoop.hbase.coprocessor.example.generated.ExampleProtos;
31  import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
32  import org.apache.hadoop.hbase.ipc.ServerRpcController;
33  import org.apache.hadoop.hbase.util.Bytes;
34  import org.junit.experimental.categories.Category;
35  
36  import java.io.IOException;
37  import java.util.Iterator;
38  import java.util.Map;
39  
40  import static junit.framework.Assert.*;
41  
42  /**
43   * Test case demonstrating client interactions with the {@link RowCountEndpoint}
44   * sample coprocessor Service implementation.
45   */
46  @Category(MediumTests.class)
47  public class TestRowCountEndpoint {
48    private static final TableName TEST_TABLE = TableName.valueOf("testrowcounter");
49    private static final byte[] TEST_FAMILY = Bytes.toBytes("f");
50    private static final byte[] TEST_COLUMN = Bytes.toBytes("col");
51  
52    private static HBaseTestingUtility TEST_UTIL = null;
53    private static Configuration CONF = null;
54  
55    // @Ignore @BeforeClass
56    public static void setupBeforeClass() throws Exception {
57      TEST_UTIL = new HBaseTestingUtility();
58      CONF = TEST_UTIL.getConfiguration();
59      CONF.setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
60          RowCountEndpoint.class.getName());
61  
62      TEST_UTIL.startMiniCluster();
63      TEST_UTIL.createTable(TEST_TABLE, new byte[][]{TEST_FAMILY});
64    }
65  
66    // @Ignore @AfterClass
67    public static void tearDownAfterClass() throws Exception {
68      TEST_UTIL.shutdownMiniCluster();
69    }
70  
71    // @Ignore @Test
72    public void testEndpoint() throws Throwable {
73      Table table = new HTable(CONF, TEST_TABLE);
74  
75      // insert some test rows
76      for (int i=0; i<5; i++) {
77        byte[] iBytes = Bytes.toBytes(i);
78        Put p = new Put(iBytes);
79        p.add(TEST_FAMILY, TEST_COLUMN, iBytes);
80        table.put(p);
81      }
82  
83      final ExampleProtos.CountRequest request = ExampleProtos.CountRequest.getDefaultInstance();
84      Map<byte[],Long> results = table.coprocessorService(ExampleProtos.RowCountService.class,
85          null, null,
86          new Batch.Call<ExampleProtos.RowCountService,Long>() {
87            public Long call(ExampleProtos.RowCountService counter) throws IOException {
88              ServerRpcController controller = new ServerRpcController();
89              BlockingRpcCallback<ExampleProtos.CountResponse> rpcCallback =
90                  new BlockingRpcCallback<ExampleProtos.CountResponse>();
91              counter.getRowCount(controller, request, rpcCallback);
92              ExampleProtos.CountResponse response = rpcCallback.get();
93              if (controller.failedOnException()) {
94                throw controller.getFailedOn();
95              }
96              return (response != null && response.hasCount()) ? response.getCount() : 0;
97            }
98          });
99      // should be one region with results
100     assertEquals(1, results.size());
101     Iterator<Long> iter = results.values().iterator();
102     Long val = iter.next();
103     assertNotNull(val);
104     assertEquals(5l, val.longValue());
105   }
106 
107 }