MCPcopy
hub / github.com/redis/redis-py / test_refresh_using_specific_nodes

Method test_refresh_using_specific_nodes

tests/test_cluster.py:577–660  ·  view source on GitHub ↗

Test making calls on specific nodes when the cluster has failed over to another node

(self, request)

Source from the content-addressed store, hash-verified

575 moved_redirection_helper(request, failover=False, circular_moved=True)
576
577 def test_refresh_using_specific_nodes(self, request):
578 """
579 Test making calls on specific nodes when the cluster has failed over to
580 another node
581 """
582 node_7006 = ClusterNode(host=default_host, port=7006, server_type=PRIMARY)
583 node_7007 = ClusterNode(host=default_host, port=7007, server_type=PRIMARY)
584 with patch.object(Redis, "parse_response") as parse_response:
585 with patch.object(NodesManager, "initialize", autospec=True) as initialize:
586 with patch.multiple(
587 Connection, send_command=DEFAULT, connect=DEFAULT, can_read=DEFAULT
588 ) as mocks:
589 # simulate 7006 as a failed node
590 def parse_response_mock(connection, command_name, **options):
591 if connection.port == 7006:
592 parse_response.failed_calls += 1
593 raise ClusterDownError(
594 "CLUSTERDOWN The cluster is "
595 "down. Use CLUSTER INFO for "
596 "more information"
597 )
598 elif connection.port == 7007:
599 parse_response.successful_calls += 1
600
601 def initialize_mock(self, *args, **kwargs):
602 # start with all slots mapped to 7006
603 self.nodes_cache = {node_7006.name: node_7006}
604 self.default_node = node_7006
605 self.slots_cache = {}
606
607 for i in range(0, 16383):
608 self.slots_cache[i] = [node_7006]
609
610 # After the first connection fails, a reinitialize
611 # should follow the cluster to 7007
612 def map_7007(self, *args, **kwargs):
613 self.nodes_cache = {node_7007.name: node_7007}
614 self.default_node = node_7007
615 self.slots_cache = {}
616
617 for i in range(0, 16383):
618 self.slots_cache[i] = [node_7007]
619
620 # Change initialize side effect for the second call
621 initialize.side_effect = map_7007
622
623 parse_response.side_effect = parse_response_mock
624 parse_response.successful_calls = 0
625 parse_response.failed_calls = 0
626 initialize.side_effect = initialize_mock
627 mocks["can_read"].return_value = False
628 mocks["send_command"].return_value = "MOCK_OK"
629 mocks["connect"].return_value = None
630 with patch.object(
631 CommandsParser, "initialize", autospec=True
632 ) as cmd_parser_initialize:
633
634 def cmd_init_mock(self, r):

Callers

nothing calls this directly

Calls 6

ClusterNodeClass · 0.90
_get_clientFunction · 0.85
objectMethod · 0.80
get_nodesMethod · 0.45
get_nodeMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected