@Override
    public HiveMetastoreClient createMetastoreClient(Optional<String> token)
            throws TException
    {
        List<HostAndPort> metastores = new ArrayList<>(addresses);
        if (metastoreLoadBalancingEnabled) {
            Collections.shuffle(metastores);
        }

        TException lastException = null;
        for (HostAndPort metastore : metastores) {
            try {
                HiveMetastoreClient client = clientFactory.create(metastore, token);

                if (!isNullOrEmpty(metastoreUsername)) {
                    client.setUGI(metastoreUsername);
                }
                return client;
            }
            catch (TException e) {
                lastException = e;
            }
        }
        throw new TException("Failed connecting to Hive metastore: " + addresses, lastException);
    }
@Test
    public void testFallbackHiveMetastoreWithHiveUser()
            throws TException
    {
        HiveCluster cluster = createHiveCluster(CONFIG_WITH_FALLBACK_WITH_USER, asList(null, null, FALLBACK_CLIENT));
        assertEquals(cluster.createMetastoreClient(Optional.empty()), FALLBACK_CLIENT);
    }