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

Method test_address_remap

tests/test_cluster.py:1053–1096  ·  view source on GitHub ↗

Test that we can create a rediscluster object with a host-port remapper and map connections through proxy objects

(self, request, master_host)

Source from the content-addressed store, hash-verified

1051 assert r.get_default_node() != curr_default_node
1052
1053 def test_address_remap(self, request, master_host):
1054 """Test that we can create a rediscluster object with
1055 a host-port remapper and map connections through proxy objects
1056 """
1057
1058 # we remap the first n nodes
1059 offset = 1000
1060 n = 6
1061 hostname, master_port = master_host
1062 ports = [master_port + i for i in range(n)]
1063
1064 def address_remap(address):
1065 # remap first three nodes to our local proxy
1066 # old = host, port
1067 host, port = address
1068 if int(port) in ports:
1069 host, port = "127.0.0.1", int(port) + offset
1070 # print(f"{old} {host, port}")
1071 return host, port
1072
1073 # create the proxies
1074 proxies = [
1075 NodeProxy(("127.0.0.1", port + offset), (hostname, port)) for port in ports
1076 ]
1077 for p in proxies:
1078 p.start()
1079 try:
1080 # create cluster:
1081 r = _get_client(
1082 RedisCluster, request, flushdb=False, address_remap=address_remap
1083 )
1084 try:
1085 assert r.ping() is True
1086 assert r.set("byte_string", b"giraffe")
1087 assert r.get("byte_string") == b"giraffe"
1088 finally:
1089 r.close()
1090 finally:
1091 for p in proxies:
1092 p.close()
1093
1094 # verify that the proxies were indeed used
1095 n_used = sum((1 if p.n_connections else 0) for p in proxies)
1096 assert n_used > 1
1097
1098
1099@pytest.mark.onlycluster

Callers

nothing calls this directly

Calls 8

_get_clientFunction · 0.85
sumClass · 0.85
NodeProxyClass · 0.70
startMethod · 0.45
pingMethod · 0.45
setMethod · 0.45
getMethod · 0.45
closeMethod · 0.45

Tested by

no test coverage detected