1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
|
import copy
import platform
import socket
import sys
import threading
import types
from errno import ECONNREFUSED
from typing import Any
from unittest import mock
from unittest.mock import call, patch
import pytest
import redis
from redis import ConnectionPool, Redis
from redis._parsers import _HiredisParser, _RESP2Parser, _RESP3Parser
from redis.backoff import NoBackoff
from redis.cache import (
CacheConfig,
CacheEntry,
CacheEntryStatus,
CacheInterface,
CacheKey,
DefaultCache,
LRUPolicy,
)
from redis.connection import (
CacheProxyConnection,
Connection,
SSLConnection,
UnixDomainSocketConnection,
parse_url,
)
from redis.credentials import UsernamePasswordCredentialProvider
from redis.exceptions import ConnectionError, InvalidResponse, RedisError, TimeoutError
from redis.retry import Retry
from redis.utils import HIREDIS_AVAILABLE
from .conftest import skip_if_server_version_lt
from .mocks import MockSocket
@pytest.mark.skipif(HIREDIS_AVAILABLE, reason="PythonParser only")
@pytest.mark.onlynoncluster
def test_invalid_response(r):
raw = b"x"
parser = r.connection._parser
with mock.patch.object(parser._buffer, "readline", return_value=raw):
with pytest.raises(InvalidResponse, match=f"Protocol Error: {raw!r}"):
parser.read_response()
@skip_if_server_version_lt("4.0.0")
@pytest.mark.redismod
def test_loading_external_modules(r):
def inner():
pass
r.load_external_module("myfuncname", inner)
assert getattr(r, "myfuncname") == inner
assert isinstance(getattr(r, "myfuncname"), types.FunctionType)
# and call it
from redis.commands import RedisModuleCommands
j = RedisModuleCommands.json
r.load_external_module("sometestfuncname", j)
# d = {'hello': 'world!'}
# mod = j(r)
# mod.set("fookey", ".", d)
# assert mod.get('fookey') == d
class TestConnection:
def test_disconnect(self):
conn = Connection()
mock_sock = mock.Mock()
conn._sock = mock_sock
conn.disconnect()
mock_sock.shutdown.assert_called_once()
mock_sock.close.assert_called_once()
assert conn._sock is None
def test_disconnect__shutdown_OSError(self):
"""An OSError on socket shutdown will still close the socket."""
conn = Connection()
mock_sock = mock.Mock()
conn._sock = mock_sock
conn._sock.shutdown.side_effect = OSError
conn.disconnect()
mock_sock.shutdown.assert_called_once()
mock_sock.close.assert_called_once()
assert conn._sock is None
def test_disconnect__close_OSError(self):
"""An OSError on socket close will still clear out the socket."""
conn = Connection()
mock_sock = mock.Mock()
conn._sock = mock_sock
conn._sock.close.side_effect = OSError
conn.disconnect()
mock_sock.shutdown.assert_called_once()
mock_sock.close.assert_called_once()
assert conn._sock is None
def clear(self, conn):
conn.retry_on_error.clear()
def test_retry_connect_on_timeout_error(self):
"""Test that the _connect function is retried in case of a timeout"""
conn = Connection(retry_on_timeout=True, retry=Retry(NoBackoff(), 3))
origin_connect = conn._connect
conn._connect = mock.Mock()
def mock_connect():
# connect only on the last retry
if conn._connect.call_count <= 2:
raise socket.timeout
else:
return origin_connect()
conn._connect.side_effect = mock_connect
conn.connect()
assert conn._connect.call_count == 3
self.clear(conn)
def test_connect_without_retry_on_os_error(self):
"""Test that the _connect function is not being retried in case of a OSError"""
with patch.object(Connection, "_connect") as _connect:
_connect.side_effect = OSError("")
conn = Connection(retry_on_timeout=True, retry=Retry(NoBackoff(), 2))
with pytest.raises(ConnectionError):
conn.connect()
assert _connect.call_count == 1
self.clear(conn)
def test_connect_timeout_error_without_retry(self):
"""Test that the _connect function is not being retried if retry_on_timeout is
set to False"""
conn = Connection(retry_on_timeout=False)
conn._connect = mock.Mock()
conn._connect.side_effect = socket.timeout
with pytest.raises(TimeoutError, match="Timeout connecting to server"):
conn.connect()
assert conn._connect.call_count == 1
self.clear(conn)
@pytest.mark.onlynoncluster
@pytest.mark.parametrize(
"parser_class",
[_RESP2Parser, _RESP3Parser, _HiredisParser],
ids=["RESP2Parser", "RESP3Parser", "HiredisParser"],
)
def test_connection_parse_response_resume(r: redis.Redis, parser_class):
"""
This test verifies that the Connection parser,
be that PythonParser or HiredisParser,
can be interrupted at IO time and then resume parsing.
"""
if parser_class is _HiredisParser and not HIREDIS_AVAILABLE:
pytest.skip("Hiredis not available)")
args = dict(r.connection_pool.connection_kwargs)
args["parser_class"] = parser_class
conn = Connection(**args)
conn.connect()
message = (
b"*3\r\n$7\r\nmessage\r\n$8\r\nchannel1\r\n"
b"$25\r\nhi\r\nthere\r\n+how\r\nare\r\nyou\r\n"
)
mock_socket = MockSocket(message, interrupt_every=2)
if isinstance(conn._parser, _RESP2Parser) or isinstance(conn._parser, _RESP3Parser):
conn._parser._buffer._sock = mock_socket
else:
conn._parser._sock = mock_socket
for i in range(100):
try:
response = conn.read_response(disconnect_on_error=False)
break
except MockSocket.TestError:
pass
else:
pytest.fail("didn't receive a response")
assert response
assert i > 0
@pytest.mark.onlynoncluster
@pytest.mark.parametrize(
"Class",
[
Connection,
SSLConnection,
UnixDomainSocketConnection,
],
)
def test_pack_command(Class):
"""
This test verifies that the pack_command works
on all supported connections. #2581
"""
cmd = (
"HSET",
"foo",
"key",
"value1",
b"key_b",
b"bytes str",
b"key_i",
67,
"key_f",
3.14159265359,
)
expected = (
b"*10\r\n$4\r\nHSET\r\n$3\r\nfoo\r\n$3\r\nkey\r\n$6\r\nvalue1\r\n"
b"$5\r\nkey_b\r\n$9\r\nbytes str\r\n$5\r\nkey_i\r\n$2\r\n67\r\n$5"
b"\r\nkey_f\r\n$13\r\n3.14159265359\r\n"
)
actual = Class().pack_command(*cmd)[0]
assert actual == expected, f"actual = {actual}, expected = {expected}"
@pytest.mark.onlynoncluster
def test_create_single_connection_client_from_url():
client = redis.Redis.from_url(
"redis://localhost:6379/0?", single_connection_client=True
)
assert client.connection is not None
@pytest.mark.parametrize("from_url", (True, False), ids=("from_url", "from_args"))
def test_pool_auto_close(request, from_url):
"""Verify that basic Redis instances have auto_close_connection_pool set to True"""
url: str = request.config.getoption("--redis-url")
url_args = parse_url(url)
def get_redis_connection():
if from_url:
return Redis.from_url(url)
return Redis(**url_args)
r1 = get_redis_connection()
assert r1.auto_close_connection_pool is True
r1.close()
@pytest.mark.skipif(sys.version_info == (3, 9), reason="Flacky test on Python 3.9")
@pytest.mark.parametrize("from_url", (True, False), ids=("from_url", "from_args"))
def test_redis_connection_pool(request, from_url):
"""Verify that basic Redis instances using `connection_pool`
have auto_close_connection_pool set to False"""
url: str = request.config.getoption("--redis-url")
url_args = parse_url(url)
pool = None
def get_redis_connection():
nonlocal pool
if from_url:
pool = ConnectionPool.from_url(url)
else:
pool = ConnectionPool(**url_args)
return Redis(connection_pool=pool)
called = 0
def mock_disconnect(_):
nonlocal called
called += 1
with patch.object(ConnectionPool, "disconnect", mock_disconnect):
with get_redis_connection() as r1:
assert r1.auto_close_connection_pool is False
assert called == 0
pool.disconnect()
@pytest.mark.parametrize("from_url", (True, False), ids=("from_url", "from_args"))
def test_redis_from_pool(request, from_url):
"""Verify that basic Redis instances created using `from_pool()`
have auto_close_connection_pool set to True"""
url: str = request.config.getoption("--redis-url")
url_args = parse_url(url)
pool = None
def get_redis_connection():
nonlocal pool
if from_url:
pool = ConnectionPool.from_url(url)
else:
pool = ConnectionPool(**url_args)
return Redis.from_pool(pool)
called = 0
def mock_disconnect(_):
nonlocal called
called += 1
with patch.object(ConnectionPool, "disconnect", mock_disconnect):
with get_redis_connection() as r1:
assert r1.auto_close_connection_pool is True
assert called == 1
pool.disconnect()
@pytest.mark.parametrize(
"conn, error, expected_message",
[
(SSLConnection(), OSError(), "Error connecting to localhost:6379."),
(SSLConnection(), OSError(12), "Error 12 connecting to localhost:6379."),
(
SSLConnection(),
OSError(12, "Some Error"),
"Error 12 connecting to localhost:6379. Some Error.",
),
(
UnixDomainSocketConnection(path="unix:///tmp/redis.sock"),
OSError(),
"Error connecting to unix:///tmp/redis.sock.",
),
(
UnixDomainSocketConnection(path="unix:///tmp/redis.sock"),
OSError(12),
"Error 12 connecting to unix:///tmp/redis.sock.",
),
(
UnixDomainSocketConnection(path="unix:///tmp/redis.sock"),
OSError(12, "Some Error"),
"Error 12 connecting to unix:///tmp/redis.sock. Some Error.",
),
],
)
def test_format_error_message(conn, error, expected_message):
"""Test that the _error_message function formats errors correctly"""
error_message = conn._error_message(error)
assert error_message == expected_message
def test_network_connection_failure():
exp_err = f"Error {ECONNREFUSED} connecting to localhost:9999. Connection refused."
with pytest.raises(ConnectionError, match=exp_err):
redis = Redis(port=9999)
redis.set("a", "b")
def test_unix_socket_connection_failure():
exp_err = "Error 2 connecting to unix:///tmp/a.sock. No such file or directory."
with pytest.raises(ConnectionError, match=exp_err):
redis = Redis(unix_socket_path="unix:///tmp/a.sock")
redis.set("a", "b")
class TestUnitConnectionPool:
@pytest.mark.parametrize(
"max_conn", (-1, "str"), ids=("non-positive", "wrong type")
)
def test_throws_error_on_incorrect_max_connections(self, max_conn):
with pytest.raises(
ValueError, match='"max_connections" must be a positive integer'
):
ConnectionPool(
max_connections=max_conn,
)
def test_throws_error_on_cache_enable_in_resp2(self):
with pytest.raises(
RedisError, match="Client caching is only supported with RESP version 3"
):
ConnectionPool(protocol=2, cache_config=CacheConfig())
def test_throws_error_on_incorrect_cache_implementation(self):
with pytest.raises(ValueError, match="Cache must implement CacheInterface"):
ConnectionPool(protocol=3, cache="wrong")
def test_returns_custom_cache_implementation(self, mock_cache):
connection_pool = ConnectionPool(protocol=3, cache=mock_cache)
assert mock_cache == connection_pool.cache
connection_pool.disconnect()
def test_creates_cache_with_custom_cache_factory(
self, mock_cache_factory, mock_cache
):
mock_cache_factory.get_cache.return_value = mock_cache
connection_pool = ConnectionPool(
protocol=3,
cache_config=CacheConfig(max_size=5),
cache_factory=mock_cache_factory,
)
assert connection_pool.cache == mock_cache
connection_pool.disconnect()
def test_creates_cache_with_given_configuration(self, mock_cache):
connection_pool = ConnectionPool(
protocol=3, cache_config=CacheConfig(max_size=100)
)
assert isinstance(connection_pool.cache, CacheInterface)
assert connection_pool.cache.config.get_max_size() == 100
assert isinstance(connection_pool.cache.eviction_policy, LRUPolicy)
connection_pool.disconnect()
def test_make_connection_proxy_connection_on_given_cache(self):
connection_pool = ConnectionPool(protocol=3, cache_config=CacheConfig())
assert isinstance(connection_pool.make_connection(), CacheProxyConnection)
connection_pool.disconnect()
class TestUnitCacheProxyConnection:
def test_clears_cache_on_disconnect(self, mock_connection, cache_conf):
cache = DefaultCache(CacheConfig(max_size=10))
cache_key = CacheKey(command="GET", redis_keys=("foo",))
cache.set(
CacheEntry(
cache_key=cache_key,
cache_value=b"bar",
status=CacheEntryStatus.VALID,
connection_ref=mock_connection,
)
)
assert cache.get(cache_key).cache_value == b"bar"
mock_connection.disconnect.return_value = None
mock_connection.retry = "mock"
mock_connection.host = "mock"
mock_connection.port = "mock"
mock_connection.credential_provider = UsernamePasswordCredentialProvider()
proxy_connection = CacheProxyConnection(
mock_connection, cache, threading.RLock()
)
proxy_connection.disconnect()
assert len(cache.collection) == 0
@pytest.mark.skipif(
platform.python_implementation() == "PyPy",
reason="Pypy doesn't support side_effect",
)
def test_read_response_returns_cached_reply(self, mock_cache, mock_connection):
mock_connection.retry = "mock"
mock_connection.host = "mock"
mock_connection.port = "mock"
mock_connection.credential_provider = UsernamePasswordCredentialProvider()
mock_cache.is_cachable.return_value = True
mock_cache.get.side_effect = [
None,
None,
CacheEntry(
cache_key=CacheKey(command="GET", redis_keys=("foo",)),
cache_value=CacheProxyConnection.DUMMY_CACHE_VALUE,
status=CacheEntryStatus.IN_PROGRESS,
connection_ref=mock_connection,
),
CacheEntry(
cache_key=CacheKey(command="GET", redis_keys=("foo",)),
cache_value=b"bar",
status=CacheEntryStatus.VALID,
connection_ref=mock_connection,
),
CacheEntry(
cache_key=CacheKey(command="GET", redis_keys=("foo",)),
cache_value=b"bar",
status=CacheEntryStatus.VALID,
connection_ref=mock_connection,
),
CacheEntry(
cache_key=CacheKey(command="GET", redis_keys=("foo",)),
cache_value=b"bar",
status=CacheEntryStatus.VALID,
connection_ref=mock_connection,
),
]
mock_connection.send_command.return_value = Any
mock_connection.read_response.return_value = b"bar"
mock_connection.can_read.return_value = False
proxy_connection = CacheProxyConnection(
mock_connection, mock_cache, threading.RLock()
)
proxy_connection.send_command(*["GET", "foo"], **{"keys": ["foo"]})
assert proxy_connection.read_response() == b"bar"
assert proxy_connection._current_command_cache_key is None
assert proxy_connection.read_response() == b"bar"
mock_cache.set.assert_has_calls(
[
call(
CacheEntry(
cache_key=CacheKey(command="GET", redis_keys=("foo",)),
cache_value=CacheProxyConnection.DUMMY_CACHE_VALUE,
status=CacheEntryStatus.IN_PROGRESS,
connection_ref=mock_connection,
)
),
call(
CacheEntry(
cache_key=CacheKey(command="GET", redis_keys=("foo",)),
cache_value=b"bar",
status=CacheEntryStatus.VALID,
connection_ref=mock_connection,
)
),
]
)
mock_cache.get.assert_has_calls(
[
call(CacheKey(command="GET", redis_keys=("foo",))),
call(CacheKey(command="GET", redis_keys=("foo",))),
call(CacheKey(command="GET", redis_keys=("foo",))),
]
)
@pytest.mark.skipif(
platform.python_implementation() == "PyPy",
reason="Pypy doesn't support side_effect",
)
def test_triggers_invalidation_processing_on_another_connection(
self, mock_cache, mock_connection
):
mock_connection.retry = "mock"
mock_connection.host = "mock"
mock_connection.port = "mock"
mock_connection.credential_provider = UsernamePasswordCredentialProvider()
another_conn = copy.deepcopy(mock_connection)
another_conn.can_read.side_effect = [True, False]
another_conn.read_response.return_value = None
cache_entry = CacheEntry(
cache_key=CacheKey(command="GET", redis_keys=("foo",)),
cache_value=b"bar",
status=CacheEntryStatus.VALID,
connection_ref=another_conn,
)
mock_cache.is_cachable.return_value = True
mock_cache.get.return_value = cache_entry
mock_connection.can_read.return_value = False
proxy_connection = CacheProxyConnection(
mock_connection, mock_cache, threading.RLock()
)
proxy_connection.send_command(*["GET", "foo"], **{"keys": ["foo"]})
assert proxy_connection.read_response() == b"bar"
assert another_conn.can_read.call_count == 2
another_conn.read_response.assert_called_once()
|