From 1219916b4369ffe77e170f4a2b2402374bc810a7 Mon Sep 17 00:00:00 2001 From: mrniko <1104661+mrniko@users.noreply.github.com> Date: Wed, 22 Jan 2025 13:05:00 +0300 Subject: [PATCH] RedissonTest.testMasterSlave() test added --- .../test/java/org/redisson/RedissonTest.java | 32 +++++++++++++++++++ .../java/org/redisson/SimpleDnsServer.java | 8 ++++- 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/redisson/src/test/java/org/redisson/RedissonTest.java b/redisson/src/test/java/org/redisson/RedissonTest.java index deecd0377..44deea822 100644 --- a/redisson/src/test/java/org/redisson/RedissonTest.java +++ b/redisson/src/test/java/org/redisson/RedissonTest.java @@ -2,6 +2,11 @@ package org.redisson; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufAllocator; +import io.netty.channel.socket.DatagramChannel; +import io.netty.channel.socket.SocketChannel; +import io.netty.resolver.AddressResolverGroup; +import io.netty.resolver.dns.DnsServerAddressStreamProvider; +import io.netty.resolver.dns.DnsServerAddresses; import io.netty.util.CharsetUtil; import net.bytebuddy.utility.RandomString; import nl.altindag.log.LogCaptor; @@ -27,6 +32,7 @@ import org.redisson.config.*; import org.redisson.connection.CRC16; import org.redisson.connection.ConnectionListener; import org.redisson.connection.MasterSlaveConnectionManager; +import org.redisson.connection.SequentialDnsAddressResolverFactory; import org.redisson.connection.pool.SlaveConnectionPool; import org.redisson.misc.RedisURI; import org.testcontainers.containers.ContainerState; @@ -702,6 +708,32 @@ public class RedissonTest extends RedisDockerTest { assertThat(c.toYAML()).isEqualTo(t); } + @Test + public void testMasterSlave() throws InterruptedException { + SimpleDnsServer s = new SimpleDnsServer(); + + Config c2 = new Config(); + c2.setAddressResolverGroupFactory(new SequentialDnsAddressResolverFactory() { + @Override + public AddressResolverGroup create(Class channelType, Class socketChannelType, DnsServerAddressStreamProvider nameServerProvider) { + return super.create(channelType, socketChannelType, hostname -> + DnsServerAddresses.singleton(s.getAddr()).stream()); + } + }); + c2.useMasterSlaveServers() + .setMasterAddress("redis://masterhost:" + REDIS.getFirstMappedPort()) + .addSlaveAddress("redis://slavehost1:" + REDIS.getFirstMappedPort(), + "redis://slavehost2:" + REDIS.getFirstMappedPort()); + + RedissonClient cc = Redisson.create(c2); + RBucket b = cc.getBucket("test"); + b.set("1"); + assertThat(b.get()).isEqualTo("1"); + + cc.shutdown(); + s.stop(); + } + @Test public void testMasterSlaveConfigYAML() throws IOException { Config c2 = new Config(); diff --git a/redisson/src/test/java/org/redisson/SimpleDnsServer.java b/redisson/src/test/java/org/redisson/SimpleDnsServer.java index 24de79563..7a09b50fd 100644 --- a/redisson/src/test/java/org/redisson/SimpleDnsServer.java +++ b/redisson/src/test/java/org/redisson/SimpleDnsServer.java @@ -8,12 +8,14 @@ import io.netty.channel.socket.nio.NioDatagramChannel; import io.netty.handler.codec.dns.*; import java.net.InetAddress; +import java.net.InetSocketAddress; public class SimpleDnsServer { private final EventLoopGroup group = new NioEventLoopGroup(); private final Channel channel; private String ip = "127.0.0.1"; + private final int port = 55; public SimpleDnsServer() throws InterruptedException { Bootstrap bootstrap = new Bootstrap(); @@ -28,10 +30,14 @@ public class SimpleDnsServer { } }); - ChannelFuture future = bootstrap.bind(55).sync(); + ChannelFuture future = bootstrap.bind(port).sync(); channel = future.channel(); } + public InetSocketAddress getAddr() { + return new InetSocketAddress(ip, port); + } + public void stop() { channel.close(); group.shutdownGracefully();