Skip to content

Commit 7b2f034

Browse files
test: fix test connection resource warnings
Close all connection opened in tests even in case of failed asserts. Part of #250
1 parent b3412e8 commit 7b2f034

File tree

3 files changed

+227
-188
lines changed

3 files changed

+227
-188
lines changed

test/suites/test_dml.py

+25-22
Original file line numberDiff line numberDiff line change
@@ -167,29 +167,32 @@ def test_06_update(self):
167167

168168
def test_07_call_16(self):
169169
con = tarantool.Connection(self.srv.host, self.srv.args['primary'], call_16 = True)
170-
con.authenticate('test', 'test')
171-
self.assertSequenceEqual(con.call('json.decode', '[123, 234, 345]'), [[123, 234, 345]])
172-
self.assertSequenceEqual(con.call('json.decode', ['[123, 234, 345]']), [[123, 234, 345]])
173-
self.assertSequenceEqual(con.call('json.decode', ('[123, 234, 345]',)), [[123, 234, 345]])
174-
with self.assertRaisesRegex(tarantool.DatabaseError, '(32, .*)'):
175-
con.call('json.decode')
176-
with self.assertRaisesRegex(tarantool.DatabaseError, '(32, .*)'):
177-
con.call('json.decode', '{[1, 2]: "world"}')
178-
ans = con.call('fiber.time')
179-
self.assertEqual(len(ans), 1)
180-
self.assertEqual(len(ans[0]), 1)
181-
self.assertIsInstance(ans[0][0], float)
182-
ans = con.call('fiber.time64')
183-
self.assertEqual(len(ans), 1)
184-
self.assertEqual(len(ans[0]), 1)
185-
self.assertIsInstance(ans[0][0], int)
186-
ans = con.call('uuid.str')
187-
self.assertEqual(len(ans), 1)
188-
self.assertEqual(len(ans[0]), 1)
189-
self.assertIsInstance(ans[0][0], str)
170+
try:
171+
con.authenticate('test', 'test')
172+
self.assertSequenceEqual(con.call('json.decode', '[123, 234, 345]'), [[123, 234, 345]])
173+
self.assertSequenceEqual(con.call('json.decode', ['[123, 234, 345]']), [[123, 234, 345]])
174+
self.assertSequenceEqual(con.call('json.decode', ('[123, 234, 345]',)), [[123, 234, 345]])
175+
with self.assertRaisesRegex(tarantool.DatabaseError, '(32, .*)'):
176+
con.call('json.decode')
177+
with self.assertRaisesRegex(tarantool.DatabaseError, '(32, .*)'):
178+
con.call('json.decode', '{[1, 2]: "world"}')
179+
ans = con.call('fiber.time')
180+
self.assertEqual(len(ans), 1)
181+
self.assertEqual(len(ans[0]), 1)
182+
self.assertIsInstance(ans[0][0], float)
183+
ans = con.call('fiber.time64')
184+
self.assertEqual(len(ans), 1)
185+
self.assertEqual(len(ans[0]), 1)
186+
self.assertIsInstance(ans[0][0], int)
187+
ans = con.call('uuid.str')
188+
self.assertEqual(len(ans), 1)
189+
self.assertEqual(len(ans[0]), 1)
190+
self.assertIsInstance(ans[0][0], str)
190191

191-
self.assertSequenceEqual(con.call('box.tuple.new', [1, 2, 3, 'fld_1']), [[1, 2, 3, 'fld_1']])
192-
self.assertSequenceEqual(con.call('box.tuple.new', 'fld_1'), [['fld_1']])
192+
self.assertSequenceEqual(con.call('box.tuple.new', [1, 2, 3, 'fld_1']), [[1, 2, 3, 'fld_1']])
193+
self.assertSequenceEqual(con.call('box.tuple.new', 'fld_1'), [['fld_1']])
194+
finally:
195+
con.close()
193196

194197
def test_07_call_17(self):
195198
con = tarantool.Connection(self.srv.host, self.srv.args['primary'])

