11package redis .clients .jedis ;
22
33import java .util .HashSet ;
4+ import java .util .Map ;
45import java .util .Set ;
56
67import org .apache .commons .pool2 .impl .GenericObjectPoolConfig ;
78
89import org .junit .jupiter .api .BeforeEach ;
910import org .junit .jupiter .api .Test ;
1011import org .junit .jupiter .api .Tag ;
12+ import org .junit .jupiter .api .Timeout ;
1113import redis .clients .jedis .exceptions .JedisConnectionException ;
1214import redis .clients .jedis .exceptions .JedisException ;
1315import redis .clients .jedis .providers .SentineledConnectionProvider ;
16+ import redis .clients .jedis .util .ReflectionTestUtil ;
1417
18+ import static org .hamcrest .MatcherAssert .assertThat ;
19+ import static org .hamcrest .Matchers .equalTo ;
20+ import static org .hamcrest .Matchers .hasKey ;
21+ import static org .hamcrest .Matchers .sameInstance ;
1522import static org .junit .jupiter .api .Assertions .assertEquals ;
1623import static org .junit .jupiter .api .Assertions .assertSame ;
1724import static org .junit .jupiter .api .Assertions .assertThrows ;
@@ -28,6 +35,8 @@ public class SentineledConnectionProviderTest {
2835 protected static final HostAndPort sentinel1 = HostAndPorts .getSentinelServers ().get (1 );
2936 protected static final HostAndPort sentinel2 = HostAndPorts .getSentinelServers ().get (3 );
3037
38+ private static final EndpointConfig primary = HostAndPorts .getRedisEndpoint ("standalone2-primary" );
39+
3140 protected Set <HostAndPort > sentinels = new HashSet <>();
3241
3342 @ BeforeEach
@@ -51,6 +60,68 @@ public void repeatedSentinelPoolInitialization() {
5160 }
5261 }
5362
63+ /**
64+ * Ensure that getConnectionMap() does not cause connection leak. (#4323)
65+ */
66+ @ Test
67+ @ Timeout ( value = 1 )
68+ public void getConnectionMapDoesNotCauseConnectionLeak () {
69+
70+ ConnectionPoolConfig config = new ConnectionPoolConfig ();
71+ config .setMaxTotal (1 );
72+
73+ try (SentineledConnectionProvider sut = new SentineledConnectionProvider (MASTER_NAME ,
74+ primary .getClientConfigBuilder ().build (), config , sentinels ,
75+ DefaultJedisClientConfig .builder ().build ())) {
76+
77+ HostAndPort resolvedPrimary = sut .getCurrentMaster ();
78+ ConnectionPool pool = ReflectionTestUtil .getField (sut ,"pool" );
79+ assertThat (pool .getNumActive (), equalTo (0 ));
80+
81+ Map <?, ?> cm = sut .getConnectionMap ();
82+
83+ // exactly one entry for current primary
84+ // and no active connections
85+ assertThat (cm .size (), equalTo (1 ));
86+ assertThat (cm , hasKey (resolvedPrimary ));
87+ assertThat (pool .getNumActive (), equalTo (0 ));
88+ // primary did not change
89+ assertThat (ReflectionTestUtil .getField (sut ,"pool" ), sameInstance (pool ));
90+ }
91+ }
92+
93+ /**
94+ * Ensure that getPrimaryNodesConnectionMap() does not cause connection leak. (#4323)
95+ */
96+ @ Test
97+ @ Timeout ( value = 1 )
98+ public void getPrimaryNodesConnectionMapDoesNotCauseConnectionLeak () {
99+
100+ ConnectionPoolConfig config = new ConnectionPoolConfig ();
101+ config .setMaxTotal (1 );
102+
103+ try (SentineledConnectionProvider sut = new SentineledConnectionProvider (MASTER_NAME ,
104+ primary .getClientConfigBuilder ().build (), config , sentinels ,
105+ DefaultJedisClientConfig .builder ().build ())) {
106+
107+ HostAndPort resolvedPrimary = sut .getCurrentMaster ();
108+ ConnectionPool pool = ReflectionTestUtil .getField (sut ,"pool" );
109+ assertThat (pool .getNumActive (), equalTo (0 ));
110+
111+
112+ Map <?, ?> cm = sut .getPrimaryNodesConnectionMap ();
113+
114+ // exactly one entry for current primary
115+ // and no active connections
116+ assertThat (cm .size (), equalTo (1 ));
117+ assertThat (cm , hasKey (resolvedPrimary ));
118+ assertThat (pool .getNumActive (), equalTo (0 ));
119+ // primary did not change
120+ assertThat (ReflectionTestUtil .getField (sut ,"pool" ), sameInstance (pool ));
121+ }
122+
123+ }
124+
54125 @ Test
55126 public void initializeWithNotAvailableSentinelsShouldThrowException () {
56127 Set <HostAndPort > wrongSentinels = new HashSet <>();
0 commit comments