Skip to content

Commit

Permalink
bring back 'destroy'
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed May 14, 2021
1 parent df8ddb7 commit f2c4062
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public void reset() {
for (JedisPool pool : nodes.values()) {
try {
if (pool != null) {
pool.close();
pool.destroy();
}
} catch (Exception e) {
// pass
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/redis/clients/jedis/JedisSentinelPool.java
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,12 @@ private static Set<HostAndPort> parseHostAndPorts(Set<String> strings) {
}

@Override
public void close() {
public void destroy() {
for (MasterListener m : masterListeners) {
m.shutdown();
}

super.close();
super.destroy();
}

public HostAndPort getCurrentHostMaster() {
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/redis/clients/jedis/util/Pool.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ public Pool(final GenericObjectPoolConfig<T> poolConfig, PooledObjectFactory<T>
super(factory, poolConfig);
}

@Override
public void close() {
destroy();
}

public T getResource() {
try {
return super.borrowObject();
Expand Down Expand Up @@ -46,6 +51,14 @@ public void returnResource(final T resource) {
}
}

public void destroy() {
try {
super.close();
} catch (Exception e) {
throw new JedisException("Could not destroy the pool", e);
}
}

public void returnBrokenResource(final T resource) {
if (resource == null) {
return;
Expand All @@ -56,4 +69,15 @@ public void returnBrokenResource(final T resource) {
throw new JedisException("Could not return the broken resource to the pool", e);
}
}

@Override
public void addObjects(int count) {
try {
for (int i = 0; i < count; i++) {
addObject();
}
} catch (Exception e) {
throw new JedisException("Error trying to add idle objects", e);
}
}
}
2 changes: 1 addition & 1 deletion src/test/java/redis/clients/jedis/tests/JedisPoolTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ public void getNumActiveReturnsTheCorrectNumber() {
}

@Test
public void testAddObject() throws Exception {
public void testAddObject() {
try (JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), 2000)) {
pool.addObjects(1);
assertEquals(1, pool.getNumIdle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void repeatedSentinelPoolInitialization() {
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000,
"foobared", 2);
pool.getResource().close();
pool.close();
pool.destroy();
}
}

Expand All @@ -76,14 +76,14 @@ public void initializeWithNotAvailableSentinelsShouldThrowException() {
wrongSentinels.add(new HostAndPort("localhost", 65431).toString());

JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, wrongSentinels);
pool.close();
pool.destroy();
}

@Test(expected = JedisException.class)
public void initializeWithNotMonitoredMasterNameShouldThrowException() {
final String wrongMasterName = "wrongMasterName";
JedisSentinelPool pool = new JedisSentinelPool(wrongMasterName, sentinels);
pool.close();
pool.destroy();
}

@Test
Expand Down Expand Up @@ -161,7 +161,7 @@ public void customClientName() {
assertEquals("my_shiny_client_name", jedis.clientGetname());
} finally {
jedis.close();
pool.close();
pool.destroy();
}

assertTrue(pool.isClosed());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void repeatedSentinelPoolInitialization() {
JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, sentinels, config, 1000,
"default", "foobared", 2);
pool.getResource().close();
pool.close();
pool.destroy();
}
}

Expand All @@ -82,14 +82,14 @@ public void initializeWithNotAvailableSentinelsShouldThrowException() {
wrongSentinels.add(new HostAndPort("localhost", 65431).toString());

JedisSentinelPool pool = new JedisSentinelPool(MASTER_NAME, wrongSentinels);
pool.close();
pool.destroy();
}

@Test(expected = JedisException.class)
public void initializeWithNotMonitoredMasterNameShouldThrowException() {
final String wrongMasterName = "wrongMasterName";
JedisSentinelPool pool = new JedisSentinelPool(wrongMasterName, sentinels);
pool.close();
pool.destroy();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void checkConnections() {
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
jedis.close();
pool.close();
pool.destroy();
}

@Test
Expand All @@ -72,7 +72,7 @@ public void checkConnectionWithDefaultPort() {
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
jedis.close();
pool.close();
pool.destroy();
}

@Test
Expand All @@ -86,7 +86,7 @@ public void checkJedisIsReusedWhenReturned() {
jedis = pool.getResource();
jedis.incr("foo");
jedis.close();
pool.close();
pool.destroy();
}

@Test
Expand All @@ -100,7 +100,7 @@ public void checkPoolRepairedWhenJedisIsBroken() {
jedis = pool.getResource();
jedis.incr("foo");
jedis.close();
pool.close();
pool.destroy();
}

@Test(expected = JedisExhaustedPoolException.class)
Expand Down Expand Up @@ -138,7 +138,7 @@ public void checkFailedJedisServer() {
ShardedJedis jedis = pool.getResource();
jedis.incr("foo");
jedis.close();
pool.close();
pool.destroy();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void checkConnections() {
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
jedis.close();
pool.close();
pool.destroy();
}

@Test
Expand All @@ -81,7 +81,7 @@ public void checkConnectionWithDefaultPort() {
jedis.set("foo", "bar");
assertEquals("bar", jedis.get("foo"));
jedis.close();
pool.close();
pool.destroy();
}

@Test
Expand All @@ -95,7 +95,7 @@ public void checkJedisIsReusedWhenReturned() {
jedis = pool.getResource();
jedis.incr("foo");
jedis.close();
pool.close();
pool.destroy();
}

@Test
Expand All @@ -109,7 +109,7 @@ public void checkPoolRepairedWhenJedisIsBroken() {
jedis = pool.getResource();
jedis.incr("foo");
jedis.close();
pool.close();
pool.destroy();
}

@Test(expected = JedisExhaustedPoolException.class)
Expand Down Expand Up @@ -147,7 +147,7 @@ public void checkFailedJedisServer() {
ShardedJedis jedis = pool.getResource();
jedis.incr("foo");
jedis.close();
pool.close();
pool.destroy();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void run() {
for (Thread t : tds)
t.join();

pool.close();
pool.destroy();

}
}

0 comments on commit f2c4062

Please sign in to comment.