test/suites/test_mesh.py

+113-101
Original file line numberDiff line numberDiff line change
@@ -80,27 +80,30 @@ def assert_srv_id(con, srv_id):
8080
{'host': self.host_2, 'port': self.port_2},
8181
], user='test', password='test')
8282

83-
# Response from instance#1.
84-
assert_srv_id(con, 1)
85-
86-
# Stop instance#1 -- response from instance#2.
87-
self.srv.stop()
88-
assert_srv_id(con, 2)
89-
90-
# Start instance#1, stop instance#2 -- response from
91-
# instance#1 again.
92-
self.srv.start()
93-
self.srv.admin('function srv_id() return 1 end')
94-
self.srv2.stop()
95-
assert_srv_id(con, 1)
96-
97-
# Stop instance #2 -- NetworkError (because we have no
98-
# alive servers anymore).
99-
self.srv.stop()
100-
with self.assertRaises(NetworkError):
101-
with warnings.catch_warnings():
102-
warnings.simplefilter('ignore', category=NetworkWarning)
103-
con.ping()
83+
try:
84+
# Response from instance#1.
85+
assert_srv_id(con, 1)
86+
87+
# Stop instance#1 -- response from instance#2.
88+
self.srv.stop()
89+
assert_srv_id(con, 2)
90+
91+
# Start instance#1, stop instance#2 -- response from
92+
# instance#1 again.
93+
self.srv.start()
94+
self.srv.admin('function srv_id() return 1 end')
95+
self.srv2.stop()
96+
assert_srv_id(con, 1)
97+
98+
# Stop instance #2 -- NetworkError (because we have no
99+
# alive servers anymore).
100+
self.srv.stop()
101+
with self.assertRaises(NetworkError):
102+
with warnings.catch_warnings():
103+
warnings.simplefilter('ignore', category=NetworkWarning)
104+
con.ping()
105+
finally:
106+
con.close()
104107

105108
def test_01_contructor(self):
106109
# Verify that an error is risen when no addresses are
@@ -126,7 +129,10 @@ def test_01_contructor(self):
126129
addrs = [{"host": "localhost", "port": 1234}]
127130
con = tarantool.MeshConnection("localhost", 1234, addrs=addrs,
128131
connect_now=False)
129-
self.assertEqual(len(con.strategy.addrs), 1)
132+
try:
133+
self.assertEqual(len(con.strategy.addrs), 1)
134+
finally:
135+
con.close()
130136

