Skip to content

Commit

Permalink
Fix flakyness of JedisPooledTest
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Aug 19, 2023
1 parent 155acfd commit f38ccbf
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions src/test/java/redis/clients/jedis/JedisPooledTest.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package redis.clients.jedis;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anything;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThanOrEqualTo;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand All @@ -22,6 +22,7 @@
public class JedisPooledTest {

private static final HostAndPort hnp = HostAndPorts.getRedisServers().get(7);
private static final HostAndPort pwp = HostAndPorts.getRedisServers().get(1); // password protected

@Test
public void checkCloseableConnections() {
Expand Down Expand Up @@ -177,17 +178,16 @@ public void testResetValidCredentials() {
DefaultRedisCredentialsProvider credentialsProvider =
new DefaultRedisCredentialsProvider(new DefaultRedisCredentials(null, "bad password"));

try (JedisPooled pool = new JedisPooled(HostAndPorts.getRedisServers().get(0),
DefaultJedisClientConfig.builder().credentialsProvider(credentialsProvider)
.clientName("my_shiny_client_name").build())) {
try (JedisPooled pool = new JedisPooled(pwp, DefaultJedisClientConfig.builder()
.credentialsProvider(credentialsProvider).build())) {
try {
pool.get("foo");
fail("Should not get resource from pool");
} catch (JedisException e) { }
assertEquals(0, pool.getPool().getNumActive());

credentialsProvider.setCredentials(new DefaultRedisCredentials(null, "foobared"));
assertNull(pool.get("foo"));
assertThat(pool.get("foo"), anything());
}
}

Expand Down Expand Up @@ -238,9 +238,8 @@ public void cleanUp() {
GenericObjectPoolConfig<Connection> poolConfig = new GenericObjectPoolConfig<>();
poolConfig.setMaxTotal(1);
poolConfig.setTestOnBorrow(true);
try (JedisPooled pool = new JedisPooled(HostAndPorts.getRedisServers().get(0),
DefaultJedisClientConfig.builder().credentialsProvider(credentialsProvider)
.build(), poolConfig)) {
try (JedisPooled pool = new JedisPooled(pwp, DefaultJedisClientConfig.builder()
.credentialsProvider(credentialsProvider).build(), poolConfig)) {
try {
pool.get("foo");
fail("Should not get resource from pool");
Expand All @@ -251,7 +250,7 @@ public void cleanUp() {
assertThat(cleanupCount.getAndSet(0), greaterThanOrEqualTo(1));

validPassword.set(true);
assertNull(pool.get("foo"));
assertThat(pool.get("foo"), anything());
assertThat(prepareCount.get(), equalTo(1));
assertThat(cleanupCount.get(), equalTo(1));
}
Expand Down

0 comments on commit f38ccbf

Please sign in to comment.