-
-
Notifications
You must be signed in to change notification settings - Fork 216
Show host and db in Redis check __repr__ (with cluster support)
#663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
ba3b28b
2029ce4
ec4b6ce
56e256b
d1ff5bc
8223212
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -149,6 +149,67 @@ async def test_redis__validation_neither_param(self): | |
| ): | ||
| RedisHealthCheck() | ||
|
|
||
| def test_redis__repr_standard_client(self): | ||
| """Verify repr includes host and db for a standard Redis client.""" | ||
| from redis.asyncio import Redis as RedisClient | ||
|
|
||
| check = RedisHealthCheck( | ||
| client_factory=lambda: RedisClient(host="myhost", port=6379, db=2) | ||
| ) | ||
| assert repr(check) == "Redis(client=RedisClient(host=myhost, db=2))" | ||
|
|
||
| def test_redis__repr_from_url(self): | ||
| """Verify repr includes host and db when client is created via from_url.""" | ||
| from redis.asyncio import Redis as RedisClient | ||
|
|
||
| check = RedisHealthCheck( | ||
| client_factory=lambda: RedisClient.from_url( | ||
| "redis://cache.example.com:6379/3" | ||
| ) | ||
| ) | ||
| assert "host=cache.example.com" in repr(check), ( | ||
| "repr should include the host from the Redis URL" | ||
| ) | ||
| assert "db=3" in repr(check), ( | ||
| "repr should include the db from the Redis URL" | ||
| ) | ||
|
|
||
| def test_redis__repr_deprecated_client(self): | ||
| """Verify repr includes host and db when using deprecated client parameter.""" | ||
| from redis.asyncio import Redis as RedisClient | ||
|
|
||
| with pytest.warns(DeprecationWarning): | ||
| check = RedisHealthCheck(client=RedisClient(host="oldhost", port=6379, db=5)) | ||
| assert "host=oldhost" in repr(check), ( | ||
| "repr should include the host from the deprecated client" | ||
| ) | ||
| assert "db=5" in repr(check), ( | ||
| "repr should include the db from the deprecated client" | ||
| ) | ||
|
|
||
| def test_redis__repr_sentinel_client(self): | ||
| """Verify repr falls back gracefully for Sentinel clients without host/db.""" | ||
| from redis.asyncio import Sentinel | ||
|
|
||
| check = RedisHealthCheck( | ||
| client_factory=lambda: Sentinel([("localhost", 26379)]).master_for( | ||
| "mymaster" | ||
| ) | ||
| ) | ||
| # Sentinel clients don't expose host/db in connection_pool.connection_kwargs | ||
| # __repr__ should fall back to the default dataclass repr without raising | ||
| assert repr(check) == "Redis()" | ||
|
Comment on lines
+152
to
+201
|
||
|
|
||
| def test_redis__repr_cluster_client(self): | ||
| """Verify repr falls back gracefully for RedisCluster clients without connection_pool.""" | ||
| from redis.asyncio import RedisCluster | ||
|
|
||
| check = RedisHealthCheck( | ||
| client_factory=lambda: RedisCluster(host="clusterhost", port=7000) | ||
| ) | ||
| # RedisCluster has no connection_pool attribute, so __repr__ should fall back | ||
| assert repr(check) == "Redis()" | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot let's actually implement support clusters and show the hosts.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Implemented in 56e256b. The test ( |
||
|
|
||
| @pytest.mark.integration | ||
| @pytest.mark.asyncio | ||
| async def test_redis__real_connection(self): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The repr method creates Redis client instances via client_factory() but never closes them, causing a resource leak. This is particularly problematic because repr can be called multiple times (for JSON output, text output, feed output, logging, debugging, etc.).
Consider one of these alternatives:
The current implementation creates a new unclosed connection pool every time repr() is invoked, which will eventually exhaust connection resources.