131137
def test_02_discovery_bad_address(self):
132138
retvals = [
@@ -147,44 +153,46 @@ def test_02_discovery_bad_address(self):
147153
self.define_custom_cluster_function(func_name, retval)
148154
con = tarantool.MeshConnection(self.host_1, self.port_1,
149155
user='test', password='test')
150-
con.cluster_discovery_function = func_name
151-
152-
# Verify that a cluster discovery (that is triggered
153-
# by ping) give one or two warnings.
154-
with warnings.catch_warnings(record=True) as ws:
155-
con.ping()
156-
self.assertTrue(len(ws) in (1, 2))
157-
for w in ws:
158-
self.assertIs(w.category, ClusterDiscoveryWarning)
159-
160-
# Verify that incorrect or empty result was discarded.
161-
self.assertEqual(len(con.strategy.addrs), 1)
162-
self.assertEqual(con.strategy.addrs[0]['host'], self.host_1)
163-
self.assertEqual(con.strategy.addrs[0]['port'], self.port_1)
164-
165-
con.close()
156+
try:
157+
con.cluster_discovery_function = func_name
158+
159+
# Verify that a cluster discovery (that is triggered
160+
# by ping) give one or two warnings.
161+
with warnings.catch_warnings(record=True) as ws:
162+
con.ping()
163+
self.assertTrue(len(ws) in (1, 2))
164+
for w in ws:
165+
self.assertIs(w.category, ClusterDiscoveryWarning)
166+
167+
# Verify that incorrect or empty result was discarded.
168+
self.assertEqual(len(con.strategy.addrs), 1)
169+
self.assertEqual(con.strategy.addrs[0]['host'], self.host_1)
170+
self.assertEqual(con.strategy.addrs[0]['port'], self.port_1)
171+
finally:
172+
con.close()
166173

167174
def test_03_discovery_bad_good_addresses(self):
168175
func_name = 'bad_and_good_addresses'
169176
retval = "{'localhost:', '%s:%d'}" % (self.host_2, self.port_2)
170177
self.define_custom_cluster_function(func_name, retval)
171178
con = tarantool.MeshConnection(self.host_1, self.port_1,
172179
user='test', password='test')
173-
con.cluster_discovery_function = func_name
174-
175-
# Verify that a cluster discovery (that is triggered
176-
# by ping) give one warning.
177-
with warnings.catch_warnings(record=True) as ws:
178-
con.ping()
179-
self.assertEqual(len(ws), 1)
180-
self.assertIs(ws[0].category, ClusterDiscoveryWarning)
180+
try:
181+
con.cluster_discovery_function = func_name
181182

182-
# Verify that only second address was accepted.
183-
self.assertEqual(len(con.strategy.addrs), 1)
184-
self.assertEqual(con.strategy.addrs[0]['host'], self.host_2)
185-
self.assertEqual(con.strategy.addrs[0]['port'], self.port_2)
183+
# Verify that a cluster discovery (that is triggered
184+
# by ping) give one warning.
185+
with warnings.catch_warnings(record=True) as ws:
186+
con.ping()
187+
self.assertEqual(len(ws), 1)
188+
self.assertIs(ws[0].category, ClusterDiscoveryWarning)
186189

187-
con.close()
190+
# Verify that only second address was accepted.
191+
self.assertEqual(len(con.strategy.addrs), 1)
192+
self.assertEqual(con.strategy.addrs[0]['host'], self.host_2)
193+
self.assertEqual(con.strategy.addrs[0]['port'], self.port_2)
194+
finally:
195+
con.close()
188196

189197
def test_04_discovery_add_address(self):
190198
# Create a mesh connection; pass only the first server
@@ -194,20 +202,21 @@ def test_04_discovery_add_address(self):
194202
cluster_discovery_function=self.get_all_nodes_func_name,
195203
connect_now=False)
196204

197-
# Verify that the strategy has one address that comes from
198-
# the constructor arguments.
199-
self.assertEqual(len(con.strategy.addrs), 1)
200-
con.connect()
201-
202-
# Verify that we work with the first server.
203-
resp = con.call('srv_id')
204-
self.assertEqual(resp.data and resp.data[0], 1)
205+
try:
206+
# Verify that the strategy has one address that comes from
207+
# the constructor arguments.
208+
self.assertEqual(len(con.strategy.addrs), 1)
209+
con.connect()
205210

206-
# Verify that the refresh was successful and the strategy
207-
# has 2 addresses.
208-
self.assertEqual(len(con.strategy.addrs), 2)
211+
# Verify that we work with the first server.
212+
resp = con.call('srv_id')
213+
self.assertEqual(resp.data and resp.data[0], 1)
209214

210-
con.close()
215+
# Verify that the refresh was successful and the strategy
216+
# has 2 addresses.
217+
self.assertEqual(len(con.strategy.addrs), 2)
218+
finally:
219+
con.close()
211220

212221
def test_05_discovery_delay(self):
213222
# Create a mesh connection, pass only the first server address.
@@ -216,25 +225,26 @@ def test_05_discovery_delay(self):
216225
cluster_discovery_function=self.get_all_nodes_func_name,
217226
cluster_discovery_delay=1)
218227

