Test that we can create a rediscluster object with a host-port remapper and map connections through proxy objects
(self, create_redis, master_host)
| 1065 | r.replace_default_node(curr_default_node) |
| 1066 | |
| 1067 | async def test_address_remap(self, create_redis, master_host): |
| 1068 | """Test that we can create a rediscluster object with |
| 1069 | a host-port remapper and map connections through proxy objects |
| 1070 | """ |
| 1071 | |
| 1072 | # we remap the first n nodes |
| 1073 | offset = 1000 |
| 1074 | n = 6 |
| 1075 | hostname, master_port = master_host |
| 1076 | ports = [master_port + i for i in range(n)] |
| 1077 | |
| 1078 | def address_remap(address): |
| 1079 | # remap first three nodes to our local proxy |
| 1080 | # old = host, port |
| 1081 | host, port = address |
| 1082 | if int(port) in ports: |
| 1083 | host, port = "127.0.0.1", int(port) + offset |
| 1084 | # print(f"{old} {host, port}") |
| 1085 | return host, port |
| 1086 | |
| 1087 | # create the proxies |
| 1088 | proxies = [ |
| 1089 | NodeProxy(("127.0.0.1", port + offset), (hostname, port)) for port in ports |
| 1090 | ] |
| 1091 | await asyncio.gather(*[p.start() for p in proxies]) |
| 1092 | try: |
| 1093 | # create cluster: |
| 1094 | r = await create_redis( |
| 1095 | cls=RedisCluster, flushdb=False, address_remap=address_remap |
| 1096 | ) |
| 1097 | try: |
| 1098 | assert await r.ping() is True |
| 1099 | assert await r.set("byte_string", b"giraffe") |
| 1100 | assert await r.get("byte_string") == b"giraffe" |
| 1101 | finally: |
| 1102 | await r.aclose() |
| 1103 | finally: |
| 1104 | await asyncio.gather(*[p.aclose() for p in proxies]) |
| 1105 | |
| 1106 | # verify that the proxies were indeed used |
| 1107 | n_used = sum((1 if p.n_connections else 0) for p in proxies) |
| 1108 | assert n_used > 1 |
| 1109 | |
| 1110 | |
| 1111 | @pytest.mark.onlycluster |