Skip to content
This repository has been archived by the owner on Jan 9, 2024. It is now read-only.

Commit

Permalink
Fix some tests that was broken after the test server has been reshard…
Browse files Browse the repository at this point in the history
…ed a few times
  • Loading branch information
Grokzen committed Oct 4, 2015
1 parent 055c168 commit 077eb9f
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 23 deletions.
22 changes: 17 additions & 5 deletions tests/test_cluster_connection_pool.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# 3rd party imports
import pytest
import redis
from mock import Mock
from mock import patch, Mock
from redis.connection import ssl_available
from redis._compat import unicode

Expand Down Expand Up @@ -110,8 +110,14 @@ def test_get_connection_by_key(self):
"""
pool = self.get_pool(connection_kwargs={})

connection = pool.get_connection_by_key("foo")
assert connection.port == 7002
# Patch the call that is made inside the method to allow control of the returned connection object
with patch.object(ClusterConnectionPool, 'get_connection_by_slot', autospec=True) as pool_mock:
def side_effect(self, *args, **kwargs):
return DummyConnection(port=1337)
pool_mock.side_effect = side_effect

connection = pool.get_connection_by_key("foo")
assert connection.port == 1337

with pytest.raises(RedisClusterException) as ex:
pool.get_connection_by_key(None)
Expand All @@ -123,8 +129,14 @@ def test_get_connection_by_slot(self):
"""
pool = self.get_pool(connection_kwargs={})

connection = pool.get_connection_by_slot(12182)
assert connection.port == 7002
# Patch the call that is made inside the method to allow control of the returned connection object
with patch.object(ClusterConnectionPool, 'get_connection_by_node', autospec=True) as pool_mock:
def side_effect(self, *args, **kwargs):
return DummyConnection(port=1337)
pool_mock.side_effect = side_effect

connection = pool.get_connection_by_slot(12182)
assert connection.port == 1337

m = Mock()
pool.get_random_connection = m
Expand Down
32 changes: 19 additions & 13 deletions tests/test_cluster_obj.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,19 +183,25 @@ def test_refresh_table_asap():
with patch.object(NodeManager, 'initialize') as mock_initialize:
mock_initialize.return_value = None

r = StrictRedisCluster(host="127.0.0.1", port=7000)
r.connection_pool.nodes.slots[12182] = [{
"host": "127.0.0.1",
"port": 7002,
"name": "127.0.0.1:7002",
"server_type": "master",
}]
r.refresh_table_asap = True

i = len(mock_initialize.mock_calls)
r.execute_command("SET", "foo", "bar")
assert len(mock_initialize.mock_calls) - i == 1
assert r.refresh_table_asap is False
# Patch parse_response to avoid issues when the cluster sometimes return MOVED
with patch.object(StrictRedisCluster, 'parse_response') as mock_parse_response:
def side_effect(self, *args, **kwargs):
return None
mock_parse_response.side_effect = side_effect

r = StrictRedisCluster(host="127.0.0.1", port=7000)
r.connection_pool.nodes.slots[12182] = [{
"host": "127.0.0.1",
"port": 7002,
"name": "127.0.0.1:7002",
"server_type": "master",
}]
r.refresh_table_asap = True

i = len(mock_initialize.mock_calls)
r.execute_command("SET", "foo", "bar")
assert len(mock_initialize.mock_calls) - i == 1
assert r.refresh_table_asap is False


def test_ask_redirection():
Expand Down
6 changes: 1 addition & 5 deletions tests/test_node_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,12 +270,8 @@ def test_reset():
"""
n = NodeManager(startup_nodes=[{}])
n.initialize = Mock()
n.slots = {"foo": "bar"}
n.nodes = ["foo", "bar"]
n.reset()

assert n.slots == {}
assert n.nodes == {}
assert n.initialize.call_count == 1


def test_cluster_one_instance():
Expand Down

0 comments on commit 077eb9f

Please sign in to comment.