219-
# Verify that the strategy has two addresses come from
220-
# the function right after connecting.
221-
self.assertEqual(len(con.strategy.addrs), 2)
222-
223-
# Drop addresses list to the initial state.
224-
con.strategy.update([con.strategy.addrs[0], ])
228+
try:
229+
# Verify that the strategy has two addresses come from
230+
# the function right after connecting.
231+
self.assertEqual(len(con.strategy.addrs), 2)
225232

226-
# Verify that the discovery will not be performed until
227-
# 'cluster_discovery_delay' seconds will be passed.
228-
con.ping()
229-
self.assertEqual(len(con.strategy.addrs), 1)
233+
# Drop addresses list to the initial state.
234+
con.strategy.update([con.strategy.addrs[0], ])
230235

231-
sleep(1.1)
236+
# Verify that the discovery will not be performed until
237+
# 'cluster_discovery_delay' seconds will be passed.
238+
con.ping()
239+
self.assertEqual(len(con.strategy.addrs), 1)
232240

233-
# Refresh after cluster_discovery_delay.
234-
con.ping()
235-
self.assertEqual(len(con.strategy.addrs), 2)
241+
sleep(1.1)
236242

237-
con.close()
243+
# Refresh after cluster_discovery_delay.
244+
con.ping()
245+
self.assertEqual(len(con.strategy.addrs), 2)
246+
finally:
247+
con.close()
238248

239249
def test_06_reconnection(self):
240250
# Create a mesh connection; pass only the first server
@@ -243,24 +253,25 @@ def test_06_reconnection(self):
243253
self.host_1, self.port_1, user='test', password='test',
244254
cluster_discovery_function=self.get_all_nodes_func_name)
245255

246-
con.last_nodes_refresh = 0
247-
resp = con.call('srv_id')
248-
self.assertEqual(resp.data and resp.data[0], 1)
249-
250-
# Verify that the last discovery was successful and the
251-
# strategy has 2 addresses.
252-
self.assertEqual(len(con.strategy.addrs), 2)
256+
try:
257+
con.last_nodes_refresh = 0
258+
resp = con.call('srv_id')
259+
self.assertEqual(resp.data and resp.data[0], 1)
253260

254-
self.srv.stop()
261+
# Verify that the last discovery was successful and the
262+
# strategy has 2 addresses.
263+
self.assertEqual(len(con.strategy.addrs), 2)
255264

256-
# Verify that we switched to the second server.
257-
with warnings.catch_warnings():
258-
# Suppress reconnection warnings.
259-
warnings.simplefilter("ignore")
260-
resp = con.call('srv_id')
261-
self.assertEqual(resp.data and resp.data[0], 2)
265+
self.srv.stop()
262266

263-
con.close()
267+
# Verify that we switched to the second server.
268+
with warnings.catch_warnings():
269+
# Suppress reconnection warnings.
270+
warnings.simplefilter("ignore")
271+
resp = con.call('srv_id')
272+
self.assertEqual(resp.data and resp.data[0], 2)
273+
finally:
274+
con.close()
264275

265276
def test_07_discovery_exclude_address(self):
266277
# Define function to get back only second server.
@@ -272,15 +283,16 @@ def test_07_discovery_exclude_address(self):
272283
self.host_1, self.port_1, user='test', password='test',
273284
cluster_discovery_function=func_name)
274285

275-
# Verify that discovery was successful and the strategy
276-
# has 1 address.
277-
self.assertEqual(len(con.strategy.addrs), 1)
278-
279-
# Verify that the current server is second one.
280-
resp = con.call('srv_id')
281-
self.assertEqual(resp.data and resp.data[0], 2)
286+
try:
287+
# Verify that discovery was successful and the strategy
288+
# has 1 address.
289+
self.assertEqual(len(con.strategy.addrs), 1)
282290

283-
con.close()
291+
# Verify that the current server is second one.
292+
resp = con.call('srv_id')
293+
self.assertEqual(resp.data and resp.data[0], 2)
294+
finally:
295+
con.close()
284296

285297
def tearDown(self):
286298
self.srv.stop()

0 commit comments

Comments
 (0)