@@ -80,27 +80,30 @@ def assert_srv_id(con, srv_id):
80
80
{'host' : self .host_2 , 'port' : self .port_2 },
81
81
], user = 'test' , password = 'test' )
82
82
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 ()
104
107
105
108
def test_01_contructor (self ):
106
109
# Verify that an error is risen when no addresses are
@@ -126,7 +129,10 @@ def test_01_contructor(self):
126
129
addrs = [{"host" : "localhost" , "port" : 1234 }]
127
130
con = tarantool .MeshConnection ("localhost" , 1234 , addrs = addrs ,
128
131
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 ()
130
136
131
137
def test_02_discovery_bad_address (self ):
132
138
retvals = [
@@ -147,44 +153,46 @@ def test_02_discovery_bad_address(self):
147
153
self .define_custom_cluster_function (func_name , retval )
148
154
con = tarantool .MeshConnection (self .host_1 , self .port_1 ,
149
155
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 ()
166
173
167
174
def test_03_discovery_bad_good_addresses (self ):
168
175
func_name = 'bad_and_good_addresses'
169
176
retval = "{'localhost:', '%s:%d'}" % (self .host_2 , self .port_2 )
170
177
self .define_custom_cluster_function (func_name , retval )
171
178
con = tarantool .MeshConnection (self .host_1 , self .port_1 ,
172
179
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
181
182
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 )
186
189
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 ()
188
196
189
197
def test_04_discovery_add_address (self ):
190
198
# Create a mesh connection; pass only the first server
@@ -194,20 +202,21 @@ def test_04_discovery_add_address(self):
194
202
cluster_discovery_function = self .get_all_nodes_func_name ,
195
203
connect_now = False )
196
204
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 ()
205
210
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 )
209
214
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 ()
211
220
212
221
def test_05_discovery_delay (self ):
213
222
# Create a mesh connection, pass only the first server address.
@@ -216,25 +225,26 @@ def test_05_discovery_delay(self):
216
225
cluster_discovery_function = self .get_all_nodes_func_name ,
217
226
cluster_discovery_delay = 1 )
218
227
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 )
225
232
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 ], ])
230
235
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 )
232
240
233
- # Refresh after cluster_discovery_delay.
234
- con .ping ()
235
- self .assertEqual (len (con .strategy .addrs ), 2 )
241
+ sleep (1.1 )
236
242
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 ()
238
248
239
249
def test_06_reconnection (self ):
240
250
# Create a mesh connection; pass only the first server
@@ -243,24 +253,25 @@ def test_06_reconnection(self):
243
253
self .host_1 , self .port_1 , user = 'test' , password = 'test' ,
244
254
cluster_discovery_function = self .get_all_nodes_func_name )
245
255
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 )
253
260
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 )
255
264
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 ()
262
266
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 ()
264
275
265
276
def test_07_discovery_exclude_address (self ):
266
277
# Define function to get back only second server.
@@ -272,15 +283,16 @@ def test_07_discovery_exclude_address(self):
272
283
self .host_1 , self .port_1 , user = 'test' , password = 'test' ,
273
284
cluster_discovery_function = func_name )
274
285
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 )
282
290
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 ()
284
296
285
297
def tearDown (self ):
286
298
self .srv .stop ()
0 commit comments