1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.client;
19
20 import static org.junit.Assert.assertEquals;
21
22 import org.apache.hadoop.conf.Configuration;
23 import org.apache.hadoop.hbase.HBaseTestingUtility;
24 import org.apache.hadoop.hbase.MediumTests;
25 import org.apache.hadoop.hbase.ServerName;
26 import org.apache.hadoop.hbase.regionserver.HRegionServer;
27 import org.apache.hadoop.hbase.util.Bytes;
28 import org.junit.AfterClass;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.junit.experimental.categories.Category;
32 import org.mockito.Mockito;
33
34 @Category(MediumTests.class)
35 public class TestCoprocessorHConnection {
36
37 private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
38
39 @BeforeClass
40 public static void setupCluster() throws Exception {
41 UTIL.startMiniCluster();
42 }
43
44 @AfterClass
45 public static void shutdownCluster() throws Exception {
46 UTIL.shutdownMiniCluster();
47 }
48
49
50
51
52
53
54 @Test
55 public void testNonServerLocalLookup() throws Exception {
56 Configuration conf = UTIL.getConfiguration();
57
58 HRegionServer server = Mockito.mock(HRegionServer.class);
59 ServerName name = new ServerName("not.a.server.hostname", 12345, -1L);
60 Mockito.when(server.getServerName()).thenReturn(name);
61 CoprocessorHConnection connection = new CoprocessorHConnection(conf, server);
62
63
64 assertEquals("Didn't get the mock server from the connection", server,
65 connection.getHRegionConnection(name.getHostname(), name.getPort()));
66
67
68 byte[] tableName = Bytes.toBytes("testNonServerLocalLookup");
69 byte[] family = Bytes.toBytes("family");
70 UTIL.createTable(tableName, family);
71
72
73 HTable table = new HTable(tableName, connection);
74 Put p = new Put(Bytes.toBytes("row"));
75 p.add(family, null, null);
76 table.put(p);
77 table.flushCommits();
78
79
80
81 table.close();
82 connection.close();
83 }
84
85 @Test
86 public void testLocalServerLookup() throws Exception {
87 Configuration conf = UTIL.getConfiguration();
88
89 HRegionServer server =
90 UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().get(0).getRegionServer();
91
92 CoprocessorHConnection connection = new CoprocessorHConnection(conf, server);
93
94
95 byte[] tableName = Bytes.toBytes("testLocalServerLookup");
96 byte[] family = Bytes.toBytes("family");
97 UTIL.createTable(tableName, family);
98
99
100 HTable table = new HTable(tableName, connection);
101 Put p = new Put(Bytes.toBytes("row"));
102 p.add(family, null, null);
103 table.put(p);
104 table.flushCommits();
105
106
107 ServerName name = server.getServerName();
108 assertEquals("Didn't get the expected server from the connection", server,
109 connection.getHRegionConnection(name.getHostname(), name.getPort()));
110
111
112 table.close();
113 connection.close();
114 }
115 }