Skip to content

Commit 145531d

Browse files
committed
test: add test cases for reconnections
Fixes #125.
1 parent 85b11ff commit 145531d

File tree

2 files changed

+74
-1
lines changed

2 files changed

+74
-1
lines changed

unit/suites/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
from .test_schema import TestSuite_Schema
88
from .test_dml import TestSuite_Request
99
from .test_protocol import TestSuite_Protocol
10+
from .test_reconnect import TestSuite_Reconnect
1011

11-
test_cases = (TestSuite_Schema, TestSuite_Request, TestSuite_Protocol)
12+
test_cases = (TestSuite_Schema, TestSuite_Request, TestSuite_Protocol,
13+
TestSuite_Reconnect)
1214

1315
def load_tests(loader, tests, pattern):
1416
suite = unittest.TestSuite()

unit/suites/test_reconnect.py

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

0 commit comments

Comments
 (0)