|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | + |
| 3 | +import unittest |
| 4 | +import warnings |
| 5 | +import tarantool |
| 6 | +from .lib.tarantool_server import TarantoolServer |
| 7 | + |
| 8 | + |
| 9 | +class TestSuite_Reconnect(unittest.TestCase): |
| 10 | + @classmethod |
| 11 | + def setUpClass(self): |
| 12 | + print(' RECONNECT '.center(70, '=')) |
| 13 | + print('-' * 70) |
| 14 | + self.srv = TarantoolServer() |
| 15 | + self.srv.script = 'unit/suites/box.lua' |
| 16 | + |
| 17 | + def test_01_simple(self): |
| 18 | + # Create a connection, but don't connect it. |
| 19 | + con = tarantool.Connection('localhost', self.srv.args['primary'], |
| 20 | + connect_now=False) |
| 21 | + |
| 22 | + # Trigger a reconnection due to server unavailability. |
| 23 | + with warnings.catch_warnings(): |
| 24 | + warnings.simplefilter("ignore") |
| 25 | + with self.assertRaises(tarantool.error.NetworkError): |
| 26 | + con.ping() |
| 27 | + |
| 28 | + # Start a server and verify that the reconnection |
| 29 | + # succeeds. |
| 30 | + self.srv.start() |
| 31 | + self.assertIs(con.ping(notime=True), "Success") |
| 32 | + |
| 33 | + # Close the connection and stop the server. |
| 34 | + con.close() |
| 35 | + self.srv.stop() |
| 36 | + |
| 37 | + def test_02_wrong_auth(self): |
| 38 | + # Create a connection with wrong credentials, but don't |
| 39 | + # connect it. |
| 40 | + con = tarantool.Connection('localhost', self.srv.args['primary'], |
| 41 | + connect_now=False, user='not_exist') |
| 42 | + |
| 43 | + # Start a server. |
| 44 | + self.srv.start() |
| 45 | + |
| 46 | + # Trigger a reconnection due to wrong credentials. |
| 47 | + with warnings.catch_warnings(): |
| 48 | + warnings.simplefilter("ignore") |
| 49 | + with self.assertRaises(tarantool.error.DatabaseError): |
| 50 | + con.ping() |
| 51 | + |
| 52 | + # Set right credentials and verify that the reconnection |
| 53 | + # succeeds. |
| 54 | + con.user = None |
| 55 | + self.assertIs(con.ping(notime=True), "Success") |
| 56 | + |
| 57 | + # Close the connection and stop the server. |
| 58 | + con.close() |
| 59 | + self.srv.stop() |
| 60 | + |
| 61 | + @classmethod |
| 62 | + def tearDownClass(self): |
| 63 | + self.srv.clean() |
0 commit comments