-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Add ability to reset password in JedisFactory #1606
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,11 +17,12 @@ | |
|
|
||
| import redis.clients.jedis.HostAndPort; | ||
| import redis.clients.jedis.Jedis; | ||
| import redis.clients.jedis.JedisFactory; | ||
| import redis.clients.jedis.JedisPool; | ||
| import redis.clients.jedis.JedisPoolConfig; | ||
| import redis.clients.jedis.Transaction; | ||
| import redis.clients.jedis.exceptions.InvalidURIException; | ||
| import redis.clients.jedis.exceptions.JedisException; | ||
| import redis.clients.jedis.exceptions.JedisConnectionException; | ||
| import redis.clients.jedis.exceptions.JedisExhaustedPoolException; | ||
|
|
||
| public class JedisPoolTest { | ||
|
|
@@ -381,4 +382,40 @@ private int getClientCount(final String clientList) { | |
| return clientList.split("\n").length; | ||
| } | ||
|
|
||
| @Test | ||
| public void testResetInvalidPassword() { | ||
| JedisFactory factory = new JedisFactory(hnp.getHost(), hnp.getPort(), 2000, 2000, "foobared", 0, "my_shiny_client_name"); | ||
|
|
||
|
|
||
| try(JedisPool pool = new JedisPool(new JedisPoolConfig(), factory); | ||
| Jedis obj1 = pool.getResource();){ | ||
| obj1.set("foo", "bar"); | ||
| assertEquals("bar", obj1.get("foo")); | ||
| assertEquals(1, pool.getNumActive()); | ||
|
|
||
| factory.setPassword("wrong password"); | ||
| try (Jedis obj2 = pool.getResource()) { | ||
| fail("Should not get resource from pool"); | ||
| } catch (JedisConnectionException e) {} | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testResetValidPassword() { | ||
| JedisFactory factory = new JedisFactory(hnp.getHost(), hnp.getPort(), 2000, 2000, "bad password", 0, "my_shiny_client_name"); | ||
| JedisPool pool = new JedisPool(new JedisPoolConfig(), factory); | ||
| Jedis obj = null; | ||
| try { | ||
| pool.getResource(); | ||
|
||
| fail("Could not get resource from pool"); | ||
| } catch (JedisConnectionException e) { | ||
| factory.setPassword("foobared"); | ||
| obj = pool.getResource(); | ||
|
||
| obj.set("foo", "bar"); | ||
| assertEquals("bar", obj.get("foo")); | ||
|
||
| } finally { | ||
| obj.close(); | ||
| pool.close(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertTrue; | ||
| import static org.junit.Assert.fail; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.Set; | ||
|
|
@@ -12,6 +13,8 @@ | |
|
|
||
| import redis.clients.jedis.HostAndPort; | ||
| import redis.clients.jedis.Jedis; | ||
| import redis.clients.jedis.JedisFactory; | ||
| import redis.clients.jedis.JedisPoolConfig; | ||
| import redis.clients.jedis.JedisSentinelPool; | ||
| import redis.clients.jedis.Transaction; | ||
| import redis.clients.jedis.exceptions.JedisConnectionException; | ||
|
|
@@ -161,6 +164,42 @@ public void customClientName() { | |
|
|
||
| assertTrue(pool.isClosed()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testResetInvalidPassword() { | ||
| JedisFactory factory = new JedisFactory(null, 0, 2000, 2000, "foobared", 0, "my_shiny_client_name"); | ||
|
|
||
| try(JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, new JedisPoolConfig(), factory); | ||
| Jedis obj1 = pool.getResource(); ) { | ||
| obj1.set("foo", "bar"); | ||
| assertEquals("bar", obj1.get("foo")); | ||
| assertEquals(1, pool.getNumActive()); | ||
|
|
||
| factory.setPassword("wrong password"); | ||
| try (Jedis obj2 = pool.getResource();) { | ||
| fail("Should not get resource from pool"); | ||
| }catch (JedisConnectionException e) {} | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testResetValidPassword() { | ||
| JedisFactory factory = new JedisFactory(null, 0, 2000, 2000, "wrong password", 0, "my_shiny_client_name"); | ||
| JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, new JedisPoolConfig(), factory); | ||
| Jedis obj = null; | ||
| try { | ||
| pool.getResource(); | ||
|
||
| fail("Could not get resource from pool"); | ||
| } catch (JedisConnectionException e) { | ||
| factory.setPassword("foobared"); | ||
| obj = pool.getResource(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Close |
||
| obj.set("foo", "bar"); | ||
| assertEquals("bar", obj.get("foo")); | ||
| } finally { | ||
| obj.close(); | ||
| pool.close(); | ||
| } | ||
| } | ||
|
|
||
| private void forceFailover(JedisSentinelPool pool) throws InterruptedException { | ||
| HostAndPort oldMaster = pool.getCurrentHostMaster(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If operation fails in try,
objwill be null in finally and will get NPE. Declare directly in catch.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done