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 java.io.IOException;
21
22 import org.apache.hadoop.conf.Configuration;
23 import org.apache.hadoop.hbase.RegionLocations;
24 import org.apache.hadoop.hbase.TableName;
25 import org.apache.hadoop.hbase.HRegionInfo;
26 import org.apache.hadoop.hbase.HRegionLocation;
27 import org.apache.hadoop.hbase.ServerName;
28 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
29 import org.apache.hadoop.hbase.protobuf.generated.AdminProtos;
30 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
31 import org.apache.hadoop.hbase.client.ConnectionManager.HConnectionImplementation;
32 import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
33 import org.mockito.Mockito;
34
35
36
37
38 public class HConnectionTestingUtility {
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 public static ClusterConnection getMockedConnection(final Configuration conf)
56 throws ZooKeeperConnectionException {
57 HConnectionKey connectionKey = new HConnectionKey(conf);
58 synchronized (ConnectionManager.CONNECTION_INSTANCES) {
59 HConnectionImplementation connection =
60 ConnectionManager.CONNECTION_INSTANCES.get(connectionKey);
61 if (connection == null) {
62 connection = Mockito.mock(HConnectionImplementation.class);
63 Mockito.when(connection.getConfiguration()).thenReturn(conf);
64 ConnectionManager.CONNECTION_INSTANCES.put(connectionKey, connection);
65 }
66 return connection;
67 }
68 }
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 public static ClusterConnection getMockedConnectionAndDecorate(final Configuration conf,
100 final AdminProtos.AdminService.BlockingInterface admin,
101 final ClientProtos.ClientService.BlockingInterface client,
102 final ServerName sn, final HRegionInfo hri)
103 throws IOException {
104 HConnectionImplementation c = Mockito.mock(HConnectionImplementation.class);
105 Mockito.when(c.getConfiguration()).thenReturn(conf);
106 ConnectionManager.CONNECTION_INSTANCES.put(new HConnectionKey(conf), c);
107 Mockito.doNothing().when(c).close();
108
109 final HRegionLocation loc = new HRegionLocation(hri, sn);
110 Mockito.when(c.getRegionLocation((TableName) Mockito.any(),
111 (byte[]) Mockito.any(), Mockito.anyBoolean())).
112 thenReturn(loc);
113 Mockito.when(c.locateRegion((TableName) Mockito.any(), (byte[]) Mockito.any())).
114 thenReturn(loc);
115 Mockito.when(c.locateRegion((TableName) Mockito.any(), (byte[]) Mockito.any(),
116 Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyInt()))
117 .thenReturn(new RegionLocations(loc));
118 if (admin != null) {
119
120 Mockito.when(c.getAdmin(Mockito.any(ServerName.class))).
121 thenReturn(admin);
122 }
123 if (client != null) {
124
125 Mockito.when(c.getClient(Mockito.any(ServerName.class))).
126 thenReturn(client);
127 }
128 NonceGenerator ng = Mockito.mock(NonceGenerator.class);
129 Mockito.when(c.getNonceGenerator()).thenReturn(ng);
130 Mockito.when(c.getAsyncProcess()).thenReturn(
131 new AsyncProcess(c, conf, null, RpcRetryingCallerFactory.instantiate(conf), false,
132 RpcControllerFactory.instantiate(conf)));
133 Mockito.doNothing().when(c).incCount();
134 Mockito.doNothing().when(c).decCount();
135 Mockito.when(c.getNewRpcRetryingCallerFactory(conf)).thenReturn(
136 RpcRetryingCallerFactory.instantiate(conf,
137 RetryingCallerInterceptorFactory.NO_OP_INTERCEPTOR, null));
138 HTableInterface t = Mockito.mock(HTableInterface.class);
139 Mockito.when(c.getTable((TableName)Mockito.any())).thenReturn(t);
140 ResultScanner rs = Mockito.mock(ResultScanner.class);
141 Mockito.when(t.getScanner((Scan)Mockito.any())).thenReturn(rs);
142 return c;
143 }
144
145
146
147
148
149
150
151
152
153
154
155
156
157 public static ClusterConnection getSpiedConnection(final Configuration conf)
158 throws IOException {
159 HConnectionKey connectionKey = new HConnectionKey(conf);
160 synchronized (ConnectionManager.CONNECTION_INSTANCES) {
161 HConnectionImplementation connection =
162 ConnectionManager.CONNECTION_INSTANCES.get(connectionKey);
163 if (connection == null) {
164 connection = Mockito.spy(new HConnectionImplementation(conf, true));
165 ConnectionManager.CONNECTION_INSTANCES.put(connectionKey, connection);
166 }
167 return connection;
168 }
169 }
170
171 public static ClusterConnection getSpiedClusterConnection(final Configuration conf)
172 throws IOException {
173 HConnectionKey connectionKey = new HConnectionKey(conf);
174 synchronized (ConnectionManager.CONNECTION_INSTANCES) {
175 HConnectionImplementation connection =
176 ConnectionManager.CONNECTION_INSTANCES.get(connectionKey);
177 if (connection == null) {
178 connection = Mockito.spy(new HConnectionImplementation(conf, true));
179 ConnectionManager.CONNECTION_INSTANCES.put(connectionKey, connection);
180 }
181 return connection;
182 }
183 }
184
185
186
187
188 public static int getConnectionCount() {
189 synchronized (ConnectionManager.CONNECTION_INSTANCES) {
190 return ConnectionManager.CONNECTION_INSTANCES.size();
191 }
192 }
193 }