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