Replace Closer class to use its close() method
This commit is contained in:
@@ -1,39 +0,0 @@
|
||||
package redis.clients.jedis.tests;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
class Closer implements Closeable {
|
||||
private final Set<Closeable> elements = new HashSet<Closeable>();
|
||||
|
||||
synchronized <T extends Closeable> T register(T element) {
|
||||
if (element != null) {
|
||||
elements.add(element);
|
||||
}
|
||||
return element;
|
||||
}
|
||||
|
||||
public synchronized void close() throws IOException {
|
||||
Throwable caught = null;
|
||||
|
||||
for (Closeable element : elements) {
|
||||
try {
|
||||
element.close();
|
||||
}
|
||||
catch (Throwable t) {
|
||||
caught = t;
|
||||
}
|
||||
}
|
||||
|
||||
elements.clear();
|
||||
|
||||
if (caught != null) {
|
||||
if (caught instanceof IOException) {
|
||||
throw (IOException) caught;
|
||||
}
|
||||
throw (RuntimeException) caught;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,5 @@
|
||||
package redis.clients.jedis.tests;
|
||||
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
@@ -15,37 +10,35 @@ import redis.clients.jedis.exceptions.JedisConnectionException;
|
||||
|
||||
public class ConnectionCloseTest extends Assert {
|
||||
|
||||
private final Closer closer = new Closer();
|
||||
|
||||
private Connection client;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
client = closer.register(new Connection());
|
||||
client = new Connection();
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
closer.close();
|
||||
client.close();
|
||||
}
|
||||
|
||||
@Test(expected = JedisConnectionException.class)
|
||||
public void checkUnkownHost() {
|
||||
client.setHost("someunknownhost");
|
||||
client.connect();
|
||||
client.setHost("someunknownhost");
|
||||
client.connect();
|
||||
}
|
||||
|
||||
@Test(expected = JedisConnectionException.class)
|
||||
public void checkWrongPort() {
|
||||
client.setHost("localhost");
|
||||
client.setPort(55665);
|
||||
client.connect();
|
||||
client.setHost("localhost");
|
||||
client.setPort(55665);
|
||||
client.connect();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void connectIfNotConnectedWhenSettingTimeoutInfinite() {
|
||||
client.setHost("localhost");
|
||||
client.setPort(6379);
|
||||
client.setTimeoutInfinite();
|
||||
client.setHost("localhost");
|
||||
client.setPort(6379);
|
||||
client.setTimeoutInfinite();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,15 +32,14 @@ public class JedisPoolTest extends Assert {
|
||||
|
||||
@Test
|
||||
public void checkCloseableConnections() throws Exception {
|
||||
Closer closer = new Closer();
|
||||
JedisPool pool = closer.register(new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||
hnp.getPort(), 2000));
|
||||
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||
hnp.getPort(), 2000);
|
||||
Jedis jedis = pool.getResource();
|
||||
jedis.auth("foobared");
|
||||
jedis.set("foo", "bar");
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
pool.returnResource(jedis);
|
||||
closer.close();
|
||||
pool.close();
|
||||
assertTrue(pool.isClosed());
|
||||
}
|
||||
|
||||
|
||||
@@ -39,15 +39,14 @@ public class JedisSentinelPoolTest extends JedisTestBase {
|
||||
public void checkCloseableConnections() throws Exception {
|
||||
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
||||
|
||||
Closer closer = new Closer();
|
||||
JedisSentinelPool pool = closer.register(new JedisSentinelPool(
|
||||
MASTER_NAME, sentinels, config, 1000, "foobared", 2));
|
||||
JedisSentinelPool pool = new JedisSentinelPool(
|
||||
MASTER_NAME, sentinels, config, 1000, "foobared", 2);
|
||||
Jedis jedis = pool.getResource();
|
||||
jedis.auth("foobared");
|
||||
jedis.set("foo", "bar");
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
pool.returnResource(jedis);
|
||||
closer.close();
|
||||
pool.close();
|
||||
assertTrue(pool.isClosed());
|
||||
}
|
||||
|
||||
|
||||
@@ -56,14 +56,13 @@ public class ShardedJedisPoolTest extends Assert {
|
||||
|
||||
@Test
|
||||
public void checkCloseableConnections() throws Exception {
|
||||
Closer closer = new Closer();
|
||||
ShardedJedisPool pool = closer.register(new ShardedJedisPool(
|
||||
new GenericObjectPoolConfig(), shards));
|
||||
ShardedJedisPool pool = new ShardedJedisPool(
|
||||
new GenericObjectPoolConfig(), shards);
|
||||
ShardedJedis jedis = pool.getResource();
|
||||
jedis.set("foo", "bar");
|
||||
assertEquals("bar", jedis.get("foo"));
|
||||
pool.returnResource(jedis);
|
||||
closer.close();
|
||||
pool.close();
|
||||
assertTrue(pool.isClosed());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user