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.client;
20  
21  import static org.junit.Assert.fail;
22  
23  import org.apache.hadoop.hbase.HBaseTestingUtility;
24  import org.apache.hadoop.hbase.HConstants;
25  import org.apache.hadoop.hbase.ServerName;
26  import org.apache.hadoop.hbase.testclassification.ClientTests;
27  import org.apache.hadoop.hbase.testclassification.MediumTests;
28  import org.junit.AfterClass;
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  import java.net.UnknownHostException;
34  
35  /**
36   * Tests that we fail fast when hostname resolution is not working and do not cache
37   * unresolved InetSocketAddresses.
38   */
39  @Category({MediumTests.class, ClientTests.class})
40  public class TestConnectionImplementation {
41    private static HBaseTestingUtility testUtil;
42    private static ConnectionManager.HConnectionImplementation conn;
43  
44    @BeforeClass
45    public static void setupBeforeClass() throws Exception {
46      testUtil = HBaseTestingUtility.createLocalHTU();
47      testUtil.startMiniCluster();
48      conn = (ConnectionManager.HConnectionImplementation) testUtil.getConnection();
49    }
50  
51    @AfterClass
52    public static void teardownAfterClass() throws Exception {
53      conn.close();
54      testUtil.shutdownMiniCluster();
55    }
56  
57    @Test(expected = UnknownHostException.class)
58    public void testGetAdminBadHostname() throws Exception {
59      // verify that we can get an instance with the cluster hostname
60      ServerName master = testUtil.getHBaseCluster().getMaster().getServerName();
61      try {
62        conn.getAdmin(master);
63      } catch (UnknownHostException uhe) {
64        fail("Obtaining admin to the cluster master should have succeeded");
65      }
66  
67      // test that we fail to get a client to an unresolvable hostname, which
68      // means it won't be cached
69      ServerName badHost =
70          ServerName.valueOf("unknownhost.example.com:" + HConstants.DEFAULT_MASTER_PORT,
71          System.currentTimeMillis());
72      conn.getAdmin(badHost);
73      fail("Obtaining admin to unresolvable hostname should have failed");
74    }
75  
76    @Test(expected = UnknownHostException.class)
77    public void testGetClientBadHostname() throws Exception {
78      // verify that we can get an instance with the cluster hostname
79      ServerName rs = testUtil.getHBaseCluster().getRegionServer(0).getServerName();
80      try {
81        conn.getClient(rs);
82      } catch (UnknownHostException uhe) {
83        fail("Obtaining client to the cluster regionserver should have succeeded");
84      }
85  
86      // test that we fail to get a client to an unresolvable hostname, which
87      // means it won't be cached
88      ServerName badHost =
89          ServerName.valueOf("unknownhost.example.com:" + HConstants.DEFAULT_REGIONSERVER_PORT,
90          System.currentTimeMillis());
91      conn.getAdmin(badHost);
92      fail("Obtaining client to unresolvable hostname should have failed");
93    }
94  }