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