|
26 | 26 | import warnings
|
27 | 27 | import mock
|
28 | 28 |
|
29 |
| -from influxdb import InfluxDBClient |
| 29 | +from influxdb import InfluxDBClient, InfluxDBClusterClient |
| 30 | +from influxdb.client import InfluxDBServerError |
30 | 31 |
|
31 | 32 |
|
32 | 33 | def _build_response_object(status_code=200, content=""):
|
@@ -534,3 +535,97 @@ def connection_error(self, *args, **kwargs):
|
534 | 535 |
|
535 | 536 | with self.assertRaises(requests.exceptions.ConnectionError):
|
536 | 537 | cli.write_points(self.dummy_points)
|
| 538 | + |
| 539 | + |
| 540 | +class FakeClient(InfluxDBClient): |
| 541 | + fail = False |
| 542 | + |
| 543 | + def query(self, |
| 544 | + query, |
| 545 | + params={}, |
| 546 | + expected_response_code=200, |
| 547 | + database=None): |
| 548 | + if query == 'Fail': |
| 549 | + raise Exception("Fail") |
| 550 | + |
| 551 | + if self.fail: |
| 552 | + raise Exception("Fail") |
| 553 | + else: |
| 554 | + return "Success" |
| 555 | + |
| 556 | + |
| 557 | +class TestInfluxDBClusterClient(unittest.TestCase): |
| 558 | + |
| 559 | + def setUp(self): |
| 560 | + # By default, raise exceptions on warnings |
| 561 | + warnings.simplefilter('error', FutureWarning) |
| 562 | + |
| 563 | + self.hosts = [('host1', 8086), ('host2', 8086), ('host3', 8086)] |
| 564 | + |
| 565 | + def test_init(self): |
| 566 | + cluster = InfluxDBClusterClient(hosts=self.hosts, |
| 567 | + username='username', |
| 568 | + password='password', |
| 569 | + database='database', |
| 570 | + shuffle=False, |
| 571 | + client_base_class=FakeClient) |
| 572 | + assert len(cluster.clients) == 3 |
| 573 | + assert len(cluster.bad_clients) == 0 |
| 574 | + for idx, client in enumerate(cluster.clients): |
| 575 | + assert client._host == self.hosts[idx][0] |
| 576 | + assert client._port == self.hosts[idx][1] |
| 577 | + |
| 578 | + def test_one_server_fails(self): |
| 579 | + cluster = InfluxDBClusterClient(hosts=self.hosts, |
| 580 | + database='database', |
| 581 | + shuffle=False, |
| 582 | + client_base_class=FakeClient) |
| 583 | + cluster.clients[0].fail = True |
| 584 | + assert cluster.query('') == 'Success' |
| 585 | + assert len(cluster.clients) == 2 |
| 586 | + assert len(cluster.bad_clients) == 1 |
| 587 | + |
| 588 | + def test_two_servers_fail(self): |
| 589 | + cluster = InfluxDBClusterClient(hosts=self.hosts, |
| 590 | + database='database', |
| 591 | + shuffle=False, |
| 592 | + client_base_class=FakeClient) |
| 593 | + cluster.clients[0].fail = True |
| 594 | + cluster.clients[1].fail = True |
| 595 | + assert cluster.query('') == 'Success' |
| 596 | + assert len(cluster.clients) == 1 |
| 597 | + assert len(cluster.bad_clients) == 2 |
| 598 | + |
| 599 | + def test_all_fail(self): |
| 600 | + cluster = InfluxDBClusterClient(hosts=self.hosts, |
| 601 | + database='database', |
| 602 | + shuffle=True, |
| 603 | + client_base_class=FakeClient) |
| 604 | + try: |
| 605 | + cluster.query('Fail') |
| 606 | + except InfluxDBServerError: |
| 607 | + pass |
| 608 | + assert len(cluster.clients) == 0 |
| 609 | + assert len(cluster.bad_clients) == 3 |
| 610 | + |
| 611 | + def test_all_good(self): |
| 612 | + cluster = InfluxDBClusterClient(hosts=self.hosts, |
| 613 | + database='database', |
| 614 | + shuffle=True, |
| 615 | + client_base_class=FakeClient) |
| 616 | + assert cluster.query('') == 'Success' |
| 617 | + assert len(cluster.clients) == 3 |
| 618 | + assert len(cluster.bad_clients) == 0 |
| 619 | + |
| 620 | + def test_recovery(self): |
| 621 | + cluster = InfluxDBClusterClient(hosts=self.hosts, |
| 622 | + database='database', |
| 623 | + shuffle=True, |
| 624 | + client_base_class=FakeClient) |
| 625 | + try: |
| 626 | + cluster.query('Fail') |
| 627 | + except InfluxDBServerError: |
| 628 | + pass |
| 629 | + assert cluster.query('') == 'Success' |
| 630 | + assert len(cluster.clients) == 1 |
| 631 | + assert len(cluster.bad_clients) == 2 |
0 commit comments