|
20 | 20 | import pytest |
21 | 21 | import pytest_asyncio |
22 | 22 |
|
23 | | -from acouchbase.cluster import get_event_loop |
| 23 | +from acouchbase.cluster import Cluster, get_event_loop |
| 24 | +from couchbase.auth import PasswordAuthenticator |
24 | 25 | from couchbase.diagnostics import (ClusterState, |
25 | 26 | EndpointPingReport, |
26 | 27 | EndpointState, |
|
30 | 31 | InvalidArgumentException, |
31 | 32 | ParsingFailedException, |
32 | 33 | QueryIndexNotFoundException) |
33 | | -from couchbase.options import DiagnosticsOptions, PingOptions |
| 34 | +from couchbase.options import (ClusterOptions, |
| 35 | + DiagnosticsOptions, |
| 36 | + PingOptions) |
34 | 37 | from couchbase.result import DiagnosticsResult, PingResult |
35 | 38 |
|
36 | 39 | from ._test_utils import TestEnvironment |
@@ -218,3 +221,139 @@ async def test_diagnostics_as_json(self, cb_env): |
218 | 221 | assert data[0]['remote'] is not None |
219 | 222 | assert data[0]['local'] is not None |
220 | 223 | assert data[0]['state'] is not None |
| 224 | + |
| 225 | + @pytest.mark.asyncio |
| 226 | + async def test_await_on_close_cluster(self, cb_env): |
| 227 | + conn_string = cb_env.config.get_connection_string() |
| 228 | + username, pw = cb_env.config.get_username_and_pw() |
| 229 | + auth = PasswordAuthenticator(username, pw) |
| 230 | + opts = ClusterOptions(auth) |
| 231 | + cluster = Cluster(conn_string, opts) |
| 232 | + assert cluster._impl.connected is False |
| 233 | + await cluster.close() |
| 234 | + assert cluster._impl.connected is False |
| 235 | + |
| 236 | + @pytest.mark.asyncio |
| 237 | + async def test_multiple_close_cluster(self, cb_env): |
| 238 | + conn_string = cb_env.config.get_connection_string() |
| 239 | + username, pw = cb_env.config.get_username_and_pw() |
| 240 | + auth = PasswordAuthenticator(username, pw) |
| 241 | + opts = ClusterOptions(auth) |
| 242 | + cluster = await Cluster.connect(conn_string, opts) |
| 243 | + assert cluster._impl.connected is True |
| 244 | + for _ in range(10): |
| 245 | + await cluster.close() |
| 246 | + assert cluster._impl.connected is False |
| 247 | + |
| 248 | + @pytest.mark.asyncio |
| 249 | + async def test_operations_after_close(self, cb_env): |
| 250 | + conn_string = cb_env.config.get_connection_string() |
| 251 | + username, pw = cb_env.config.get_username_and_pw() |
| 252 | + auth = PasswordAuthenticator(username, pw) |
| 253 | + opts = ClusterOptions(auth) |
| 254 | + cluster = await Cluster.connect(conn_string, opts) |
| 255 | + assert cluster._impl.connected is True |
| 256 | + |
| 257 | + # Close the cluster |
| 258 | + await cluster.close() |
| 259 | + assert cluster._impl.connected is False |
| 260 | + |
| 261 | + # Verify operations after close raise RuntimeError |
| 262 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 263 | + await cluster.ping() |
| 264 | + |
| 265 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 266 | + await cluster.diagnostics() |
| 267 | + |
| 268 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 269 | + cluster.bucket(cb_env.bucket.name) |
| 270 | + |
| 271 | + @pytest.mark.asyncio |
| 272 | + async def test_connected_property(self, cb_env): |
| 273 | + conn_string = cb_env.config.get_connection_string() |
| 274 | + username, pw = cb_env.config.get_username_and_pw() |
| 275 | + auth = PasswordAuthenticator(username, pw) |
| 276 | + opts = ClusterOptions(auth) |
| 277 | + cluster = await Cluster.connect(conn_string, opts) |
| 278 | + assert cluster._impl.connected is True |
| 279 | + |
| 280 | + # Close the cluster |
| 281 | + await cluster.close() |
| 282 | + |
| 283 | + assert cluster._impl.connected is False |
| 284 | + |
| 285 | + @pytest.mark.asyncio |
| 286 | + async def test_streaming_operations_after_close(self, cb_env): |
| 287 | + """Verify all streaming operations raise RuntimeError after cluster close""" |
| 288 | + from couchbase.search import MatchQuery |
| 289 | + |
| 290 | + conn_string = cb_env.config.get_connection_string() |
| 291 | + username, pw = cb_env.config.get_username_and_pw() |
| 292 | + auth = PasswordAuthenticator(username, pw) |
| 293 | + opts = ClusterOptions(auth) |
| 294 | + cluster = await Cluster.connect(conn_string, opts) |
| 295 | + |
| 296 | + # Close the cluster |
| 297 | + await cluster.close() |
| 298 | + |
| 299 | + # Test cluster.query |
| 300 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 301 | + cluster.query("SELECT 1=1") |
| 302 | + |
| 303 | + # Test cluster.analytics_query |
| 304 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 305 | + cluster.analytics_query("SELECT 1=1") |
| 306 | + |
| 307 | + # Test cluster.search |
| 308 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 309 | + cluster.search_query("dummy-index", MatchQuery("test")) |
| 310 | + |
| 311 | + # Test bucket.view_query |
| 312 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 313 | + bucket = cluster.bucket(cb_env.bucket.name) |
| 314 | + bucket.view_query("dummy-design", "dummy-view") |
| 315 | + |
| 316 | + @pytest.mark.flaky(reruns=5, reruns_delay=1) |
| 317 | + @pytest.mark.asyncio |
| 318 | + async def test_management_operations_after_close(self, cb_env): |
| 319 | + from couchbase.management.views import DesignDocumentNamespace |
| 320 | + |
| 321 | + conn_string = cb_env.config.get_connection_string() |
| 322 | + username, pw = cb_env.config.get_username_and_pw() |
| 323 | + auth = PasswordAuthenticator(username, pw) |
| 324 | + opts = ClusterOptions(auth) |
| 325 | + cluster = await Cluster.connect(conn_string, opts) |
| 326 | + bucket_name = cb_env.bucket.name |
| 327 | + |
| 328 | + # Get bucket instance before closing (for bucket-level manager tests) |
| 329 | + bucket = cluster.bucket(bucket_name) |
| 330 | + |
| 331 | + # Close the cluster |
| 332 | + await cluster.close() |
| 333 | + assert cluster._impl.connected is False |
| 334 | + |
| 335 | + # Test cluster-level management APIs |
| 336 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 337 | + await cluster.buckets().get_all_buckets() |
| 338 | + |
| 339 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 340 | + await cluster.users().get_all_users() |
| 341 | + |
| 342 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 343 | + await cluster.query_indexes().get_all_indexes(bucket_name) |
| 344 | + |
| 345 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 346 | + await cluster.analytics_indexes().get_all_datasets() |
| 347 | + |
| 348 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 349 | + await cluster.search_indexes().get_all_indexes() |
| 350 | + |
| 351 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 352 | + await cluster.eventing_functions().get_all_functions() |
| 353 | + |
| 354 | + # Test bucket-level management APIs |
| 355 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 356 | + await bucket.collections().get_all_scopes() |
| 357 | + |
| 358 | + with pytest.raises(RuntimeError, match="Cannot perform operations on a closed cluster"): |
| 359 | + await bucket.view_indexes().get_all_design_documents(DesignDocumentNamespace.DEVELOPMENT) |
0 commit comments