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;
|
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.After;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
@@ -15,37 +10,35 @@ import redis.clients.jedis.exceptions.JedisConnectionException;
|
|||||||
|
|
||||||
public class ConnectionCloseTest extends Assert {
|
public class ConnectionCloseTest extends Assert {
|
||||||
|
|
||||||
private final Closer closer = new Closer();
|
|
||||||
|
|
||||||
private Connection client;
|
private Connection client;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
client = closer.register(new Connection());
|
client = new Connection();
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void tearDown() throws Exception {
|
public void tearDown() throws Exception {
|
||||||
closer.close();
|
client.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = JedisConnectionException.class)
|
@Test(expected = JedisConnectionException.class)
|
||||||
public void checkUnkownHost() {
|
public void checkUnkownHost() {
|
||||||
client.setHost("someunknownhost");
|
client.setHost("someunknownhost");
|
||||||
client.connect();
|
client.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected = JedisConnectionException.class)
|
@Test(expected = JedisConnectionException.class)
|
||||||
public void checkWrongPort() {
|
public void checkWrongPort() {
|
||||||
client.setHost("localhost");
|
client.setHost("localhost");
|
||||||
client.setPort(55665);
|
client.setPort(55665);
|
||||||
client.connect();
|
client.connect();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void connectIfNotConnectedWhenSettingTimeoutInfinite() {
|
public void connectIfNotConnectedWhenSettingTimeoutInfinite() {
|
||||||
client.setHost("localhost");
|
client.setHost("localhost");
|
||||||
client.setPort(6379);
|
client.setPort(6379);
|
||||||
client.setTimeoutInfinite();
|
client.setTimeoutInfinite();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,15 +32,14 @@ public class JedisPoolTest extends Assert {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkCloseableConnections() throws Exception {
|
public void checkCloseableConnections() throws Exception {
|
||||||
Closer closer = new Closer();
|
JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
||||||
JedisPool pool = closer.register(new JedisPool(new JedisPoolConfig(), hnp.getHost(),
|
hnp.getPort(), 2000);
|
||||||
hnp.getPort(), 2000));
|
|
||||||
Jedis jedis = pool.getResource();
|
Jedis jedis = pool.getResource();
|
||||||
jedis.auth("foobared");
|
jedis.auth("foobared");
|
||||||
jedis.set("foo", "bar");
|
jedis.set("foo", "bar");
|
||||||
assertEquals("bar", jedis.get("foo"));
|
assertEquals("bar", jedis.get("foo"));
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
closer.close();
|
pool.close();
|
||||||
assertTrue(pool.isClosed());
|
assertTrue(pool.isClosed());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -39,15 +39,14 @@ public class JedisSentinelPoolTest extends JedisTestBase {
|
|||||||
public void checkCloseableConnections() throws Exception {
|
public void checkCloseableConnections() throws Exception {
|
||||||
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
|
||||||
|
|
||||||
Closer closer = new Closer();
|
JedisSentinelPool pool = new JedisSentinelPool(
|
||||||
JedisSentinelPool pool = closer.register(new JedisSentinelPool(
|
MASTER_NAME, sentinels, config, 1000, "foobared", 2);
|
||||||
MASTER_NAME, sentinels, config, 1000, "foobared", 2));
|
|
||||||
Jedis jedis = pool.getResource();
|
Jedis jedis = pool.getResource();
|
||||||
jedis.auth("foobared");
|
jedis.auth("foobared");
|
||||||
jedis.set("foo", "bar");
|
jedis.set("foo", "bar");
|
||||||
assertEquals("bar", jedis.get("foo"));
|
assertEquals("bar", jedis.get("foo"));
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
closer.close();
|
pool.close();
|
||||||
assertTrue(pool.isClosed());
|
assertTrue(pool.isClosed());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -56,14 +56,13 @@ public class ShardedJedisPoolTest extends Assert {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void checkCloseableConnections() throws Exception {
|
public void checkCloseableConnections() throws Exception {
|
||||||
Closer closer = new Closer();
|
ShardedJedisPool pool = new ShardedJedisPool(
|
||||||
ShardedJedisPool pool = closer.register(new ShardedJedisPool(
|
new GenericObjectPoolConfig(), shards);
|
||||||
new GenericObjectPoolConfig(), shards));
|
|
||||||
ShardedJedis jedis = pool.getResource();
|
ShardedJedis jedis = pool.getResource();
|
||||||
jedis.set("foo", "bar");
|
jedis.set("foo", "bar");
|
||||||
assertEquals("bar", jedis.get("foo"));
|
assertEquals("bar", jedis.get("foo"));
|
||||||
pool.returnResource(jedis);
|
pool.returnResource(jedis);
|
||||||
closer.close();
|
pool.close();
|
||||||
assertTrue(pool.isClosed());
|
assertTrue(pool.isClosed());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user