Esempio n. 1
0
    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)
Esempio n. 2
0
 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)
Esempio n. 3
0
    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)
Esempio n. 4
0
 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)
Esempio n. 5
0
    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)
Esempio n. 6
0
    def test_init_base_host_selector_class(self):
        """Test base initialization and functionality."""
        host_provider = HostsProvider([])
        base_host_selector = BaseHostSelector(host_provider)
        # Check that some base states are set.
        self.assertTrue(base_host_selector._last is None)
        self.assertTrue(base_host_selector._current is None)
        self.assertTrue(base_host_selector._select_time is None)
        self.assertEquals(base_host_selector._bad_hosts, {})
        self.assertEquals(base_host_selector._retry_time, 60)
        self.assertTrue(base_host_selector._host_provider is host_provider)

        # This is an abstract class. _chose_host() should raise an exception.
        self.assertRaises(NotImplementedError, base_host_selector._choose_host)
Esempio n. 7
0
    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)
Esempio n. 8
0
 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)
Esempio n. 9
0
    def test_init_base_host_selector_class(self):
        """Test base initialization and functionality."""

        fd, tmp_file = tempfile.mkstemp()
        host_provider = HostsProvider([], file_path=tmp_file)
        base_host_selector = BaseHostSelector(host_provider)
        # Check that some base states are set.
        self.assertTrue(base_host_selector._last is None)
        self.assertTrue(base_host_selector._current is None)
        self.assertTrue(base_host_selector._select_time is None)
        self.assertEquals(base_host_selector._bad_hosts, {})
        self.assertEquals(base_host_selector._retry_time, 60)
        self.assertTrue(base_host_selector._host_provider is host_provider)

        # This is an abstract class. _chose_host() should raise an exception.
        self.assertRaises(NotImplementedError, base_host_selector._choose_host)
        HostSelectorWithLocalFileTestCase.FILE_WATCH._clear_all_watches()
        os.remove(tmp_file)
Esempio n. 10
0
 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)