示例#1
0
class TestHost(unittest.TestCase):
    def setUp(self):
        self.host = Host("foo.com")

    def test_url(self):
        self.assertEqual(self.host.url, "foo.com")

    def test_priority(self):
        # Default priority test
        self.assertEqual(self.host.priority, 0)

        host_with_less_priority = Host("foo.com", 5)
        # Specific priority
        self.assertEqual(host_with_less_priority.priority, 5)

    def test_up(self):
        self.assertEqual(self.host.up, True)

    def test_last_use(self):
        self.assertEqual(self.host.last_use, 0)

    def test_retry_count(self):
        self.assertEqual(self.host.retry_count, 0)

    def test_reset(self):
        self.host.up = False
        self.host.last_use = time.time()
        self.host.retry_count = 3

        self.host.reset()

        self.assertEqual(self.host.up, True)
        self.assertEqual(self.host.last_use, 0)
        self.assertEqual(self.host.retry_count, 0)
    def test_hosts_got_sorted(self):
        collection = HostsCollection([Host("a", 10), Host("b", 20), Host("c")])

        hosts = collection.read()

        self.assertEqual(hosts[0].url, "b")
        self.assertEqual(hosts[1].url, "a")
        self.assertEqual(hosts[2].url, "c")
    def test_hosts_got_sorted(self):
        collection = HostsCollection([Host('a', 10), Host('b', 20), Host('c')])

        hosts = collection.read()

        self.assertEqual(hosts[0].url, 'b')
        self.assertEqual(hosts[1].url, 'a')
        self.assertEqual(hosts[2].url, 'c')
    def build_hosts(self):
        # type: () -> HostsCollection

        return HostsCollection([
            Host('{}-dsn.algolia.net'.format(self.app_id), 10, CallType.READ),
            Host('{}.algolia.net'.format(self.app_id), 10, CallType.WRITE),
            Host('{}-1.algolianet.com'.format(self.app_id)),
            Host('{}-2.algolianet.com'.format(self.app_id)),
            Host('{}-3.algolianet.com'.format(self.app_id))
        ])
示例#5
0
    def hosts(app_id):
        # type: (str) -> HostsCollection

        hosts = [
            Host('{}-1.algolianet.com'.format(app_id)),
            Host('{}-2.algolianet.com'.format(app_id)),
            Host('{}-3.algolianet.com'.format(app_id))
        ]
        shuffle(hosts)

        return HostsCollection([hosts[0]])
示例#6
0
    def test_priority(self):
        # Default priority test
        self.assertEqual(self.host.priority, 0)

        host_with_less_priority = Host("foo.com", 5)
        # Specific priority
        self.assertEqual(host_with_less_priority.priority, 5)
示例#7
0
    def build_hosts(self):
        # type: () -> HostsCollection

        return HostsCollection([
            Host("{}.{}.{}".format("personalization", self._region,
                                   "algolia.com"))
        ])
示例#8
0
    def build_hosts(self):
        # type: () -> HostsCollection

        return HostsCollection([
            Host('{}.{}.{}'.format('recommendation', self._region,
                                   'algolia.com'))
        ])
示例#9
0
    def test_dns_timeout(self):
        config = SearchConfig(F.get_app_id(), F.get_api_key())

        config.hosts = HostsCollection([
            Host('algolia.biz', 10),
            Host('{}-1.algolianet.com'.format(F.get_app_id())),
            Host('{}-2.algolianet.com'.format(F.get_app_id())),
            Host('{}-3.algolianet.com'.format(F.get_app_id()))
        ])

        client = SearchClient.create_with_config(config)

        client.list_indices()
        # We test that the first Host `algolia.biz` is down.
        self.assertFalse(config.hosts.read()[0].up)
        self.assertTrue(config.hosts.read()[1].up)
        self.assertTrue(config.hosts.read()[2].up)
        self.assertTrue(config.hosts.read()[3].up)
    def test_down_hosts(self):
        a = Host('a', 10)
        b = Host('b', 20)
        c = Host('c')

        self.retry_strategy._now = mock.Mock(
            name="_now")
        self.retry_strategy._now.return_value = 1000

        hosts = list(self.retry_strategy.valid_hosts([a, b, c]))
        self.assertEqual(len(hosts), 3)

        a.last_use = 800.0  # 1000 - 800 = 200 (lower than TTL - 300)
        a.up = False
        hosts = list(self.retry_strategy.valid_hosts([a, b, c]))
        self.assertEqual(len(hosts), 2)  # still down

        a.last_use = 400.0  # 1000 - 400 = 600 (bigger than TTL - 300)
        hosts = list(self.retry_strategy.valid_hosts([a, b, c]))
        self.assertEqual(len(hosts), 3)  # got up
    def test_down_hosts(self):
        a = Host('a', 10)
        b = Host('b', 20)
        c = Host('c')

        self.retry_strategy._now = mock.Mock(name="_now")
        self.retry_strategy._now.return_value = 1000

        hosts = list(self.retry_strategy.valid_hosts([a, b, c]))
        self.assertEqual(len(hosts), 3)

        a.last_use = 800.0  # 1000 - 800 = 200 (lower than TTL - 300)
        a.up = False
        hosts = list(self.retry_strategy.valid_hosts([a, b, c]))
        self.assertEqual(len(hosts), 2)  # still down

        a.last_use = 400.0  # 1000 - 400 = 600 (bigger than TTL - 300)
        hosts = list(self.retry_strategy.valid_hosts([a, b, c]))
        self.assertEqual(len(hosts), 3)  # got up
示例#12
0
 def setUp(self):
     self.host = Host("foo.com")
 def setUp(self):
     self.time = time.time()
     self.retry_strategy = RetryStrategy()
     self.host = Host('foo.com')
     self.response = Response()
    def build_hosts(self):
        # type: () -> HostsCollection

        return HostsCollection(
            [Host('{}.{}.{}'.format('insights', self._region, 'algolia.io'))])
示例#15
0
 def setUp(self):
     self.host = Host('foo.com')
示例#16
0
    def build_hosts(self):
        # type: () -> HostsCollection

        return HostsCollection(
            [Host("{}.{}.{}".format("insights", self._region, "algolia.io"))])