def test_reject_invalidation(self): """Test rejecting invalidation.""" fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w') as f: f.write('\n'.join(HostSelectorWithLocalFileTestCase.HOST_LIST)) host_provider = HostsProvider(HostSelectorWithLocalFileTestCase.HOST_LIST, file_path=tmp_file) base_host_selector = BaseHostSelector(host_provider, expire_time=0, retry_time=0) with patch(hosts.__name__ + ".BaseHostSelector._choose_host", new=Mock(return_value=HostSelectorWithLocalFileTestCase.HOST_LIST[0])): # Get one host. host1 = base_host_selector.get_host() self.assertEquals(host1, HostSelectorWithLocalFileTestCase.HOST_LIST[0]) # If invalidated the state of the object changes. self.assertTrue(host1 not in base_host_selector._bad_hosts) base_host_selector.invalidate() # Because 1 is larger than 2 * 0.2 = 0.4 self.assertTrue(host1 not in base_host_selector._bad_hosts) base_host_selector._invalidation_threshold = 0.5 host1 = base_host_selector.get_host() self.assertEquals(host1, HostSelectorWithLocalFileTestCase.HOST_LIST[0]) base_host_selector.invalidate() # Because 1 <= 2 * 0.5 = 1.0 self.assertTrue(host1 in base_host_selector._bad_hosts) HostSelectorWithLocalFileTestCase.FILE_WATCH._clear_all_watches() os.remove(tmp_file)
def test_retrieving_and_invalidation(self): """Test host retrieval.""" host_provider = HostsProvider(HostSelectorTestCase.HOST_LIST) base_host_selector = BaseHostSelector( host_provider, expire_time=0, retry_time=0, invalidation_threshold=1.0) self.assertTrue(base_host_selector.get_last_host() is None) with patch(hosts.__name__ + ".BaseHostSelector._choose_host", new=Mock(return_value=HostSelectorTestCase.HOST_LIST[0])): # Get one host. host1 = base_host_selector.get_host() self.assertEquals(host1, HostSelectorTestCase.HOST_LIST[0]) # If invalidated the state of the object changes. self.assertTrue(host1 not in base_host_selector._bad_hosts) base_host_selector.invalidate() self.assertTrue(host1 in base_host_selector._bad_hosts) # If called again, with retry_time being set to 0 bad hosts should be # invalidated. with patch(hosts.__name__ + ".BaseHostSelector._choose_host", new=Mock(return_value=HostSelectorTestCase.HOST_LIST[1])): host2 = base_host_selector.get_host() # Now bad hosts should be empty self.assertTrue(not base_host_selector._bad_hosts) self.assertEquals(host2, HostSelectorTestCase.HOST_LIST[1]) base_host_selector.invalidate() self.assertTrue(host2 in base_host_selector._bad_hosts)
def test_retrieving_and_invalidation(self): """Test host retrieval.""" fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w') as f: f.write('\n'.join(HostSelectorWithLocalFileTestCase.HOST_LIST)) host_provider = HostsProvider(HostSelectorWithLocalFileTestCase.HOST_LIST, file_path=tmp_file) base_host_selector = BaseHostSelector( host_provider, expire_time=0, retry_time=0, invalidation_threshold=1.0) self.assertTrue(base_host_selector.get_last_host() is None) with patch(hosts.__name__ + ".BaseHostSelector._choose_host", new=Mock(return_value=HostSelectorWithLocalFileTestCase.HOST_LIST[0])): # Get one host. host1 = base_host_selector.get_host() self.assertEquals(host1, HostSelectorWithLocalFileTestCase.HOST_LIST[0]) # If invalidated the state of the object changes. self.assertTrue(host1 not in base_host_selector._bad_hosts) base_host_selector.invalidate() self.assertTrue(host1 in base_host_selector._bad_hosts) # If called again, with retry_time being set to 0 bad hosts should be # invalidated. with patch(hosts.__name__ + ".BaseHostSelector._choose_host", new=Mock(return_value=HostSelectorWithLocalFileTestCase.HOST_LIST[1])): host2 = base_host_selector.get_host() # Now bad hosts should be empty self.assertTrue(not base_host_selector._bad_hosts) self.assertEquals(host2, HostSelectorWithLocalFileTestCase.HOST_LIST[1]) base_host_selector.invalidate() self.assertTrue(host2 in base_host_selector._bad_hosts) HostSelectorWithLocalFileTestCase.FILE_WATCH._clear_all_watches() os.remove(tmp_file)
def test_retrieving_and_invalidation(self): """Test host retrieval.""" host_provider = HostsProvider(HostSelectorTestCase.HOST_LIST) base_host_selector = BaseHostSelector(host_provider, expire_time=0, retry_time=0, invalidation_threshold=1.0) self.assertTrue(base_host_selector.get_last_host() is None) with patch(hosts.__name__ + ".BaseHostSelector._choose_host", new=Mock(return_value=HostSelectorTestCase.HOST_LIST[0])): # Get one host. host1 = base_host_selector.get_host() self.assertEquals(host1, HostSelectorTestCase.HOST_LIST[0]) # If invalidated the state of the object changes. self.assertTrue(host1 not in base_host_selector._bad_hosts) base_host_selector.invalidate() self.assertTrue(host1 in base_host_selector._bad_hosts) # If called again, with retry_time being set to 0 bad hosts should be # invalidated. with patch(hosts.__name__ + ".BaseHostSelector._choose_host", new=Mock(return_value=HostSelectorTestCase.HOST_LIST[1])): host2 = base_host_selector.get_host() # Now bad hosts should be empty self.assertTrue(not base_host_selector._bad_hosts) self.assertEquals(host2, HostSelectorTestCase.HOST_LIST[1]) base_host_selector.invalidate() self.assertTrue(host2 in base_host_selector._bad_hosts)
def test_reject_invalidation(self): """Test rejecting invalidation.""" fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w') as f: f.write('\n'.join(HostSelectorWithLocalFileTestCase.HOST_LIST)) host_provider = HostsProvider( HostSelectorWithLocalFileTestCase.HOST_LIST, file_path=tmp_file) base_host_selector = BaseHostSelector(host_provider, expire_time=0, retry_time=0) with patch(hosts.__name__ + ".BaseHostSelector._choose_host", new=Mock(return_value=HostSelectorWithLocalFileTestCase. HOST_LIST[0])): # Get one host. host1 = base_host_selector.get_host() self.assertEquals(host1, HostSelectorWithLocalFileTestCase.HOST_LIST[0]) # If invalidated the state of the object changes. self.assertTrue(host1 not in base_host_selector._bad_hosts) base_host_selector.invalidate() # Because 1 is larger than 2 * 0.2 = 0.4 self.assertTrue(host1 not in base_host_selector._bad_hosts) base_host_selector._invalidation_threshold = 0.5 host1 = base_host_selector.get_host() self.assertEquals(host1, HostSelectorWithLocalFileTestCase.HOST_LIST[0]) base_host_selector.invalidate() # Because 1 <= 2 * 0.5 = 1.0 self.assertTrue(host1 in base_host_selector._bad_hosts) HostSelectorWithLocalFileTestCase.FILE_WATCH._clear_all_watches() os.remove(tmp_file)
def test_reject_invalidation(self): """Test rejecting invalidation.""" host_provider = HostsProvider(HostSelectorTestCase.HOST_LIST) base_host_selector = BaseHostSelector(host_provider, expire_time=0, retry_time=0) with patch(hosts.__name__ + ".BaseHostSelector._choose_host", new=Mock(return_value=HostSelectorTestCase.HOST_LIST[0])): # Get one host. host1 = base_host_selector.get_host() self.assertEquals(host1, HostSelectorTestCase.HOST_LIST[0]) # If invalidated the state of the object changes. self.assertTrue(host1 not in base_host_selector._bad_hosts) base_host_selector.invalidate() # Because 1 is larger than 2 * 0.2 = 0.4 self.assertTrue(host1 not in base_host_selector._bad_hosts) base_host_selector._invalidation_threshold = 0.5 host1 = base_host_selector.get_host() self.assertEquals(host1, HostSelectorTestCase.HOST_LIST[0]) base_host_selector.invalidate() # Because 1 <= 2 * 0.5 = 1.0 self.assertTrue(host1 in base_host_selector._bad_hosts)
def test_retrieving_and_invalidation(self): """Test host retrieval.""" fd, tmp_file = tempfile.mkstemp() with open(tmp_file, 'w') as f: f.write('\n'.join(HostSelectorWithLocalFileTestCase.HOST_LIST)) host_provider = HostsProvider( HostSelectorWithLocalFileTestCase.HOST_LIST, file_path=tmp_file) base_host_selector = BaseHostSelector(host_provider, expire_time=0, retry_time=0, invalidation_threshold=1.0) self.assertTrue(base_host_selector.get_last_host() is None) with patch(hosts.__name__ + ".BaseHostSelector._choose_host", new=Mock(return_value=HostSelectorWithLocalFileTestCase. HOST_LIST[0])): # Get one host. host1 = base_host_selector.get_host() self.assertEquals(host1, HostSelectorWithLocalFileTestCase.HOST_LIST[0]) # If invalidated the state of the object changes. self.assertTrue(host1 not in base_host_selector._bad_hosts) base_host_selector.invalidate() self.assertTrue(host1 in base_host_selector._bad_hosts) # If called again, with retry_time being set to 0 bad hosts should be # invalidated. with patch(hosts.__name__ + ".BaseHostSelector._choose_host", new=Mock(return_value=HostSelectorWithLocalFileTestCase. HOST_LIST[1])): host2 = base_host_selector.get_host() # Now bad hosts should be empty self.assertTrue(not base_host_selector._bad_hosts) self.assertEquals(host2, HostSelectorWithLocalFileTestCase.HOST_LIST[1]) base_host_selector.invalidate() self.assertTrue(host2 in base_host_selector._bad_hosts) HostSelectorWithLocalFileTestCase.FILE_WATCH._clear_all_watches() os.remove(tmp_file)