def test_random_host_selector_with_serverset(self): testutil.initialize_kazoo_client_manager(ZK_HOSTS) kazoo_client = KazooClientManager().get_client() kazoo_client.ensure_path(HostSelectorTestCase.SERVER_SET_PATH) host_provider = HostsProvider(HostSelectorTestCase.PORT_LIST, HostSelectorTestCase.SERVER_SET_PATH) self.assertTrue(host_provider.initialized) self.assertTrue(host_provider.hosts) # Since there is no live hosts in the server set, host provider should # still use the static host list. self.assertEqual(host_provider._current_host_tuple, host_provider._static_host_tuple) random_host_selector = RandomHostSelector( host_provider, expire_time=0, retry_time=0, invalidation_threshold=1.0) self.assertTrue(random_host_selector.get_host() in HostSelectorTestCase.PORT_LIST) server_set = ServerSet(HostSelectorTestCase.SERVER_SET_PATH, ZK_HOSTS) g = server_set.join(HostSelectorTestCase.PORT_LIST[0], use_ip=False) g.get() no_of_iterations = 100 # After the first endpoint joins, random host selector should only # start to use hosts in the server set. returned_hosts = [random_host_selector.get_host() for i in xrange(no_of_iterations)] self.assertEqual(len(set(returned_hosts)), 1) self.assertEqual(len(host_provider.hosts), 1) g = server_set.join(HostSelectorTestCase.PORT_LIST[1], use_ip=False) g.get() # After the second endpoint joins the server set, random host selector # should return both endpoints now. returned_hosts = [random_host_selector.get_host() for i in xrange(no_of_iterations)] self.assertEqual(len(set(returned_hosts)), 2) self.assertEqual(len(host_provider.hosts), 2)
def test_random_host_selector_with_serverset(self): testutil.initialize_kazoo_client_manager(ZK_HOSTS) kazoo_client = KazooClientManager().get_client() kazoo_client.ensure_path(HostSelectorTestCase.SERVER_SET_PATH) host_provider = HostsProvider(HostSelectorTestCase.PORT_LIST, HostSelectorTestCase.SERVER_SET_PATH) self.assertTrue(host_provider.initialized) self.assertTrue(host_provider.hosts) # Since there is no live hosts in the server set, host provider should # still use the static host list. self.assertEqual(host_provider._current_host_tuple, host_provider._static_host_tuple) random_host_selector = RandomHostSelector(host_provider, expire_time=0, retry_time=0, invalidation_threshold=1.0) self.assertTrue( random_host_selector.get_host() in HostSelectorTestCase.PORT_LIST) server_set = ServerSet(HostSelectorTestCase.SERVER_SET_PATH, ZK_HOSTS) g = server_set.join(HostSelectorTestCase.PORT_LIST[0], use_ip=False) g.get() no_of_iterations = 100 # After the first endpoint joins, random host selector should only # start to use hosts in the server set. returned_hosts = [ random_host_selector.get_host() for i in xrange(no_of_iterations) ] self.assertEqual(len(set(returned_hosts)), 1) self.assertEqual(len(host_provider.hosts), 1) g = server_set.join(HostSelectorTestCase.PORT_LIST[1], use_ip=False) g.get() # After the second endpoint joins the server set, random host selector # should return both endpoints now. returned_hosts = [ random_host_selector.get_host() for i in xrange(no_of_iterations) ] self.assertEqual(len(set(returned_hosts)), 2) self.assertEqual(len(host_provider.hosts), 2)