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