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