|
14 | 14 | parse_url, |
15 | 15 | ) |
16 | 16 | from valkey.asyncio import ConnectionPool, Valkey |
17 | | -from valkey.asyncio.connection import Connection, UnixDomainSocketConnection |
| 17 | +from valkey.asyncio.connection import ( |
| 18 | + Connection, |
| 19 | + SSLConnection, |
| 20 | + UnixDomainSocketConnection, |
| 21 | +) |
18 | 22 | from valkey.asyncio.retry import Retry |
19 | 23 | from valkey.backoff import NoBackoff |
20 | 24 | from valkey.exceptions import ConnectionError, InvalidResponse, TimeoutError |
@@ -494,3 +498,53 @@ async def test_connection_garbage_collection(request): |
494 | 498 |
|
495 | 499 | await client.aclose() |
496 | 500 | await pool.aclose() |
| 501 | + |
| 502 | + |
| 503 | +@pytest.mark.parametrize( |
| 504 | + "conn, error, expected_message", |
| 505 | + [ |
| 506 | + (SSLConnection(), OSError(), "Error connecting to localhost:6379."), |
| 507 | + (SSLConnection(), OSError(12), "Error 12 connecting to localhost:6379."), |
| 508 | + ( |
| 509 | + SSLConnection(), |
| 510 | + OSError(12, "Some Error"), |
| 511 | + "Error 12 connecting to localhost:6379. Some Error.", |
| 512 | + ), |
| 513 | + ( |
| 514 | + UnixDomainSocketConnection(path="unix:///tmp/valkey.sock"), |
| 515 | + OSError(), |
| 516 | + "Error connecting to unix:///tmp/valkey.sock.", |
| 517 | + ), |
| 518 | + ( |
| 519 | + UnixDomainSocketConnection(path="unix:///tmp/valkey.sock"), |
| 520 | + OSError(12), |
| 521 | + "Error 12 connecting to unix:///tmp/valkey.sock.", |
| 522 | + ), |
| 523 | + ( |
| 524 | + UnixDomainSocketConnection(path="unix:///tmp/valkey.sock"), |
| 525 | + OSError(12, "Some Error"), |
| 526 | + "Error 12 connecting to unix:///tmp/valkey.sock. Some Error.", |
| 527 | + ), |
| 528 | + ], |
| 529 | +) |
| 530 | +async def test_format_error_message(conn, error, expected_message): |
| 531 | + """Test that the _error_message function formats errors correctly""" |
| 532 | + error_message = conn._error_message(error) |
| 533 | + assert error_message == expected_message |
| 534 | + |
| 535 | + |
| 536 | +async def test_network_connection_failure(): |
| 537 | + with pytest.raises(ConnectionError) as e: |
| 538 | + valkey = Valkey(host="127.0.0.1", port=9999) |
| 539 | + await valkey.set("a", "b") |
| 540 | + assert str(e.value).startswith("Error 111 connecting to 127.0.0.1:9999. Connect") |
| 541 | + |
| 542 | + |
| 543 | +async def test_unix_socket_connection_failure(): |
| 544 | + with pytest.raises(ConnectionError) as e: |
| 545 | + valkey = Valkey(unix_socket_path="unix:///tmp/a.sock") |
| 546 | + await valkey.set("a", "b") |
| 547 | + assert ( |
| 548 | + str(e.value) |
| 549 | + == "Error 2 connecting to unix:///tmp/a.sock. No such file or directory." |
| 550 | + ) |
0 commit comments