diff --git a/src/main/java/redis/clients/jedis/BinaryClient.java b/src/main/java/redis/clients/jedis/BinaryClient.java index 8487c05a7f..993be834da 100644 --- a/src/main/java/redis/clients/jedis/BinaryClient.java +++ b/src/main/java/redis/clients/jedis/BinaryClient.java @@ -40,8 +40,8 @@ public class BinaryClient extends Connection { private boolean isInMulti; - private String user; - private String password; + @Deprecated private String user; + @Deprecated private String password; private int db; @@ -51,6 +51,12 @@ public BinaryClient() { super(); } + /** + * @param host + * @deprecated This constructor will be removed in future. It can be replaced with + * {@link #BinaryClient(java.lang.String, int)} with the host and {@link Protocol#DEFAULT_PORT}. + */ + @Deprecated public BinaryClient(final String host) { super(host); } @@ -59,16 +65,30 @@ public BinaryClient(final String host, final int port) { super(host, port); } + /** + * @deprecated This constructor will be removed in future. Use + * {@link #BinaryClient(redis.clients.jedis.HostAndPort, redis.clients.jedis.JedisClientConfig)}. + */ + @Deprecated public BinaryClient(final String host, final int port, final boolean ssl) { super(host, port, ssl); } + /** + * @deprecated This constructor will be removed in future. Use + * {@link #BinaryClient(redis.clients.jedis.HostAndPort, redis.clients.jedis.JedisClientConfig)}. + */ + @Deprecated public BinaryClient(final String host, final int port, final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { super(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier); } + public BinaryClient(final HostAndPort hostPort, final JedisClientConfig clientConfig) { + super(hostPort, clientConfig); + } + public BinaryClient(final JedisSocketFactory jedisSocketFactory) { super(jedisSocketFactory); } @@ -81,31 +101,39 @@ public boolean isInWatch() { return isInWatch; } - private byte[][] joinParameters(byte[] first, byte[][] rest) { - byte[][] result = new byte[rest.length + 1][]; - result[0] = first; - System.arraycopy(rest, 0, result, 1, rest.length); - return result; - } - - private byte[][] joinParameters(byte[] first, byte[] second, byte[][] rest) { - byte[][] result = new byte[rest.length + 2][]; - result[0] = first; - result[1] = second; - System.arraycopy(rest, 0, result, 2, rest.length); - return result; + /** + * @param user + * @deprecated This method will be removed in future. Because this class will be restricted from + * holding any user data. + */ + @Deprecated + public void setUser(final String user) { + this.user = user; } - public void setUser(final String user) { this.user = user; } - + /** + * @param password + * @deprecated This method will be removed in future. Because this class will be restricted from + * holding any user data. + */ + @Deprecated public void setPassword(final String password) { this.password = password; } + + /** + * This method should be called only after a successful SELECT command. + * @param db + */ public void setDb(int db) { this.db = db; } + public int getDB() { + return db; + } + @Override public void connect() { if (!isConnected()) { @@ -124,6 +152,25 @@ public void connect() { } } + @Override + public void disconnect() { + db = 0; + super.disconnect(); + } + + @Override + public void close() { + db = 0; + super.close(); + } + + public void resetState() { + if (isInWatch()) { + unwatch(); + getStatusCodeReply(); + } + } + public void ping() { sendCommand(PING); } @@ -973,29 +1020,6 @@ public void getrange(final byte[] key, final long startOffset, final long endOff sendCommand(GETRANGE, key, toByteArray(startOffset), toByteArray(endOffset)); } - public int getDB() { - return db; - } - - @Override - public void disconnect() { - db = 0; - super.disconnect(); - } - - @Override - public void close() { - db = 0; - super.close(); - } - - public void resetState() { - if (isInWatch()) { - unwatch(); - getStatusCodeReply(); - } - } - public void eval(final byte[] script, final byte[] keyCount, final byte[][] params) { sendCommand(EVAL, joinParameters(script, keyCount, params)); } @@ -1654,4 +1678,18 @@ public void xinfoConsumers (byte[] key, byte[] group) { sendCommand(XINFO,Keyword.CONSUMERS.getRaw(),key,group); } + private static byte[][] joinParameters(byte[] first, byte[][] rest) { + byte[][] result = new byte[rest.length + 1][]; + result[0] = first; + System.arraycopy(rest, 0, result, 1, rest.length); + return result; + } + + private static byte[][] joinParameters(byte[] first, byte[] second, byte[][] rest) { + byte[][] result = new byte[rest.length + 2][]; + result[0] = first; + result[1] = second; + System.arraycopy(rest, 0, result, 2, rest.length); + return result; + } } diff --git a/src/main/java/redis/clients/jedis/BinaryJedis.java b/src/main/java/redis/clients/jedis/BinaryJedis.java index 28197129cb..a43a7df63c 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedis.java +++ b/src/main/java/redis/clients/jedis/BinaryJedis.java @@ -43,7 +43,7 @@ public class BinaryJedis implements BasicCommands, BinaryJedisCommands, MultiKeyBinaryCommands, AdvancedBinaryJedisCommands, BinaryScriptingCommands, Closeable { - protected Client client = null; + protected final Client client; protected Transaction transaction = null; protected Pipeline pipeline = null; protected static final byte[][] DUMMY_ARRAY = new byte[0][]; @@ -52,31 +52,85 @@ public BinaryJedis() { client = new Client(); } - public BinaryJedis(final String host) { - URI uri = URI.create(host); + /** + * @deprecated This constructor will not support a host string in future. It will accept only a + * uri string. {@link JedisURIHelper#isValid(java.net.URI)} can used before this. If this + * constructor was being used with a host, it can be replaced with + * {@link #BinaryJedis(java.lang.String, int)} with the host and {@link Protocol#DEFAULT_PORT}. + * @param uriString + */ + @Deprecated + public BinaryJedis(final String uriString) { + URI uri = URI.create(uriString); if (JedisURIHelper.isValid(uri)) { - initializeClientFromURI(uri); + client = createClientFromURI(uri); + initializeFromURI(uri); } else { - client = new Client(host); + client = new Client(uriString); } } public BinaryJedis(final HostAndPort hp) { - this(hp.getHost(), hp.getPort()); + this(hp, DefaultJedisClientConfig.builder().build()); } public BinaryJedis(final String host, final int port) { client = new Client(host, port); } + public BinaryJedis(final String host, final int port, final JedisClientConfig config) { + this(new HostAndPort(host, port), config); + } + + public BinaryJedis(final HostAndPort hostPort, final JedisClientConfig config) { + client = new Client(hostPort, config); + initializeFromClientConfig(config); + } + + private void initializeFromClientConfig(JedisClientConfig config) { + try { + connect(); + String password = config.getPassword(); + if (password != null) { + String user = config.getUser(); + if (user != null) { + auth(user, password); + } else { + auth(password); + } + } + int dbIndex = config.getDatabase(); + if (dbIndex > 0) { + select(dbIndex); + } + String clientName = config.getClientName(); + if (clientName != null) { + // TODO: need to figure out something without encoding + clientSetname(redis.clients.jedis.util.SafeEncoder.encode(clientName)); + } + } catch (JedisException je) { + try { + if (isConnected()) { + quit(); + } + disconnect(); + } catch (Exception e) { + // + } + throw je; + } + } + public BinaryJedis(final String host, final int port, final boolean ssl) { - client = new Client(host, port, ssl); + this(host, port, DefaultJedisClientConfig.builder().withSsl(ssl).build()); } public BinaryJedis(final String host, final int port, final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - client = new Client(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier); + this(host, port, DefaultJedisClientConfig.builder().withSsl(ssl) + .withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).build()); } public BinaryJedis(final String host, final int port, final int timeout) { @@ -95,62 +149,61 @@ public BinaryJedis(final String host, final int port, final int timeout, final b public BinaryJedis(final String host, final int port, final int connectionTimeout, final int soTimeout) { - client = new Client(host, port); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); + this(host, port, DefaultJedisClientConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout).build()); } public BinaryJedis(final String host, final int port, final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout) { - client = new Client(host, port); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); - client.setInfiniteSoTimeout(infiniteSoTimeout); + this(host, port, DefaultJedisClientConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout) + .withInfiniteSoTimeout(infiniteSoTimeout).build()); } public BinaryJedis(final String host, final int port, final int connectionTimeout, final int soTimeout, final boolean ssl) { - client = new Client(host, port, ssl); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); + this(host, port, DefaultJedisClientConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout).withSsl(ssl).build()); } public BinaryJedis(final String host, final int port, final int connectionTimeout, final int soTimeout, final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - client = new Client(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); + this(host, port, DefaultJedisClientConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout).withSsl(ssl) + .withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).build()); } public BinaryJedis(final String host, final int port, final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout, final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - client = new Client(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); - client.setInfiniteSoTimeout(infiniteSoTimeout); + this(host, port, DefaultJedisClientConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout) + .withInfiniteSoTimeout(infiniteSoTimeout).withSsl(ssl) + .withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).build()); } public BinaryJedis(final JedisShardInfo shardInfo) { - client = new Client(shardInfo.getHost(), shardInfo.getPort(), shardInfo.getSsl(), - shardInfo.getSslSocketFactory(), shardInfo.getSslParameters(), - shardInfo.getHostnameVerifier()); - client.setConnectionTimeout(shardInfo.getConnectionTimeout()); - client.setSoTimeout(shardInfo.getSoTimeout()); - client.setUser(shardInfo.getUser()); - client.setPassword(shardInfo.getPassword()); - client.setDb(shardInfo.getDb()); + this(shardInfo.getHost(), shardInfo.getPort(), DefaultJedisClientConfig.builder() + .withConnectionTimeout(shardInfo.getConnectionTimeout()).withSoTimeout(shardInfo.getSoTimeout()) + .withUser(shardInfo.getUser()).withPassword(shardInfo.getPassword()).withDatabse(shardInfo.getDb()) + .withSsl(shardInfo.getSsl()).withSslSocketFactory(shardInfo.getSslSocketFactory()) + .withSslParameters(shardInfo.getSslParameters()).withHostnameVerifier(shardInfo.getHostnameVerifier()).build()); } public BinaryJedis(URI uri) { - initializeClientFromURI(uri); + client = createClientFromURI(uri); + initializeFromURI(uri); } public BinaryJedis(URI uri, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - initializeClientFromURI(uri, sslSocketFactory, sslParameters, hostnameVerifier); + this(uri, DefaultJedisClientConfig.builder() + .withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).build()); } public BinaryJedis(final URI uri, final int timeout) { @@ -163,65 +216,72 @@ public BinaryJedis(final URI uri, final int timeout, final SSLSocketFactory sslS } public BinaryJedis(final URI uri, final int connectionTimeout, final int soTimeout) { - initializeClientFromURI(uri); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); + this(uri, DefaultJedisClientConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout).build()); } public BinaryJedis(final URI uri, final int connectionTimeout, final int soTimeout, final SSLSocketFactory sslSocketFactory,final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - initializeClientFromURI(uri, sslSocketFactory, sslParameters, hostnameVerifier); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); + this(uri, DefaultJedisClientConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout) + .withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).build()); } public BinaryJedis(final URI uri, final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { - initializeClientFromURI(uri, sslSocketFactory, sslParameters, hostnameVerifier); - client.setConnectionTimeout(connectionTimeout); - client.setSoTimeout(soTimeout); - client.setInfiniteSoTimeout(infiniteSoTimeout); + this(uri, DefaultJedisClientConfig.builder() + .withConnectionTimeout(connectionTimeout).withSoTimeout(soTimeout) + .withInfiniteSoTimeout(infiniteSoTimeout).withSslSocketFactory(sslSocketFactory) + .withSslParameters(sslParameters).withHostnameVerifier(hostnameVerifier).build()); } - public BinaryJedis(final JedisSocketFactory jedisSocketFactory) { - client = new Client(jedisSocketFactory); - } - - private void initializeClientFromURI(URI uri) { - initializeClientFromURI(uri, null, null, null); - } - - private void initializeClientFromURI(URI uri, final SSLSocketFactory sslSocketFactory, - final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { + public BinaryJedis(final URI uri, JedisClientConfig config) { if (!JedisURIHelper.isValid(uri)) { - throw new InvalidURIException(String.format( - "Cannot open Redis connection due invalid URI. %s", uri.toString())); + throw new InvalidURIException(String.format("Cannot open Redis connection due invalid URI \"%s\".", uri.toString())); } + client = new Client(new HostAndPort(uri.getHost(), uri.getPort()), + DefaultJedisClientConfig.builder().withConnectionTimeout(config.getConnectionTimeout()) + .withSoTimeout(config.getSoTimeout()).withInfiniteSoTimeout(config.getInfiniteSoTimeout()) + .withUser(JedisURIHelper.getUser(uri)).withPassword(JedisURIHelper.getPassword(uri)) + .withDatabse(JedisURIHelper.getDBIndex(uri)).withClientName(config.getClientName()) + .withSsl(JedisURIHelper.isRedisSSLScheme(uri)) + .withSslSocketFactory(config.getSslSocketFactory()) + .withSslParameters(config.getSslParameters()) + .withHostnameVerifier(config.getHostnameVerifier()).build()); + initializeFromURI(uri); + } + + private static Client createClientFromURI(URI uri) { + if (!JedisURIHelper.isValid(uri)) { + throw new InvalidURIException(String.format("Cannot open Redis connection due invalid URI \"%s\".", uri.toString())); + } + return new Client(new HostAndPort(uri.getHost(), uri.getPort()), + DefaultJedisClientConfig.builder().withSsl(JedisURIHelper.isRedisSSLScheme(uri)).build()); + } - client = new Client(uri.getHost(), uri.getPort(), JedisURIHelper.isRedisSSLScheme(uri), - sslSocketFactory, sslParameters, hostnameVerifier); - + private void initializeFromURI(URI uri) { String password = JedisURIHelper.getPassword(uri); if (password != null) { String user = JedisURIHelper.getUser(uri); - if (user == null) { - client.auth(password); + if (user != null) { + auth(user, password); } else { - client.auth(user, password); + auth(password); } - client.getStatusCodeReply(); } - int dbIndex = JedisURIHelper.getDBIndex(uri); if (dbIndex > 0) { - client.select(dbIndex); - client.getStatusCodeReply(); - client.setDb(dbIndex); + select(dbIndex); } } + public BinaryJedis(final JedisSocketFactory jedisSocketFactory) { + client = new Client(jedisSocketFactory); + } + public boolean isConnected() { return client.isConnected(); } @@ -230,6 +290,44 @@ public boolean isBroken() { return client.isBroken(); } + public void connect() { + client.connect(); + } + + public void disconnect() { + client.disconnect(); + } + + public void resetState() { + if (isConnected()) { + if (transaction != null) { + transaction.close(); + } + + if (pipeline != null) { + pipeline.close(); + } + + client.resetState(); + } + + transaction = null; + pipeline = null; + } + + @Override + public void close() { + client.close(); + } + + @Override + public int getDB() { + return client.getDB(); + } + + /** + * @return PONG + */ @Override public String ping() { checkIsInMultiOrPipeline(); @@ -238,7 +336,7 @@ public String ping() { } /** - * Works same as ping() but returns argument message instead of PONG. + * Works same as {@link #ping()} but returns argument message instead of PONG. * @param message * @return message */ @@ -1454,6 +1552,7 @@ public Long lpos(final byte[] key, final byte[] element, final LPosParams params * @see #lpos(byte[], byte[], LPosParams, long) * @param key * @param element + * @param params * @param count * @return Returns value will be a list containing position of the matching elements inside the list. */ @@ -2059,31 +2158,6 @@ protected void checkIsInMultiOrPipeline() { } } - public void connect() { - client.connect(); - } - - public void disconnect() { - client.disconnect(); - } - - public void resetState() { - if (isConnected()) { - if (transaction != null) { - transaction.close(); - } - - if (pipeline != null) { - pipeline.close(); - } - - client.resetState(); - } - - transaction = null; - pipeline = null; - } - @Override public String watch(final byte[]... keys) { checkIsInMultiOrPipeline(); @@ -2098,11 +2172,6 @@ public String unwatch() { return client.getStatusCodeReply(); } - @Override - public void close() { - client.close(); - } - /** * Sort a Set or a List. *

@@ -3491,11 +3560,6 @@ public void psubscribe(BinaryJedisPubSub jedisPubSub, final byte[]... patterns) } } - @Override - public int getDB() { - return client.getDB(); - } - /** * Evaluates scripts using the Lua interpreter built into Redis starting from version 2.6.0. *

diff --git a/src/main/java/redis/clients/jedis/BinaryJedisCluster.java b/src/main/java/redis/clients/jedis/BinaryJedisCluster.java index e48188ef1a..3afe10473b 100644 --- a/src/main/java/redis/clients/jedis/BinaryJedisCluster.java +++ b/src/main/java/redis/clients/jedis/BinaryJedisCluster.java @@ -84,21 +84,32 @@ public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeo this(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, user, password, clientName, poolConfig, ssl, null, null, null, null); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String password, String clientName, GenericObjectPoolConfig poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { this(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, null, password, clientName, poolConfig, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String user, String password, String clientName, GenericObjectPoolConfig poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { - this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig, - connectionTimeout, soTimeout, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); - this.maxAttempts = maxAttempts; + this(jedisClusterNode, connectionTimeout, soTimeout, 0, maxAttempts, user, password, clientName, poolConfig, + ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int infiniteSoTimeout, int maxAttempts, String user, String password, String clientName, GenericObjectPoolConfig poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { @@ -107,6 +118,12 @@ public BinaryJedisCluster(Set jedisClusterNode, int connectionTimeo this.maxAttempts = maxAttempts; } + public BinaryJedisCluster(Set jedisClusterNode, JedisClientConfig clientConfig, + int maxAttempts, GenericObjectPoolConfig poolConfig) { + this.connectionHandler = new JedisSlotBasedConnectionHandler(jedisClusterNode, poolConfig, clientConfig); + this.maxAttempts = maxAttempts; + } + @Override public void close() { if (connectionHandler != null) { diff --git a/src/main/java/redis/clients/jedis/Client.java b/src/main/java/redis/clients/jedis/Client.java index 91ce04b072..3343562e3b 100644 --- a/src/main/java/redis/clients/jedis/Client.java +++ b/src/main/java/redis/clients/jedis/Client.java @@ -29,6 +29,12 @@ public Client() { super(); } + /** + * @param host + * @deprecated This constructor will be removed in future. It can be replaced with + * {@link #Client(java.lang.String, int)} with the host and {@link Protocol#DEFAULT_PORT}. + */ + @Deprecated public Client(final String host) { super(host); } @@ -37,16 +43,28 @@ public Client(final String host, final int port) { super(host, port); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public Client(final String host, final int port, final boolean ssl) { super(host, port, ssl); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public Client(final String host, final int port, final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { super(host, port, ssl, sslSocketFactory, sslParameters, hostnameVerifier); } + public Client(final HostAndPort hostPort, final JedisClientConfig clientConfig) { + super(hostPort, clientConfig); + } + public Client(final JedisSocketFactory jedisSocketFactory) { super(jedisSocketFactory); } diff --git a/src/main/java/redis/clients/jedis/Connection.java b/src/main/java/redis/clients/jedis/Connection.java index 6d533c549a..20abce5b47 100644 --- a/src/main/java/redis/clients/jedis/Connection.java +++ b/src/main/java/redis/clients/jedis/Connection.java @@ -23,38 +23,62 @@ public class Connection implements Closeable { private static final byte[][] EMPTY_ARGS = new byte[0][]; - private JedisSocketFactory jedisSocketFactory; + private boolean socketParamModified = false; // for backward compatibility + private JedisSocketFactory socketFactory; // TODO: sould be final private Socket socket; private RedisOutputStream outputStream; private RedisInputStream inputStream; + private int soTimeout = Protocol.DEFAULT_TIMEOUT; private int infiniteSoTimeout = 0; private boolean broken = false; public Connection() { - this(Protocol.DEFAULT_HOST); + this(Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT); } + /** + * @param host + * @deprecated This constructor will be removed in future. It can be replaced with + * {@link #Connection(java.lang.String, int)} with the host and {@link Protocol#DEFAULT_PORT}. + */ + @Deprecated public Connection(final String host) { this(host, Protocol.DEFAULT_PORT); } public Connection(final String host, final int port) { - this(host, port, false); + this(new HostAndPort(host, port), DefaultJedisClientConfig.builder().build()); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public Connection(final String host, final int port, final boolean ssl) { - this(host, port, ssl, null, null, null); + this(new HostAndPort(host, port), DefaultJedisClientConfig.builder().withSsl(ssl).build()); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public Connection(final String host, final int port, final boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) { - this(new DefaultJedisSocketFactory(host, port, Protocol.DEFAULT_TIMEOUT, - Protocol.DEFAULT_TIMEOUT, ssl, sslSocketFactory, sslParameters, hostnameVerifier)); + this(new HostAndPort(host, port), DefaultJedisClientConfig.builder().withSsl(ssl) + .withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).build()); + } + + public Connection(final HostAndPort hostAndPort, final JedisClientConfig clientConfig) { + this(new DefaultJedisSocketFactory(hostAndPort, clientConfig)); + this.soTimeout = clientConfig.getSoTimeout(); + this.infiniteSoTimeout = clientConfig.getInfiniteSoTimeout(); } public Connection(final JedisSocketFactory jedisSocketFactory) { - this.jedisSocketFactory = jedisSocketFactory; + this.socketFactory = jedisSocketFactory; + this.soTimeout = jedisSocketFactory.getSoTimeout(); } public Socket getSocket() { @@ -62,19 +86,34 @@ public Socket getSocket() { } public int getConnectionTimeout() { - return jedisSocketFactory.getConnectionTimeout(); + return socketFactory.getConnectionTimeout(); } public int getSoTimeout() { - return jedisSocketFactory.getSoTimeout(); + return soTimeout; } + /** + * @param connectionTimeout + * @deprecated This method is not supported anymore and is kept for backward compatibility. It + * will be removed in future. + */ + @Deprecated public void setConnectionTimeout(int connectionTimeout) { - jedisSocketFactory.setConnectionTimeout(connectionTimeout); + socketFactory.setConnectionTimeout(connectionTimeout); } public void setSoTimeout(int soTimeout) { - jedisSocketFactory.setSoTimeout(soTimeout); + socketFactory.setSoTimeout(soTimeout); + this.soTimeout = soTimeout; + if (this.socket != null) { + try { + this.socket.setSoTimeout(soTimeout); + } catch (SocketException ex) { + broken = true; + throw new JedisConnectionException(ex); + } + } } public void setInfiniteSoTimeout(int infiniteSoTimeout) { @@ -95,7 +134,7 @@ public void setTimeoutInfinite() { public void rollbackTimeout() { try { - socket.setSoTimeout(jedisSocketFactory.getSoTimeout()); + socket.setSoTimeout(socketFactory.getSoTimeout()); } catch (SocketException ex) { broken = true; throw new JedisConnectionException(ex); @@ -142,32 +181,57 @@ public void sendCommand(final ProtocolCommand cmd, final byte[]... args) { } public String getHost() { - return jedisSocketFactory.getHost(); + return socketFactory.getHost(); } + /** + * @param host + * @deprecated This method will be removed in future. + */ + @Deprecated public void setHost(final String host) { - jedisSocketFactory.setHost(host); + socketFactory.setHost(host); + socketParamModified = true; } public int getPort() { - return jedisSocketFactory.getPort(); + return socketFactory.getPort(); } + /** + * @param port + * @deprecated This method will be removed in future. + */ + @Deprecated public void setPort(final int port) { - jedisSocketFactory.setPort(port); + socketFactory.setPort(port); + socketParamModified = true; } - public void connect() { + public void connect() throws JedisConnectionException { + if (socketParamModified) { // this is only for backward compatibility + try { + disconnect(); + } catch(Exception e) { + // swallow + } + } if (!isConnected()) { try { - socket = jedisSocketFactory.createSocket(); + socket = socketFactory.createSocket(); outputStream = new RedisOutputStream(socket.getOutputStream()); inputStream = new RedisInputStream(socket.getInputStream()); - } catch (IOException ex) { + } catch (IOException ioe) { + broken = true; + throw new JedisConnectionException("Failed to create input/output stream", ioe); + } catch (JedisConnectionException jce) { broken = true; - throw new JedisConnectionException("Failed connecting to " - + jedisSocketFactory.getDescription(), ex); + throw jce; + } finally { + if (broken) { + IOUtils.closeQuietly(socket); + } } } } diff --git a/src/main/java/redis/clients/jedis/DefaultJedisClientConfig.java b/src/main/java/redis/clients/jedis/DefaultJedisClientConfig.java new file mode 100644 index 0000000000..6db777e97d --- /dev/null +++ b/src/main/java/redis/clients/jedis/DefaultJedisClientConfig.java @@ -0,0 +1,201 @@ +package redis.clients.jedis; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLParameters; +import javax.net.ssl.SSLSocketFactory; + +public final class DefaultJedisClientConfig implements JedisClientConfig { + + private final int connectionTimeout; + private final int soTimeout; + private final int infiniteSoTimeout; + + private final String user; + private final String password; + private final int database; + private final String clientName; + + private final boolean ssl; + private final SSLSocketFactory sslSocketFactory; + private final SSLParameters sslParameters; + private final HostnameVerifier hostnameVerifier; + + private final HostAndPortMapper hostAndPortMapper; + + private DefaultJedisClientConfig(int connectionTimeout, int soTimeout, int infiniteSoTimeout, + String user, String password, int database, String clientName, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, HostAndPortMapper hostAndPortMapper) { + this.connectionTimeout = connectionTimeout; + this.soTimeout = soTimeout; + this.infiniteSoTimeout = infiniteSoTimeout; + this.user = user; + this.password = password; + this.database = database; + this.clientName = clientName; + this.ssl = ssl; + this.sslSocketFactory = sslSocketFactory; + this.sslParameters = sslParameters; + this.hostnameVerifier = hostnameVerifier; + this.hostAndPortMapper = hostAndPortMapper; + } + + @Override + public int getConnectionTimeout() { + return connectionTimeout; + } + + @Override + public int getSoTimeout() { + return soTimeout; + } + + @Override + public int getInfiniteSoTimeout() { + return infiniteSoTimeout; + } + + @Override + public String getUser() { + return user; + } + + @Override + public String getPassword() { + return password; + } + + @Override + public int getDatabase() { + return database; + } + + @Override + public String getClientName() { + return clientName; + } + + @Override + public boolean isSsl() { + return ssl; + } + + @Override + public SSLSocketFactory getSslSocketFactory() { + return sslSocketFactory; + } + + @Override + public SSLParameters getSslParameters() { + return sslParameters; + } + + @Override + public HostnameVerifier getHostnameVerifier() { + return hostnameVerifier; + } + + @Override + public HostAndPortMapper getHostAndPortMapper() { + return hostAndPortMapper; + } + + public static Builder builder() { + return new Builder(); + } + + public static class Builder { + + private int connectionTimeout = Protocol.DEFAULT_TIMEOUT; + private int soTimeout = Protocol.DEFAULT_TIMEOUT; + private int infiniteSoTimeout = 0; + + private String user = null; + private String password = null; + private int databse = Protocol.DEFAULT_DATABASE; + private String clientName = null; + + private boolean ssl = false; + private SSLSocketFactory sslSocketFactory = null; + private SSLParameters sslParameters = null; + private HostnameVerifier hostnameVerifier = null; + + private HostAndPortMapper hostAndPortMapper = null; + + private Builder() { + } + + public DefaultJedisClientConfig build() { + return new DefaultJedisClientConfig(connectionTimeout, soTimeout, infiniteSoTimeout, + user, password, databse, clientName, + ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMapper); + } + + public Builder withConnectionTimeout(int connectionTimeout) { + this.connectionTimeout = connectionTimeout; + return this; + } + + public Builder withSoTimeout(int soTimeout) { + this.soTimeout = soTimeout; + return this; + } + + public Builder withInfiniteSoTimeout(int infiniteSoTimeout) { + this.infiniteSoTimeout = infiniteSoTimeout; + return this; + } + + public Builder withUser(String user) { + this.user = user; + return this; + } + + public Builder withPassword(String password) { + this.password = password; + return this; + } + + public Builder withDatabse(int databse) { + this.databse = databse; + return this; + } + + public Builder withClientName(String clientName) { + this.clientName = clientName; + return this; + } + + public Builder withSsl(boolean ssl) { + this.ssl = ssl; + return this; + } + + public Builder withSslSocketFactory(SSLSocketFactory sslSocketFactory) { + this.sslSocketFactory = sslSocketFactory; + return this; + } + + public Builder withSslParameters(SSLParameters sslParameters) { + this.sslParameters = sslParameters; + return this; + } + + public Builder withHostnameVerifier(HostnameVerifier hostnameVerifier) { + this.hostnameVerifier = hostnameVerifier; + return this; + } + + public Builder withHostAndPortMapper(HostAndPortMapper hostAndPortMapper) { + this.hostAndPortMapper = hostAndPortMapper; + return this; + } + } + + public static DefaultJedisClientConfig copyConfig(JedisClientConfig copy) { + return new DefaultJedisClientConfig(copy.getConnectionTimeout(), copy.getSoTimeout(), + copy.getInfiniteSoTimeout(), copy.getUser(), copy.getPassword(), copy.getDatabase(), + copy.getClientName(), copy.isSsl(), copy.getSslSocketFactory(), copy.getSslParameters(), + copy.getHostnameVerifier(), copy.getHostAndPortMapper()); + } +} diff --git a/src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java b/src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java index 1c914962a9..63200c2d9e 100644 --- a/src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java +++ b/src/main/java/redis/clients/jedis/DefaultJedisSocketFactory.java @@ -1,31 +1,41 @@ package redis.clients.jedis; -import redis.clients.jedis.exceptions.JedisConnectionException; - +import java.io.IOException; +import java.net.InetSocketAddress; +import java.net.Socket; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.SSLParameters; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; -import java.io.IOException; -import java.net.InetSocketAddress; -import java.net.Socket; + +import redis.clients.jedis.exceptions.JedisConnectionException; +import redis.clients.jedis.util.IOUtils; public class DefaultJedisSocketFactory implements JedisSocketFactory { - private String host; - private int port; - private int connectionTimeout; - private int soTimeout; - private boolean ssl; - private SSLSocketFactory sslSocketFactory; - private SSLParameters sslParameters; - private HostnameVerifier hostnameVerifier; + protected static final HostAndPort DEFAULT_HOST_AND_PORT = new HostAndPort(Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT); + + private HostAndPort hostAndPort = DEFAULT_HOST_AND_PORT; + private int connectionTimeout = Protocol.DEFAULT_TIMEOUT; + private int soTimeout = Protocol.DEFAULT_TIMEOUT; + private boolean ssl = false; + private SSLSocketFactory sslSocketFactory = null; + private SSLParameters sslParameters = null; + private HostnameVerifier hostnameVerifier = null; + private HostAndPortMapper hostAndPortMapper = null; + + public DefaultJedisSocketFactory() { + } + + public DefaultJedisSocketFactory(HostAndPort hostAndPort) { + this(hostAndPort, null); + } + @Deprecated public DefaultJedisSocketFactory(String host, int port, int connectionTimeout, int soTimeout, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) { - this.host = host; - this.port = port; + this.hostAndPort = new HostAndPort(host, port); this.connectionTimeout = connectionTimeout; this.soTimeout = soTimeout; this.ssl = ssl; @@ -34,77 +44,114 @@ public DefaultJedisSocketFactory(String host, int port, int connectionTimeout, i this.hostnameVerifier = hostnameVerifier; } + public DefaultJedisSocketFactory(HostAndPort hostAndPort, JedisClientConfig config) { + this.hostAndPort = hostAndPort; + if (config != null) { + this.connectionTimeout = config.getConnectionTimeout(); + this.soTimeout = config.getSoTimeout(); + this.ssl = config.isSsl(); + this.sslSocketFactory = config.getSslSocketFactory(); + this.sslParameters = config.getSslParameters(); + this.hostnameVerifier = config.getHostnameVerifier(); + this.hostAndPortMapper = config.getHostAndPortMapper(); + } + } + @Override - public Socket createSocket() throws IOException { + public Socket createSocket() throws JedisConnectionException { Socket socket = null; try { socket = new Socket(); // ->@wjw_add socket.setReuseAddress(true); - socket.setKeepAlive(true); // Will monitor the TCP connection is - // valid - socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to - // ensure timely delivery of data - socket.setSoLinger(true, 0); // Control calls close () method, - // the underlying socket is closed - // immediately + socket.setKeepAlive(true); // Will monitor the TCP connection is valid + socket.setTcpNoDelay(true); // Socket buffer Whetherclosed, to ensure timely delivery of data + socket.setSoLinger(true, 0); // Control calls close () method, the underlying socket is closed immediately // <-@wjw_add - socket.connect(new InetSocketAddress(getHost(), getPort()), getConnectionTimeout()); + HostAndPort hostAndPort = getSocketHostAndPort(); + socket.connect(new InetSocketAddress(hostAndPort.getHost(), hostAndPort.getPort()), getConnectionTimeout()); socket.setSoTimeout(getSoTimeout()); if (ssl) { + SSLSocketFactory sslSocketFactory = getSslSocketFactory(); if (null == sslSocketFactory) { sslSocketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); } - socket = sslSocketFactory.createSocket(socket, getHost(), getPort(), true); + socket = sslSocketFactory.createSocket(socket, hostAndPort.getHost(), hostAndPort.getPort(), true); + + SSLParameters sslParameters = getSslParameters(); if (null != sslParameters) { ((SSLSocket) socket).setSSLParameters(sslParameters); } - if ((null != hostnameVerifier) - && (!hostnameVerifier.verify(getHost(), ((SSLSocket) socket).getSession()))) { + + HostnameVerifier hostnameVerifier = getHostnameVerifier(); + if (null != hostnameVerifier + && !hostnameVerifier.verify(hostAndPort.getHost(), ((SSLSocket) socket).getSession())) { String message = String.format( - "The connection to '%s' failed ssl/tls hostname verification.", getHost()); + "The connection to '%s' failed ssl/tls hostname verification.", hostAndPort.getHost()); throw new JedisConnectionException(message); } } + return socket; - } catch (Exception ex) { - if (socket != null) { - socket.close(); + + } catch (IOException ex) { + + IOUtils.closeQuietly(socket); + + throw new JedisConnectionException("Failed to create socket.", ex); + } + } + + public HostAndPort getSocketHostAndPort() { + HostAndPortMapper mapper = getHostAndPortMapper(); + HostAndPort hostAndPort = getHostAndPort(); + if (mapper != null) { + HostAndPort mapped = mapper.getHostAndPort(hostAndPort); + if (mapped != null) { + return mapped; } - throw ex; } + return hostAndPort; + } + + public HostAndPort getHostAndPort() { + return this.hostAndPort; + } + + public void setHostAndPort(HostAndPort hostAndPort) { + this.hostAndPort = hostAndPort; } @Override public String getDescription() { - return host + ":" + port; + return this.hostAndPort.toString(); } @Override public String getHost() { - return host; + return this.hostAndPort.getHost(); } @Override public void setHost(String host) { - this.host = host; + this.hostAndPort = new HostAndPort(host, this.hostAndPort.getPort()); } @Override public int getPort() { - return port; + return this.hostAndPort.getPort(); } @Override public void setPort(int port) { - this.port = port; + this.hostAndPort = new HostAndPort(this.hostAndPort.getHost(), port); } @Override public int getConnectionTimeout() { - return connectionTimeout; + return this.connectionTimeout; } @Override @@ -114,11 +161,51 @@ public void setConnectionTimeout(int connectionTimeout) { @Override public int getSoTimeout() { - return soTimeout; + return this.soTimeout; } @Override public void setSoTimeout(int soTimeout) { this.soTimeout = soTimeout; } + + public boolean isSsl() { + return ssl; + } + + public void setSsl(boolean ssl) { + this.ssl = ssl; + } + + public SSLSocketFactory getSslSocketFactory() { + return sslSocketFactory; + } + + public void setSslSocketFactory(SSLSocketFactory sslSocketFactory) { + this.sslSocketFactory = sslSocketFactory; + } + + public SSLParameters getSslParameters() { + return sslParameters; + } + + public void setSslParameters(SSLParameters sslParameters) { + this.sslParameters = sslParameters; + } + + public HostnameVerifier getHostnameVerifier() { + return hostnameVerifier; + } + + public void setHostnameVerifier(HostnameVerifier hostnameVerifier) { + this.hostnameVerifier = hostnameVerifier; + } + + public HostAndPortMapper getHostAndPortMapper() { + return hostAndPortMapper; + } + + public void setHostAndPortMapper(HostAndPortMapper hostAndPortMapper) { + this.hostAndPortMapper = hostAndPortMapper; + } } diff --git a/src/main/java/redis/clients/jedis/HostAndPortMapper.java b/src/main/java/redis/clients/jedis/HostAndPortMapper.java new file mode 100644 index 0000000000..68727d9ca6 --- /dev/null +++ b/src/main/java/redis/clients/jedis/HostAndPortMapper.java @@ -0,0 +1,6 @@ +package redis.clients.jedis; + +public interface HostAndPortMapper { + + HostAndPort getHostAndPort(HostAndPort hap); +} diff --git a/src/main/java/redis/clients/jedis/Jedis.java b/src/main/java/redis/clients/jedis/Jedis.java index 31aee87fb8..f39893b2f4 100644 --- a/src/main/java/redis/clients/jedis/Jedis.java +++ b/src/main/java/redis/clients/jedis/Jedis.java @@ -45,14 +45,26 @@ public Jedis() { super(); } - public Jedis(final String host) { - super(host); + /** + * @deprecated This constructor will not support a host string in future. It will accept only a + * uri string. {@link JedisURIHelper#isValid(java.net.URI)} can used before this. If this + * constructor was being used with a host, it can be replaced with + * {@link #Jedis(java.lang.String, int)} with the host and {@link Protocol#DEFAULT_PORT}. + * @param uri + */ + @Deprecated + public Jedis(final String uri) { + super(uri); } public Jedis(final HostAndPort hp) { super(hp); } + public Jedis(final HostAndPort hp, final JedisClientConfig config) { + super(hp, config); + } + public Jedis(final String host, final int port) { super(host, port); } @@ -147,6 +159,10 @@ public Jedis(final URI uri, final int connectionTimeout, final int soTimeout, super(uri, connectionTimeout, soTimeout, infiniteSoTimeout, sslSocketFactory, sslParameters, hostnameVerifier); } + public Jedis(final URI uri, JedisClientConfig config) { + super(uri, config); + } + public Jedis(final JedisSocketFactory jedisSocketFactory) { super(jedisSocketFactory); } diff --git a/src/main/java/redis/clients/jedis/JedisClientConfig.java b/src/main/java/redis/clients/jedis/JedisClientConfig.java new file mode 100644 index 0000000000..4add4808c1 --- /dev/null +++ b/src/main/java/redis/clients/jedis/JedisClientConfig.java @@ -0,0 +1,37 @@ +package redis.clients.jedis; + +import javax.net.ssl.HostnameVerifier; +import javax.net.ssl.SSLParameters; +import javax.net.ssl.SSLSocketFactory; + +public interface JedisClientConfig { + + int getConnectionTimeout(); + + int getSoTimeout(); + + /** + * @return Socket timeout (in milliseconds) to use during blocking operation. Default is '0', + * which means to block forever. + */ + int getInfiniteSoTimeout(); + + String getUser(); + + String getPassword(); + + int getDatabase(); + + String getClientName(); + + boolean isSsl(); + + SSLSocketFactory getSslSocketFactory(); + + SSLParameters getSslParameters(); + + HostnameVerifier getHostnameVerifier(); + + HostAndPortMapper getHostAndPortMapper(); + +} diff --git a/src/main/java/redis/clients/jedis/JedisCluster.java b/src/main/java/redis/clients/jedis/JedisCluster.java index 8409f02440..b1631dadf2 100644 --- a/src/main/java/redis/clients/jedis/JedisCluster.java +++ b/src/main/java/redis/clients/jedis/JedisCluster.java @@ -83,6 +83,10 @@ public JedisCluster(HostAndPort node, int connectionTimeout, int soTimeout, int this(Collections.singleton(node), connectionTimeout, soTimeout, maxAttempts, user, password, clientName, poolConfig, ssl); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public JedisCluster(HostAndPort node, int connectionTimeout, int soTimeout, int maxAttempts, String password, String clientName, final GenericObjectPoolConfig poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -91,6 +95,10 @@ public JedisCluster(HostAndPort node, int connectionTimeout, int soTimeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public JedisCluster(HostAndPort node, int connectionTimeout, int soTimeout, int maxAttempts, String user, String password, String clientName, final GenericObjectPoolConfig poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -99,6 +107,10 @@ public JedisCluster(HostAndPort node, int connectionTimeout, int soTimeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + public JedisCluster(HostAndPort node, final JedisClientConfig clientConfig, int maxAttempts, final GenericObjectPoolConfig poolConfig) { + this(Collections.singleton(node), clientConfig, maxAttempts, poolConfig); + } + public JedisCluster(Set nodes) { this(nodes, DEFAULT_TIMEOUT); } @@ -161,6 +173,10 @@ public JedisCluster(Set jedisClusterNode, int connectionTimeout, in super(jedisClusterNode, connectionTimeout, soTimeout, maxAttempts, user, password, clientName, poolConfig, ssl); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public JedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String password, String clientName, final GenericObjectPoolConfig poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -169,6 +185,10 @@ public JedisCluster(Set jedisClusterNode, int connectionTimeout, in ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public JedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int infiniteSoTimeout, int maxAttempts, String password, String clientName, final GenericObjectPoolConfig poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -177,6 +197,10 @@ public JedisCluster(Set jedisClusterNode, int connectionTimeout, in clientName, poolConfig, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public JedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int maxAttempts, String user, String password, String clientName, final GenericObjectPoolConfig poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -185,6 +209,10 @@ public JedisCluster(Set jedisClusterNode, int connectionTimeout, in ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public JedisCluster(Set jedisClusterNode, int connectionTimeout, int soTimeout, int infiniteSoTimeout, int maxAttempts, String user, String password, String clientName, final GenericObjectPoolConfig poolConfig, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -193,6 +221,11 @@ public JedisCluster(Set jedisClusterNode, int connectionTimeout, in clientName, poolConfig, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + public JedisCluster(Set nodes, final JedisClientConfig clientConfig, int maxAttempts, + final GenericObjectPoolConfig poolConfig) { + super(nodes, clientConfig, maxAttempts, poolConfig); + } + @Override public String set(final String key, final String value) { return new JedisClusterCommand(connectionHandler, maxAttempts) { diff --git a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java index 473a5cbb53..f6ac4d1fe9 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisClusterConnectionHandler.java @@ -34,6 +34,10 @@ public JedisClusterConnectionHandler(Set nodes, final GenericObject this(nodes, poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, false, null, null, null, null); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String password, String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -41,6 +45,10 @@ public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolCo this(nodes, poolConfig, connectionTimeout, soTimeout, null, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, String user, String password, String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -48,14 +56,42 @@ public JedisClusterConnectionHandler(Set nodes, GenericObjectPoolCo this(nodes, poolConfig, connectionTimeout, soTimeout, 0, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public JedisClusterConnectionHandler(Set nodes, final GenericObjectPoolConfig poolConfig, int connectionTimeout, int soTimeout, int infiniteSoTimeout, String user, String password, String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap portMap) { - this.cache = new JedisClusterInfoCache(poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, - user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); - initializeSlotsCache(nodes, connectionTimeout, soTimeout, infiniteSoTimeout, - user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier); + this(nodes, + DefaultJedisClientConfig.builder().withConnectionTimeout(connectionTimeout) + .withSoTimeout(soTimeout).withInfiniteSoTimeout(infiniteSoTimeout) + .withUser(user).withPassword(password).withClientName(clientName) + .withSsl(ssl).withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).build(), + poolConfig, + DefaultJedisClientConfig.builder().withConnectionTimeout(connectionTimeout) + .withSoTimeout(soTimeout).withInfiniteSoTimeout(infiniteSoTimeout) + .withUser(user).withPassword(password).withClientName(clientName) + .withSsl(ssl).withSslSocketFactory(sslSocketFactory).withSslParameters(sslParameters) + .withHostnameVerifier(hostnameVerifier).withHostAndPortMapper(portMap).build()); + } + + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated + public JedisClusterConnectionHandler(Set nodes, final JedisClientConfig seedNodesClientConfig, + final GenericObjectPoolConfig poolConfig, final JedisClientConfig clusterNodesClientConfig) { + this.cache = new JedisClusterInfoCache(poolConfig, clusterNodesClientConfig); + initializeSlotsCache(nodes, seedNodesClientConfig); + } + + public JedisClusterConnectionHandler(Set nodes, + final GenericObjectPoolConfig poolConfig, final JedisClientConfig clientConfig) { + this.cache = new JedisClusterInfoCache(poolConfig, clientConfig); + initializeSlotsCache(nodes, clientConfig); } protected abstract Jedis getConnection(); @@ -70,22 +106,10 @@ public Map getNodes() { return cache.getNodes(); } - private void initializeSlotsCache(Set startNodes, - int connectionTimeout, int soTimeout, int infiniteSoTimeout, String user, String password, String clientName, - boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) { - for (HostAndPort hostAndPort : startNodes) { + private void initializeSlotsCache(Set startNodes, JedisClientConfig clientConfig) { - try (Jedis jedis = new Jedis(hostAndPort.getHost(), hostAndPort.getPort(), connectionTimeout, - soTimeout, infiniteSoTimeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier)) { - - if (user != null) { - jedis.auth(user, password); - } else if (password != null) { - jedis.auth(password); - } - if (clientName != null) { - jedis.clientSetname(clientName); - } + for (HostAndPort hostAndPort : startNodes) { + try (Jedis jedis = new Jedis(hostAndPort, clientConfig)) { cache.discoverClusterNodesAndSlots(jedis); return; } catch (JedisConnectionException e) { diff --git a/src/main/java/redis/clients/jedis/JedisClusterHostAndPortMap.java b/src/main/java/redis/clients/jedis/JedisClusterHostAndPortMap.java index 9a5a5029cb..f76b0f4d9a 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterHostAndPortMap.java +++ b/src/main/java/redis/clients/jedis/JedisClusterHostAndPortMap.java @@ -1,5 +1,15 @@ package redis.clients.jedis; -public interface JedisClusterHostAndPortMap { +/** + * @deprecated This will be removed in future. Prefer to use {@link HostAndPortMapper} instead. + */ +@Deprecated +public interface JedisClusterHostAndPortMap extends HostAndPortMapper { + HostAndPort getSSLHostAndPort(String host, int port); + + @Override + default HostAndPort getHostAndPort(HostAndPort hap) { + return getSSLHostAndPort(hap.getHost(), hap.getPort()); + } } diff --git a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java index b5242dd1e3..6d3ae52d1a 100644 --- a/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java +++ b/src/main/java/redis/clients/jedis/JedisClusterInfoCache.java @@ -25,20 +25,9 @@ public class JedisClusterInfoCache { private final Lock r = rwl.readLock(); private final Lock w = rwl.writeLock(); private volatile boolean rediscovering; - private final GenericObjectPoolConfig poolConfig; - - private int connectionTimeout; - private int soTimeout; - private int infiniteSoTimeout; - private String user; - private String password; - private String clientName; - private boolean ssl; - private SSLSocketFactory sslSocketFactory; - private SSLParameters sslParameters; - private HostnameVerifier hostnameVerifier; - private JedisClusterHostAndPortMap hostAndPortMap; + private final GenericObjectPoolConfig poolConfig; + private final JedisClientConfig clientConfig; private static final int MASTER_NODE_INDEX = 2; @@ -58,15 +47,19 @@ public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final String user, final String password, final String clientName) { - this(poolConfig, connectionTimeout, soTimeout, user, password, clientName, false, null, null, null, null); + this(poolConfig, connectionTimeout, soTimeout, user, password, clientName, false, null, null, null, (HostAndPortMapper) null); } public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout, final String user, final String password, final String clientName) { - this(poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, false, null, null, null, null); + this(poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, false, null, null, null, (HostAndPortMapper) null); } + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final String password, final String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -74,6 +67,17 @@ public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int this(poolConfig, connectionTimeout, soTimeout, null, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, + final int soTimeout, final String password, final String clientName, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, HostAndPortMapper hostAndPortMap) { + this(poolConfig, connectionTimeout, soTimeout, null, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); + } + + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final String user, final String password, final String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, @@ -81,23 +85,44 @@ public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int this(poolConfig, connectionTimeout, soTimeout, 0, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); } + public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, + final int soTimeout, final String user, final String password, final String clientName, + boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, HostAndPortMapper hostAndPortMap) { + this(poolConfig, connectionTimeout, soTimeout, 0, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap); + } + + /** + * @deprecated This constructor will be removed in future. + */ + @Deprecated public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout, final String user, final String password, final String clientName, boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier, JedisClusterHostAndPortMap hostAndPortMap) { + this(poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, + ssl, sslSocketFactory, sslParameters, hostnameVerifier, (HostAndPortMapper) hostAndPortMap); + } + + public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, + final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout, + final String user, final String password, final String clientName, boolean ssl, + SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, + HostnameVerifier hostnameVerifier, HostAndPortMapper hostAndPortMap) { + this(poolConfig, + DefaultJedisClientConfig.builder().withConnectionTimeout(connectionTimeout) + .withSoTimeout(soTimeout).withInfiniteSoTimeout(infiniteSoTimeout) + .withUser(user).withPassword(password).withClientName(clientName) + .withSsl(ssl).withSslSocketFactory(sslSocketFactory) + .withSslParameters(sslParameters) .withHostnameVerifier(hostnameVerifier) + .withHostAndPortMapper(hostAndPortMap).build() + ); + } + + public JedisClusterInfoCache(final GenericObjectPoolConfig poolConfig, final JedisClientConfig clientConfig) { this.poolConfig = poolConfig; - this.connectionTimeout = connectionTimeout; - this.soTimeout = soTimeout; - this.infiniteSoTimeout = infiniteSoTimeout; - this.user = user; - this.password = password; - this.clientName = clientName; - this.ssl = ssl; - this.sslSocketFactory = sslSocketFactory; - this.sslParameters = sslParameters; - this.hostnameVerifier = hostnameVerifier; - this.hostAndPortMap = hostAndPortMap; + this.clientConfig = clientConfig; } public void discoverClusterNodesAndSlots(Jedis jedis) { @@ -206,25 +231,17 @@ private void discoverClusterSlots(Jedis jedis) { private HostAndPort generateHostAndPort(List hostInfos) { String host = SafeEncoder.encode((byte[]) hostInfos.get(0)); int port = ((Long) hostInfos.get(1)).intValue(); - if (ssl && hostAndPortMap != null) { - HostAndPort hostAndPort = hostAndPortMap.getSSLHostAndPort(host, port); - if (hostAndPort != null) { - return hostAndPort; - } - } return new HostAndPort(host, port); } - public JedisPool setupNodeIfNotExist(HostAndPort node) { + public JedisPool setupNodeIfNotExist(final HostAndPort node) { w.lock(); try { String nodeKey = getNodeKey(node); JedisPool existingPool = nodes.get(nodeKey); if (existingPool != null) return existingPool; - JedisPool nodePool = new JedisPool(poolConfig, node.getHost(), node.getPort(), - connectionTimeout, soTimeout, infiniteSoTimeout, user, password, 0, clientName, - ssl, sslSocketFactory, sslParameters, hostnameVerifier); + JedisPool nodePool = new JedisPool(poolConfig, node, clientConfig); nodes.put(nodeKey, nodePool); return nodePool; } finally { @@ -318,10 +335,18 @@ public static String getNodeKey(HostAndPort hnp) { return hnp.getHost() + ":" + hnp.getPort(); } + /** + * @deprecated This method will be removed in future. + */ + @Deprecated public static String getNodeKey(Client client) { return client.getHost() + ":" + client.getPort(); } + /** + * @deprecated This method will be removed in future. + */ + @Deprecated public static String getNodeKey(Jedis jedis) { return getNodeKey(jedis.getClient()); } diff --git a/src/main/java/redis/clients/jedis/JedisFactory.java b/src/main/java/redis/clients/jedis/JedisFactory.java index 5677c490ef..3962181580 100644 --- a/src/main/java/redis/clients/jedis/JedisFactory.java +++ b/src/main/java/redis/clients/jedis/JedisFactory.java @@ -25,17 +25,8 @@ class JedisFactory implements PooledObjectFactory { private static final Logger logger = LoggerFactory.getLogger(JedisFactory.class); private final AtomicReference hostAndPort = new AtomicReference<>(); - private final int connectionTimeout; - private final int soTimeout; - private final int infiniteSoTimeout; - private final String user; - private final String password; - private final int database; - private final String clientName; - private final boolean ssl; - private final SSLSocketFactory sslSocketFactory; - private final SSLParameters sslParameters; - private final HostnameVerifier hostnameVerifier; + + private final JedisClientConfig config; JedisFactory(final String host, final int port, final int connectionTimeout, final int soTimeout, final String password, final int database, final String clientName) { @@ -66,22 +57,21 @@ class JedisFactory implements PooledObjectFactory { this(host, port, connectionTimeout, soTimeout, 0, user, password, database, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier); } + JedisFactory(final HostAndPort hostAndPort, final JedisClientConfig clientConfig) { + this.hostAndPort.set(hostAndPort); + this.config = DefaultJedisClientConfig.copyConfig(clientConfig); + } + JedisFactory(final String host, final int port, final int connectionTimeout, final int soTimeout, final int infiniteSoTimeout, final String user, final String password, final int database, final String clientName, final boolean ssl, final SSLSocketFactory sslSocketFactory, final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { this.hostAndPort.set(new HostAndPort(host, port)); - this.connectionTimeout = connectionTimeout; - this.soTimeout = soTimeout; - this.infiniteSoTimeout = infiniteSoTimeout; - this.user = user; - this.password = password; - this.database = database; - this.clientName = clientName; - this.ssl = ssl; - this.sslSocketFactory = sslSocketFactory; - this.sslParameters = sslParameters; - this.hostnameVerifier = hostnameVerifier; + this.config = DefaultJedisClientConfig.builder().withConnectionTimeout(connectionTimeout) + .withSoTimeout(soTimeout).withInfiniteSoTimeout(infiniteSoTimeout).withUser(user) + .withPassword(password).withDatabse(database).withClientName(clientName) + .withSsl(ssl).withSslSocketFactory(sslSocketFactory) + .withSslParameters(sslParameters).withHostnameVerifier(hostnameVerifier).build(); } JedisFactory(final URI uri, final int connectionTimeout, final int soTimeout, @@ -100,21 +90,15 @@ class JedisFactory implements PooledObjectFactory { final SSLParameters sslParameters, final HostnameVerifier hostnameVerifier) { if (!JedisURIHelper.isValid(uri)) { throw new InvalidURIException(String.format( - "Cannot open Redis connection due invalid URI. %s", uri.toString())); + "Cannot open Redis connection due invalid URI. %s", uri.toString())); } - this.hostAndPort.set(new HostAndPort(uri.getHost(), uri.getPort())); - this.connectionTimeout = connectionTimeout; - this.soTimeout = soTimeout; - this.infiniteSoTimeout = infiniteSoTimeout; - this.user = JedisURIHelper.getUser(uri); - this.password = JedisURIHelper.getPassword(uri); - this.database = JedisURIHelper.getDBIndex(uri); - this.clientName = clientName; - this.ssl = JedisURIHelper.isRedisSSLScheme(uri); - this.sslSocketFactory = sslSocketFactory; - this.sslParameters = sslParameters; - this.hostnameVerifier = hostnameVerifier; + this.config = DefaultJedisClientConfig.builder().withConnectionTimeout(connectionTimeout) + .withSoTimeout(soTimeout).withInfiniteSoTimeout(infiniteSoTimeout) + .withUser(JedisURIHelper.getUser(uri)).withPassword(JedisURIHelper.getPassword(uri)) + .withDatabse(JedisURIHelper.getDBIndex(uri)).withClientName(clientName) + .withSsl(JedisURIHelper.isRedisSSLScheme(uri)).withSslSocketFactory(sslSocketFactory) + .withSslParameters(sslParameters).withHostnameVerifier(hostnameVerifier).build(); } public void setHostAndPort(final HostAndPort hostAndPort) { @@ -124,8 +108,8 @@ public void setHostAndPort(final HostAndPort hostAndPort) { @Override public void activateObject(PooledObject pooledJedis) throws Exception { final BinaryJedis jedis = pooledJedis.getObject(); - if (jedis.getDB() != database) { - jedis.select(database); + if (jedis.getDB() != config.getDatabase()) { + jedis.select(config.getDatabase()); } } @@ -142,38 +126,36 @@ public void destroyObject(PooledObject pooledJedis) throws Exception { logger.warn("Error while QUIT", e); } try { - jedis.disconnect(); + jedis.close(); } catch (Exception e) { - logger.warn("Error while disconnect", e); + logger.warn("Error while close", e); } } } @Override public PooledObject makeObject() throws Exception { - final HostAndPort hp = this.hostAndPort.get(); - final Jedis jedis = new Jedis(hp.getHost(), hp.getPort(), connectionTimeout, soTimeout, - infiniteSoTimeout, ssl, sslSocketFactory, sslParameters, hostnameVerifier); - + final HostAndPort hostPort = this.hostAndPort.get(); + Jedis jedis = null; try { + jedis = new Jedis(hostPort, config); jedis.connect(); - if (user != null) { - jedis.auth(user, password); - } else if (password != null) { - jedis.auth(password); - } - if (database != 0) { - jedis.select(database); - } - if (clientName != null) { - jedis.clientSetname(clientName); - } + return new DefaultPooledObject<>(jedis); } catch (JedisException je) { - jedis.close(); + if (jedis != null) { + try { + jedis.quit(); + } catch (Exception e) { + logger.warn("Error while QUIT", e); + } + try { + jedis.close(); + } catch (Exception e) { + logger.warn("Error while close", e); + } + } throw je; } - - return new DefaultPooledObject<>(jedis); } @Override diff --git a/src/main/java/redis/clients/jedis/JedisPool.java b/src/main/java/redis/clients/jedis/JedisPool.java index 608fc34284..96383b845a 100644 --- a/src/main/java/redis/clients/jedis/JedisPool.java +++ b/src/main/java/redis/clients/jedis/JedisPool.java @@ -233,6 +233,11 @@ public JedisPool(final GenericObjectPoolConfig poolConfig, final String host, in user, password, database, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier)); } + public JedisPool(final GenericObjectPoolConfig poolConfig, final HostAndPort hostAndPort, + final JedisClientConfig clientConfig) { + super(poolConfig, new JedisFactory(hostAndPort, clientConfig)); + } + public JedisPool(final GenericObjectPoolConfig poolConfig) { this(poolConfig, Protocol.DEFAULT_HOST, Protocol.DEFAULT_PORT); } diff --git a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java index 457fa80d0c..64f32efe66 100644 --- a/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java +++ b/src/main/java/redis/clients/jedis/JedisSlotBasedConnectionHandler.java @@ -64,6 +64,11 @@ public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPool super(nodes, poolConfig, connectionTimeout, soTimeout, infiniteSoTimeout, user, password, clientName, ssl, sslSocketFactory, sslParameters, hostnameVerifier, portMap); } + public JedisSlotBasedConnectionHandler(Set nodes, GenericObjectPoolConfig poolConfig, + JedisClientConfig clientConfig) { + super(nodes, poolConfig, clientConfig); + } + @Override public Jedis getConnection() { // In antirez's redis-rb-cluster implementation, diff --git a/src/main/java/redis/clients/jedis/JedisSocketFactory.java b/src/main/java/redis/clients/jedis/JedisSocketFactory.java index 60677847ea..85912de136 100644 --- a/src/main/java/redis/clients/jedis/JedisSocketFactory.java +++ b/src/main/java/redis/clients/jedis/JedisSocketFactory.java @@ -2,6 +2,7 @@ import java.io.IOException; import java.net.Socket; +import redis.clients.jedis.exceptions.JedisConnectionException; /** * JedisSocketFactory: responsible for creating socket connections @@ -15,23 +16,29 @@ */ public interface JedisSocketFactory { - Socket createSocket() throws IOException; + /** + * WARNING: Throwing IOException will not be supported in future. + * @return Socket + * @throws IOException this will be removed in future + * @throws JedisConnectionException + */ + Socket createSocket() throws IOException, JedisConnectionException; - String getDescription(); + @Deprecated String getDescription(); - String getHost(); + @Deprecated String getHost(); - void setHost(String host); + @Deprecated void setHost(String host); - int getPort(); + @Deprecated int getPort(); - void setPort(int port); + @Deprecated void setPort(int port); - int getConnectionTimeout(); + @Deprecated int getConnectionTimeout(); - void setConnectionTimeout(int connectionTimeout); + @Deprecated void setConnectionTimeout(int connectionTimeout); - int getSoTimeout(); + @Deprecated int getSoTimeout(); - void setSoTimeout(int soTimeout); + @Deprecated void setSoTimeout(int soTimeout); } diff --git a/src/test/java/redis/clients/jedis/tests/ConnectionTest.java b/src/test/java/redis/clients/jedis/tests/ConnectionTest.java index e6ebe058e1..a90ca0dce1 100644 --- a/src/test/java/redis/clients/jedis/tests/ConnectionTest.java +++ b/src/test/java/redis/clients/jedis/tests/ConnectionTest.java @@ -4,10 +4,10 @@ import static org.junit.Assert.fail; import org.junit.After; -import org.junit.Before; import org.junit.Test; import redis.clients.jedis.Connection; +import redis.clients.jedis.Protocol; import redis.clients.jedis.Protocol.Command; import redis.clients.jedis.commands.ProtocolCommand; import redis.clients.jedis.exceptions.JedisConnectionException; @@ -15,40 +15,49 @@ public class ConnectionTest { private Connection client; - @Before - public void setUp() throws Exception { - client = new Connection(); - } - @After public void tearDown() throws Exception { - client.close(); + if (client != null) { + client.close(); + } } @Test(expected = JedisConnectionException.class) - public void checkUnkownHost() { + public void checkUnkownHostBackwardCompatible() { + client = new Connection(); client.setHost("someunknownhost"); client.connect(); } @Test(expected = JedisConnectionException.class) - public void checkWrongPort() { + public void checkUnkownHost() { + client = new Connection("someunknownhost", Protocol.DEFAULT_PORT); + client.connect(); + } + + @Test(expected = JedisConnectionException.class) + public void checkWrongPortBackwardCompatible() { + client = new Connection(); client.setHost("localhost"); client.setPort(55665); client.connect(); } + @Test(expected = JedisConnectionException.class) + public void checkWrongPort() { + client = new Connection(Protocol.DEFAULT_HOST, 55665); + client.connect(); + } + @Test public void connectIfNotConnectedWhenSettingTimeoutInfinite() { - client.setHost("localhost"); - client.setPort(6379); + client = new Connection("localhost", 6379); client.setTimeoutInfinite(); } @Test public void checkCloseable() { - client.setHost("localhost"); - client.setPort(6379); + client = new Connection("localhost", 6379); client.connect(); client.close(); } diff --git a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java index de58c76c42..b8c15e3d5e 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisClusterTest.java @@ -10,6 +10,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -38,6 +39,7 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisCluster; import redis.clients.jedis.ClusterReset; +import redis.clients.jedis.DefaultJedisClientConfig; import redis.clients.jedis.JedisClusterInfoCache; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; @@ -58,7 +60,9 @@ public class JedisClusterTest { private static final int DEFAULT_TIMEOUT = 2000; private static final int DEFAULT_REDIRECTIONS = 5; - private static final JedisPoolConfig DEFAULT_CONFIG = new JedisPoolConfig(); + private static final JedisPoolConfig DEFAULT_POOL_CONFIG = new JedisPoolConfig(); + private static final DefaultJedisClientConfig DEFAULT_CLIENT_CONFIG + = DefaultJedisClientConfig.builder().withPassword("cluster").build(); private HostAndPort nodeInfo1 = HostAndPortUtil.getClusterServers().get(0); private HostAndPort nodeInfo2 = HostAndPortUtil.getClusterServers().get(1); @@ -163,21 +167,35 @@ public void testDiscoverNodesAutomatically() { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); assertEquals(3, jc.getClusterNodes().size()); JedisCluster jc2 = new JedisCluster(new HostAndPort("127.0.0.1", 7379), DEFAULT_TIMEOUT, - DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); assertEquals(3, jc2.getClusterNodes().size()); } + @Test + public void testDiscoverNodesAutomaticallyWithSocketConfig() { + HostAndPort hp = new HostAndPort("127.0.0.1", 7379); + + try (JedisCluster jc = new JedisCluster(hp, DEFAULT_CLIENT_CONFIG, DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) { + assertEquals(3, jc.getClusterNodes().size()); + } + + try (JedisCluster jc = new JedisCluster(Collections.singleton(hp), DEFAULT_CLIENT_CONFIG, + DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) { + assertEquals(3, jc.getClusterNodes().size()); + } + } + @Test public void testSetClientName() { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); String clientName = "myAppName"; JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", clientName, DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", clientName, DEFAULT_POOL_CONFIG); Map clusterNodes = jc.getClusterNodes(); Collection values = clusterNodes.values(); for (JedisPool jedisPool : values) { @@ -190,19 +208,34 @@ public void testSetClientName() { } } + @Test + public void testSetClientNameWithConfig() { + HostAndPort hp = new HostAndPort("127.0.0.1", 7379); + String clientName = "config-pattern-app"; + try (JedisCluster jc = new JedisCluster(Collections.singleton(hp), + DefaultJedisClientConfig.builder().withPassword("cluster").withClientName(clientName).build(), + DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) { + jc.getClusterNodes().values().forEach(jedisPool -> { + try (Jedis jedis = jedisPool.getResource()) { + assertEquals(clientName, jedis.clientGetname()); + } + }); + } + } + @Test public void testCalculateConnectionPerSlot() { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); jc.set("foo", "bar"); jc.set("test", "test"); assertEquals("bar", node3.get("foo")); assertEquals("test", node2.get("test")); JedisCluster jc2 = new JedisCluster(new HostAndPort("127.0.0.1", 7379), DEFAULT_TIMEOUT, - DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); jc2.set("foo", "bar"); jc2.set("test", "test"); assertEquals("bar", node3.get("foo")); @@ -241,7 +274,7 @@ public void testMigrate() { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(nodeInfo1); JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); String node3Id = JedisClusterTestUtil.getNodeId(node3.clusterNodes()); String node2Id = JedisClusterTestUtil.getNodeId(node2.clusterNodes()); node3.clusterSetSlotMigrating(15363, node2Id); @@ -292,7 +325,7 @@ public void testMigrateToNewNode() throws InterruptedException { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(nodeInfo1); JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); node3.clusterMeet(LOCAL_IP, nodeInfo4.getPort()); String node3Id = JedisClusterTestUtil.getNodeId(node3.clusterNodes()); @@ -345,7 +378,7 @@ public void testRecalculateSlotsWhenMoved() throws InterruptedException { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); int slot51 = JedisClusterCRC16.getSlot("51"); node2.clusterDelSlots(slot51); node3.clusterDelSlots(slot51); @@ -357,11 +390,11 @@ public void testRecalculateSlotsWhenMoved() throws InterruptedException { } @Test - public void testAskResponse() throws InterruptedException { + public void testAskResponse() { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); int slot51 = JedisClusterCRC16.getSlot("51"); node3.clusterSetSlotImporting(slot51, JedisClusterTestUtil.getNodeId(node2.clusterNodes())); node2.clusterSetSlotMigrating(slot51, JedisClusterTestUtil.getNodeId(node3.clusterNodes())); @@ -369,18 +402,41 @@ public void testAskResponse() throws InterruptedException { assertEquals("foo", jc.get("51")); } + @Test + public void testAskResponseWithConfig() { + HostAndPort hp = new HostAndPort("127.0.0.1", 7379); + try (JedisCluster jc = new JedisCluster(Collections.singleton(hp), DEFAULT_CLIENT_CONFIG, DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) { + int slot51 = JedisClusterCRC16.getSlot("51"); + node3.clusterSetSlotImporting(slot51, JedisClusterTestUtil.getNodeId(node2.clusterNodes())); + node2.clusterSetSlotMigrating(slot51, JedisClusterTestUtil.getNodeId(node3.clusterNodes())); + jc.set("51", "foo"); + assertEquals("foo", jc.get("51")); + } + } + @Test(expected = JedisClusterMaxAttemptsException.class) public void testRedisClusterMaxRedirections() { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); int slot51 = JedisClusterCRC16.getSlot("51"); // This will cause an infinite redirection loop node2.clusterSetSlotMigrating(slot51, JedisClusterTestUtil.getNodeId(node3.clusterNodes())); jc.set("51", "foo"); } + @Test(expected = JedisClusterMaxAttemptsException.class) + public void testRedisClusterMaxRedirectionsWithConfig() { + HostAndPort hp = new HostAndPort("127.0.0.1", 7379); + try (JedisCluster jc = new JedisCluster(Collections.singleton(hp), DEFAULT_CLIENT_CONFIG, DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) { + int slot51 = JedisClusterCRC16.getSlot("51"); + // This will cause an infinite redirection loop + node2.clusterSetSlotMigrating(slot51, JedisClusterTestUtil.getNodeId(node3.clusterNodes())); + jc.set("51", "foo"); + } + } + @Test public void testClusterForgetNode() throws InterruptedException { // at first, join node4 to cluster @@ -456,7 +512,7 @@ public void testClusterCountKeysInSlot() { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort())); JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); int count = 5; for (int index = 0; index < count; index++) { @@ -473,7 +529,7 @@ public void testStableSlotWhenMigratingNodeOrImportingNodeIsNotSpecified() Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort())); JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); int slot51 = JedisClusterCRC16.getSlot("51"); jc.set("51", "foo"); @@ -507,27 +563,22 @@ public void testCloseable() throws IOException { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort())); - JedisCluster jc = null; - try { - jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); - jc.set("51", "foo"); - } finally { - if (jc != null) { - jc.close(); - } - } + JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); + jc.set("51", "foo"); + jc.close(); - Iterator poolIterator = jc.getClusterNodes().values().iterator(); - while (poolIterator.hasNext()) { - JedisPool pool = poolIterator.next(); - try { - pool.getResource(); - fail("JedisCluster's internal pools should be already destroyed"); - } catch (JedisConnectionException e) { - // ok to go... - } - } + assertEquals(0, jc.getClusterNodes().size()); + } + + @Test + public void testCloseableWithConfig() { + HostAndPort hp = nodeInfo1; + JedisCluster jc = new JedisCluster(hp, DEFAULT_CLIENT_CONFIG, DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG); + jc.set("51", "foo"); + jc.close(); + + assertEquals(0, jc.getClusterNodes().size()); } @Test @@ -536,7 +587,7 @@ public void testJedisClusterTimeout() { jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort())); JedisCluster jc = new JedisCluster(jedisClusterNode, 4000, 4000, DEFAULT_REDIRECTIONS, - "cluster", DEFAULT_CONFIG); + "cluster", DEFAULT_POOL_CONFIG); for (JedisPool pool : jc.getClusterNodes().values()) { Jedis jedis = pool.getResource(); @@ -546,13 +597,29 @@ public void testJedisClusterTimeout() { } } + @Test + public void testJedisClusterTimeoutWithConfig() { + HostAndPort hp = nodeInfo1; + try (JedisCluster jc = new JedisCluster(hp, DefaultJedisClientConfig.builder() + .withConnectionTimeout(4000).withSoTimeout(4000).withPassword("cluster").build(), + DEFAULT_REDIRECTIONS, DEFAULT_POOL_CONFIG)) { + + jc.getClusterNodes().values().forEach(pool -> { + try (Jedis jedis = pool.getResource()) { + assertEquals(4000, jedis.getClient().getConnectionTimeout()); + assertEquals(4000, jedis.getClient().getSoTimeout()); + } + }); + } + } + @Test public void testJedisClusterRunsWithMultithreaded() throws InterruptedException, ExecutionException, IOException { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("127.0.0.1", 7379)); final JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); jc.set("foo", "bar"); ThreadPoolExecutor executor = new ThreadPoolExecutor(10, 100, 0, TimeUnit.SECONDS, @@ -618,7 +685,7 @@ public void testLocalhostNodeNotAddedWhen127Present() { JedisPoolConfig config = new JedisPoolConfig(); config.setMaxTotal(1); JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); Map clusterNodes = jc.getClusterNodes(); assertEquals(3, clusterNodes.size()); assertFalse(clusterNodes.containsKey(JedisClusterInfoCache.getNodeKey(localhost))); @@ -644,7 +711,7 @@ public void nullKeys() { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort(nodeInfo1.getHost(), nodeInfo1.getPort())); JedisCluster cluster = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); String foo = "foo"; byte[] bfoo = new byte[]{0x0b, 0x0f, 0x00, 0x00}; @@ -699,7 +766,7 @@ public void georadiusStore() { jedisClusterNode.add(nodeInfo2); jedisClusterNode.add(nodeInfo3); JedisCluster cluster = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); // prepare datas Map coordinateMap = new HashMap(); @@ -723,7 +790,7 @@ public void georadiusStoreBinary() { jedisClusterNode.add(nodeInfo2); jedisClusterNode.add(nodeInfo3); JedisCluster cluster = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", DEFAULT_CONFIG); + DEFAULT_REDIRECTIONS, "cluster", DEFAULT_POOL_CONFIG); // prepare datas Map bcoordinateMap = new HashMap(); diff --git a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java index 31838aecf4..187c21c088 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisPoolTest.java @@ -20,6 +20,7 @@ import redis.clients.jedis.JedisPoolConfig; import redis.clients.jedis.Transaction; import redis.clients.jedis.exceptions.InvalidURIException; +import redis.clients.jedis.exceptions.JedisConnectionException; import redis.clients.jedis.exceptions.JedisExhaustedPoolException; public class JedisPoolTest { @@ -355,6 +356,7 @@ public void closeBrokenResourceTwice() { j.getClient().getOne(); fail(); } catch (Exception e) { + assertTrue(e instanceof JedisConnectionException); } assertTrue(j.isBroken()); j.close(); diff --git a/src/test/java/redis/clients/jedis/tests/JedisPoolWithCompleteCredentialsTest.java b/src/test/java/redis/clients/jedis/tests/JedisPoolWithCompleteCredentialsTest.java index 9da5ae6a6f..54a716adce 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisPoolWithCompleteCredentialsTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisPoolWithCompleteCredentialsTest.java @@ -11,6 +11,7 @@ import org.junit.BeforeClass; import org.junit.Test; +import redis.clients.jedis.DefaultJedisClientConfig; import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; @@ -76,6 +77,26 @@ public void checkResourceIsClosableAndReusable() { } } + @Test + public void checkResourceWithConfigIsClosableAndReusable() { + GenericObjectPoolConfig config = new GenericObjectPoolConfig(); + config.setMaxTotal(1); + config.setBlockWhenExhausted(false); + try (JedisPool pool = new JedisPool(config, hnp, DefaultJedisClientConfig.builder() + .withUser("acljedis").withPassword("fizzbuzz").withClientName("closable-resuable-pool").build())) { + + Jedis jedis = pool.getResource(); + jedis.set("hello", "jedis"); + jedis.close(); + + Jedis jedis2 = pool.getResource(); + assertEquals(jedis, jedis2); + assertEquals("jedis", jedis2.get("hello")); + assertEquals("closable-resuable-pool", jedis2.clientGetname()); + jedis2.close(); + } + } + @Test public void checkPoolRepairedWhenJedisIsBroken() { JedisPool pool = new JedisPool(new JedisPoolConfig(), hnp.getHost(), hnp.getPort(), diff --git a/src/test/java/redis/clients/jedis/tests/JedisTest.java b/src/test/java/redis/clients/jedis/tests/JedisTest.java index b8e0596fef..e92d33f55c 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisTest.java @@ -6,14 +6,19 @@ import static org.junit.Assert.fail; import java.io.IOException; +import java.net.ServerSocket; +import java.net.SocketTimeoutException; import java.net.URI; import java.net.URISyntaxException; +import java.time.Duration; +import java.time.Instant; import java.util.HashMap; import java.util.Map; import org.junit.Test; import redis.clients.jedis.BinaryJedis; +import redis.clients.jedis.DefaultJedisClientConfig; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.Protocol; @@ -40,11 +45,10 @@ public void checkBinaryData() { for (int b = 0; b < bigdata.length; b++) { bigdata[b] = (byte) ((byte) b % 255); } - Map hash = new HashMap(); + Map hash = new HashMap<>(); hash.put("data", SafeEncoder.encode(bigdata)); - String status = jedis.hmset("foo", hash); - assertEquals("OK", status); + assertEquals("OK", jedis.hmset("foo", hash)); assertEquals(hash, jedis.hgetAll("foo")); } @@ -52,9 +56,17 @@ public void checkBinaryData() { public void connectWithShardInfo() { JedisShardInfo shardInfo = new JedisShardInfo("localhost", Protocol.DEFAULT_PORT); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - jedis.get("foo"); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + jedis.get("foo"); + } + } + + @Test + public void connectWithConfig() { + try (Jedis jedis = new Jedis(hnp, DefaultJedisClientConfig.builder().build())) { + jedis.auth("foobared"); + assertEquals("PONG", jedis.ping()); + } } @Test @@ -79,26 +91,6 @@ public void timeoutConnection() throws Exception { jedis.close(); } - @Test - public void timeoutConnectionWithURI() throws Exception { - Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2"), 15000); - String timeout = jedis.configGet("timeout").get(1); - jedis.configSet("timeout", "1"); - Thread.sleep(2000); - try { - jedis.hmget("foobar", "foo"); - fail("Operation should throw JedisConnectionException"); - } catch(JedisConnectionException jce) { - // expected - } - jedis.close(); - - // reset config - jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2")); - jedis.configSet("timeout", timeout); - jedis.close(); - } - @Test public void infiniteTimeout() throws Exception { try (Jedis timeoutJedis = new Jedis("localhost", 6379, 350, 350, 350)) { @@ -134,7 +126,7 @@ public void shouldReconnectToSameDB() throws IOException { } @Test - public void startWithUrlString() { + public void startWithUrl() { try (Jedis j = new Jedis("localhost", 6380)) { j.auth("foobared"); j.select(2); @@ -148,16 +140,17 @@ public void startWithUrlString() { } @Test - public void startWithUrl() throws URISyntaxException { - Jedis j = new Jedis("localhost", 6380); - j.auth("foobared"); - j.select(2); - j.set("foo", "bar"); + public void startWithUri() throws URISyntaxException { + try (Jedis j = new Jedis("localhost", 6380)) { + j.auth("foobared"); + j.select(2); + j.set("foo", "bar"); + } - Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2")); - assertEquals("PONG", jedis.ping()); - assertEquals("bar", jedis.get("foo")); - jedis.close(); + try (Jedis jedis = new Jedis(new URI("redis://:foobared@localhost:6380/2"))) { + assertEquals("PONG", jedis.ping()); + assertEquals("bar", jedis.get("foo")); + } } @Test @@ -191,12 +184,42 @@ public void allowUrlWithNoDBAndNoPassword() { } @Test - public void checkCloseable() { - try (BinaryJedis bj = new BinaryJedis("localhost")) { - bj.connect(); + public void uriWithDBindexShouldUseTimeout() throws URISyntaxException, IOException { + int fakePort = 6378; + int timeoutMillis = 3250; + int deltaMillis = 500; + URI uri = new URI(String.format("redis://localhost:%d/1", fakePort)); + Instant start = Instant.now(); + + try (ServerSocket server = new ServerSocket(fakePort); + Jedis jedis = new Jedis(uri, timeoutMillis)) { + fail("Jedis should fail to connect to a fake port"); + } catch (JedisConnectionException ex) { + assertEquals(SocketTimeoutException.class, ex.getCause().getClass()); + assertEquals(timeoutMillis, Duration.between(start, Instant.now()).toMillis(), deltaMillis); } } + @Test + public void checkCloseable() { + BinaryJedis bj = new BinaryJedis(); + bj.close(); + } + + @Test + public void checkCloseableAfterConnect() { + BinaryJedis bj = new BinaryJedis(); + bj.connect(); + bj.close(); + } + + @Test + public void checkCloseableAfterCommand() { + BinaryJedis bj = new BinaryJedis(); + bj.auth("foobared"); + bj.close(); + } + @Test public void checkDisconnectOnQuit() { jedis.quit(); diff --git a/src/test/java/redis/clients/jedis/tests/JedisWithCompleteCredentialsTest.java b/src/test/java/redis/clients/jedis/tests/JedisWithCompleteCredentialsTest.java index f4e184a362..855ae576e5 100644 --- a/src/test/java/redis/clients/jedis/tests/JedisWithCompleteCredentialsTest.java +++ b/src/test/java/redis/clients/jedis/tests/JedisWithCompleteCredentialsTest.java @@ -7,6 +7,7 @@ import org.junit.BeforeClass; import org.junit.Test; +import redis.clients.jedis.DefaultJedisClientConfig; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.Protocol; @@ -48,7 +49,15 @@ public void connectWithShardInfo() { } @Test - public void startWithUrlString() { + public void connectWithConfig() { + try (Jedis jedis = new Jedis(hnp, DefaultJedisClientConfig.builder().build())) { + jedis.auth("acljedis", "fizzbuzz"); + assertEquals("PONG", jedis.ping()); + } + } + + @Test + public void startWithUrl() { try(Jedis j = new Jedis("localhost", 6379)){ assertEquals("OK", j.auth("acljedis", "fizzbuzz")); assertEquals("OK", j.select(2)); @@ -61,7 +70,7 @@ public void startWithUrlString() { } @Test - public void startWithUrl() throws URISyntaxException { + public void startWithUri() throws URISyntaxException { try(Jedis j = new Jedis("localhost", 6379)){ assertEquals("OK", j.auth("acljedis", "fizzbuzz")); assertEquals("OK", j.select(2)); diff --git a/src/test/java/redis/clients/jedis/tests/SSLJedisClusterTest.java b/src/test/java/redis/clients/jedis/tests/SSLJedisClusterTest.java index ab2f41134a..02e51b3c19 100644 --- a/src/test/java/redis/clients/jedis/tests/SSLJedisClusterTest.java +++ b/src/test/java/redis/clients/jedis/tests/SSLJedisClusterTest.java @@ -33,7 +33,7 @@ public class SSLJedisClusterTest extends JedisClusterTest { private static final int DEFAULT_TIMEOUT = 2000; private static final int DEFAULT_REDIRECTIONS = 5; - private static final JedisPoolConfig DEFAULT_CONFIG = new JedisPoolConfig(); + private static final JedisPoolConfig DEFAULT_POOL_CONFIG = new JedisPoolConfig(); private JedisClusterHostAndPortMap hostAndPortMap = new JedisClusterHostAndPortMap() { public HostAndPort getSSLHostAndPort(String host, int port) { @@ -59,23 +59,23 @@ public void testSSLDiscoverNodesAutomatically() { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("localhost", 8379)); try(JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, - "cluster", null, DEFAULT_CONFIG, true, null, null, null, hostAndPortMap)){ + "cluster", null, DEFAULT_POOL_CONFIG, true, null, null, null, hostAndPortMap)){ Map clusterNodes = jc.getClusterNodes(); assertEquals(3, clusterNodes.size()); - assertTrue(clusterNodes.containsKey("localhost:8379")); - assertTrue(clusterNodes.containsKey("localhost:8380")); - assertTrue(clusterNodes.containsKey("localhost:8381")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7379")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7380")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7381")); jc.get("foo"); } try(JedisCluster jc2 = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_CONFIG, true, null, null, null, hostAndPortMap)){ + DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_POOL_CONFIG, true, null, null, null, hostAndPortMap)){ Map clusterNodes = jc2.getClusterNodes(); assertEquals(3, clusterNodes.size()); - assertTrue(clusterNodes.containsKey("localhost:8379")); - assertTrue(clusterNodes.containsKey("localhost:8380")); - assertTrue(clusterNodes.containsKey("localhost:8381")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7379")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7380")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7381")); jc2.get("foo"); } } @@ -85,7 +85,7 @@ public void testSSLWithoutPortMap() { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("localhost", 8379)); try(JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, - "cluster", null, DEFAULT_CONFIG, true, null, null, null, null)){ + "cluster", null, DEFAULT_POOL_CONFIG, true, null, null, null, null)){ Map clusterNodes = jc.getClusterNodes(); assertEquals(3, clusterNodes.size()); @@ -98,7 +98,7 @@ public void testSSLWithoutPortMap() { @Test public void connectByIpAddress() { try(JedisCluster jc = new JedisCluster(new HostAndPort("127.0.0.1", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_POOL_CONFIG, true, null, null, null, hostAndPortMap)){ jc.get("foo"); } @@ -110,7 +110,7 @@ public void connectToNodesFailsWithSSLParametersAndNoHostMapping() { sslParameters.setEndpointIdentificationAlgorithm("HTTPS"); try ( JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_POOL_CONFIG, true, null, sslParameters, null, portMap)){ jc.get("foo"); Assert.fail("The code did not throw the expected JedisClusterMaxAttemptsException."); @@ -126,7 +126,7 @@ public void connectToNodesSucceedsWithSSLParametersAndHostMapping() { sslParameters.setEndpointIdentificationAlgorithm("HTTPS"); try(JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_POOL_CONFIG, true, null, sslParameters, null, hostAndPortMap)){ jc.get("foo"); } @@ -138,7 +138,7 @@ public void connectByIpAddressFailsWithSSLParameters() { sslParameters.setEndpointIdentificationAlgorithm("HTTPS"); try (JedisCluster jc = new JedisCluster(new HostAndPort("127.0.0.1", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_POOL_CONFIG, true, null, sslParameters, null, hostAndPortMap)){ jc.get("key"); Assert.fail("The code did not throw the expected JedisConnectionException."); @@ -155,7 +155,7 @@ public void connectWithCustomHostNameVerifier() { HostnameVerifier localhostVerifier = new LocalhostVerifier(); try (JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_POOL_CONFIG, true, null, null, hostnameVerifier, portMap)){ jc.get("foo"); Assert.fail("The code did not throw the expected JedisClusterMaxAttemptsException."); @@ -165,7 +165,7 @@ public void connectWithCustomHostNameVerifier() { } try (JedisCluster jc2 = new JedisCluster(new HostAndPort("127.0.0.1", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_POOL_CONFIG, true, null, null, hostnameVerifier, portMap)){ jc2.get("foo"); Assert.fail("The code did not throw the expected JedisNoReachableClusterNodeException."); @@ -175,7 +175,7 @@ public void connectWithCustomHostNameVerifier() { } try(JedisCluster jc3 = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_POOL_CONFIG, true, null, null, localhostVerifier, portMap)){ jc3.get("foo"); } @@ -186,7 +186,7 @@ public void connectWithCustomSocketFactory() throws Exception { final SSLSocketFactory sslSocketFactory = SSLJedisTest.createTrustStoreSslSocketFactory(); try(JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_POOL_CONFIG, true, sslSocketFactory, null, null, portMap)){ assertEquals(3, jc.getClusterNodes().size()); } @@ -197,7 +197,7 @@ public void connectWithEmptyTrustStore() throws Exception { final SSLSocketFactory sslSocketFactory = SSLJedisTest.createTrustNoOneSslSocketFactory(); try ( JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_POOL_CONFIG, true, sslSocketFactory, null, null, null)){ jc.get("key"); Assert.fail("The code did not throw the expected JedisConnectionException."); @@ -222,7 +222,7 @@ public HostAndPort getSSLHostAndPort(String host, int port) { }; JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 7379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_CONFIG, false, + DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_POOL_CONFIG, false, null, null, null, hostAndPortMap); Map nodes = jc.getClusterNodes(); @@ -240,7 +240,7 @@ public HostAndPort getSSLHostAndPort(String host, int port) { }; try(JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 7379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_CONFIG, false, + DEFAULT_REDIRECTIONS, "cluster", null, DEFAULT_POOL_CONFIG, false, null, null, null, hostAndPortMap)) { Map clusterNodes = jc.getClusterNodes(); diff --git a/src/test/java/redis/clients/jedis/tests/SSLJedisClusterWithCompleteCredentialsTest.java b/src/test/java/redis/clients/jedis/tests/SSLJedisClusterWithCompleteCredentialsTest.java index 0941bd90ee..6eeddc7575 100644 --- a/src/test/java/redis/clients/jedis/tests/SSLJedisClusterWithCompleteCredentialsTest.java +++ b/src/test/java/redis/clients/jedis/tests/SSLJedisClusterWithCompleteCredentialsTest.java @@ -19,7 +19,7 @@ public class SSLJedisClusterWithCompleteCredentialsTest extends JedisClusterTest { private static final int DEFAULT_TIMEOUT = 2000; private static final int DEFAULT_REDIRECTIONS = 5; - private static final JedisPoolConfig DEFAULT_CONFIG = new JedisPoolConfig(); + private static final JedisPoolConfig DEFAULT_POOL_CONFIG = new JedisPoolConfig(); private JedisClusterHostAndPortMap hostAndPortMap = new JedisClusterHostAndPortMap() { public HostAndPort getSSLHostAndPort(String host, int port) { @@ -46,24 +46,26 @@ public static void prepare() { public void testSSLDiscoverNodesAutomatically() { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("localhost", 8379)); - JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, - "default","cluster", null, DEFAULT_CONFIG, true, null, null, null, hostAndPortMap); + JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, + DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_POOL_CONFIG, true, null, null, + null, hostAndPortMap); Map clusterNodes = jc.getClusterNodes(); assertEquals(3, clusterNodes.size()); - assertTrue(clusterNodes.containsKey("localhost:8379")); - assertTrue(clusterNodes.containsKey("localhost:8380")); - assertTrue(clusterNodes.containsKey("localhost:8381")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7379")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7380")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7381")); jc.get("foo"); jc.close(); - - JedisCluster jc2 = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_CONFIG, true, null, null, null, hostAndPortMap); + + JedisCluster jc2 = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, + DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_POOL_CONFIG, + true, null, null, null, hostAndPortMap); clusterNodes = jc2.getClusterNodes(); assertEquals(3, clusterNodes.size()); - assertTrue(clusterNodes.containsKey("localhost:8379")); - assertTrue(clusterNodes.containsKey("localhost:8380")); - assertTrue(clusterNodes.containsKey("localhost:8381")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7379")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7380")); + assertTrue(clusterNodes.containsKey("127.0.0.1:7381")); jc2.get("foo"); jc2.close(); } @@ -73,9 +75,9 @@ public void testSSLWithoutPortMap() { Set jedisClusterNode = new HashSet(); jedisClusterNode.add(new HostAndPort("localhost", 8379)); try(JedisCluster jc = new JedisCluster(jedisClusterNode, DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, DEFAULT_REDIRECTIONS, - "default", "cluster", null, DEFAULT_CONFIG, + "default", "cluster", null, DEFAULT_POOL_CONFIG, true, null, null, null, null)){ - + Map clusterNodes = jc.getClusterNodes(); assertEquals(3, clusterNodes.size()); assertTrue(clusterNodes.containsKey("127.0.0.1:7379")); @@ -87,7 +89,7 @@ public void testSSLWithoutPortMap() { @Test public void connectByIpAddress() { try(JedisCluster jc = new JedisCluster(new HostAndPort("127.0.0.1", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_POOL_CONFIG, true, null, null, null, hostAndPortMap)){ jc.get("foo"); } @@ -99,7 +101,7 @@ public void connectToNodesFailsWithSSLParametersAndNoHostMapping() { sslParameters.setEndpointIdentificationAlgorithm("HTTPS"); try (JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_POOL_CONFIG, true, null, sslParameters, null, portMap)){ jc.get("foo"); Assert.fail("The code did not throw the expected JedisClusterMaxAttemptsException."); @@ -115,7 +117,7 @@ public void connectToNodesSucceedsWithSSLParametersAndHostMapping() { sslParameters.setEndpointIdentificationAlgorithm("HTTPS"); try(JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_POOL_CONFIG, true, null, sslParameters, null, hostAndPortMap)){ jc.get("foo"); } @@ -127,7 +129,7 @@ public void connectByIpAddressFailsWithSSLParameters() { sslParameters.setEndpointIdentificationAlgorithm("HTTPS"); try (JedisCluster jc = new JedisCluster(new HostAndPort("127.0.0.1", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "user", "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "user", "cluster", null, DEFAULT_POOL_CONFIG, true, null, sslParameters, null, hostAndPortMap)){ jc.get("key"); Assert.fail("The code did not throw the expected JedisConnectionException."); @@ -143,7 +145,7 @@ public void connectWithCustomHostNameVerifier() { HostnameVerifier localhostVerifier = new LocalhostVerifier(); try (JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_POOL_CONFIG, true, null, null, hostnameVerifier, portMap)){ jc.get("foo"); Assert.fail("The code did not throw the expected JedisClusterMaxAttemptsException."); @@ -153,7 +155,7 @@ public void connectWithCustomHostNameVerifier() { } try ( JedisCluster jc2 = new JedisCluster(new HostAndPort("127.0.0.1", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_POOL_CONFIG, true, null, null, hostnameVerifier, portMap)){ jc2.get("key"); Assert.fail("The code did not throw the expected NullPointerException."); @@ -161,7 +163,7 @@ public void connectWithCustomHostNameVerifier() { } JedisCluster jc3 = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_POOL_CONFIG, true, null, null, localhostVerifier, portMap); jc3.get("foo"); jc3.close(); @@ -172,7 +174,7 @@ public void connectWithCustomSocketFactory() throws Exception { final SSLSocketFactory sslSocketFactory = SSLJedisTest.createTrustStoreSslSocketFactory(); try(JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_POOL_CONFIG, true, sslSocketFactory, null, null, portMap)){ assertEquals(3, jc.getClusterNodes().size()); } @@ -183,7 +185,7 @@ public void connectWithEmptyTrustStore() throws Exception { final SSLSocketFactory sslSocketFactory = SSLJedisTest.createTrustNoOneSslSocketFactory(); try (JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 8379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_CONFIG, true, + DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_POOL_CONFIG, true, sslSocketFactory, null, null, null)){ jc.get("key"); Assert.fail("The code did not throw the expected JedisConnectionException."); @@ -206,7 +208,7 @@ public HostAndPort getSSLHostAndPort(String host, int port) { }; try(JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 7379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_CONFIG, false, + DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_POOL_CONFIG, false, null, null, null, hostAndPortMap)){ Map nodes = jc.getClusterNodes(); @@ -224,7 +226,7 @@ public HostAndPort getSSLHostAndPort(String host, int port) { }; try(JedisCluster jc = new JedisCluster(new HostAndPort("localhost", 7379), DEFAULT_TIMEOUT, DEFAULT_TIMEOUT, - DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_CONFIG, false, + DEFAULT_REDIRECTIONS, "default", "cluster", null, DEFAULT_POOL_CONFIG, false, null, null, null, hostAndPortMap)){ Map clusterNodes = jc.getClusterNodes(); diff --git a/src/test/java/redis/clients/jedis/tests/SSLJedisTest.java b/src/test/java/redis/clients/jedis/tests/SSLJedisTest.java index b13edaeeba..e62b6e2039 100644 --- a/src/test/java/redis/clients/jedis/tests/SSLJedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/SSLJedisTest.java @@ -29,6 +29,8 @@ import org.junit.BeforeClass; import org.junit.Test; +import redis.clients.jedis.DefaultJedisClientConfig; +import redis.clients.jedis.HostAndPort; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisShardInfo; import redis.clients.jedis.exceptions.JedisConnectionException; @@ -51,6 +53,22 @@ private static void setJvmTrustStore(String trustStoreFilePath, String trustStor System.setProperty("javax.net.ssl.trustStoreType", trustStoreType); } + @Test + public void connectWithSsl() { + try (Jedis jedis = new Jedis("localhost", 6390, true)) { + jedis.auth("foobared"); + assertEquals("PONG", jedis.ping()); + } + } + + @Test + public void connectWithConfig() { + try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390), DefaultJedisClientConfig.builder().withSsl(true).build())) { + jedis.auth("foobared"); + assertEquals("PONG", jedis.ping()); + } + } + /** * Tests opening a default SSL/TLS connection to redis using "rediss://" scheme url. */ @@ -67,7 +85,7 @@ public void connectWithUrl() { * Tests opening a default SSL/TLS connection to redis. */ @Test - public void connectWithoutShardInfo() { + public void connectWithUri() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. try (Jedis jedis = new Jedis(URI.create("rediss://localhost:6390"))) { jedis.auth("foobared"); @@ -94,10 +112,9 @@ public void connectWithShardInfo() throws Exception { JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, null); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - assertEquals("PONG", jedis.ping()); - jedis.disconnect(); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + assertEquals("PONG", jedis.ping()); + } } /** @@ -123,8 +140,7 @@ public void connectWithShardInfoByIpAddress() throws Exception { JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, null); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - try { + try (Jedis jedis = new Jedis(shardInfo)) { assertEquals("PONG", jedis.ping()); fail("The code did not throw the expected JedisConnectionException."); } catch (JedisConnectionException e) { @@ -133,12 +149,6 @@ public void connectWithShardInfoByIpAddress() throws Exception { assertEquals("Unexpected second inner exception.", CertificateException.class, e.getCause().getCause().getClass()); } - - try { - jedis.close(); - } catch (Throwable e1) { - // Expected. - } } /** @@ -155,10 +165,9 @@ public void connectWithShardInfoAndCustomHostnameVerifier() { JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - assertEquals("PONG", jedis.ping()); - jedis.disconnect(); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + assertEquals("PONG", jedis.ping()); + } } /** @@ -174,10 +183,9 @@ public void connectWithShardInfoAndCustomSocketFactory() throws Exception { JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - assertEquals("PONG", jedis.ping()); - jedis.disconnect(); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + assertEquals("PONG", jedis.ping()); + } } /** @@ -196,20 +204,13 @@ public void connectWithShardInfoAndCustomHostnameVerifierByIpAddress() { JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - try { + try (Jedis jedis = new Jedis(shardInfo)) { assertEquals("PONG", jedis.ping()); fail("The code did not throw the expected JedisConnectionException."); } catch (JedisConnectionException e) { assertEquals("The JedisConnectionException does not contain the expected message.", "The connection to '127.0.0.1' failed ssl/tls hostname verification.", e.getMessage()); } - - try { - jedis.close(); - } catch (Throwable e1) { - // Expected. - } } /** @@ -228,8 +229,7 @@ public void connectWithShardInfoAndEmptyTrustStore() throws Exception { JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, null, null); shardInfo.setPassword("foobared"); - Jedis jedis = new Jedis(shardInfo); - try { + try (Jedis jedis = new Jedis(shardInfo)) { assertEquals("PONG", jedis.ping()); fail("The code did not throw the expected JedisConnectionException."); } catch (JedisConnectionException e) { @@ -240,12 +240,6 @@ public void connectWithShardInfoAndEmptyTrustStore() throws Exception { assertEquals("Unexpected third inner exception.", InvalidAlgorithmParameterException.class, e.getCause().getCause().getCause().getClass()); } - - try { - jedis.close(); - } catch (Throwable e1) { - // Expected. - } } /** diff --git a/src/test/java/redis/clients/jedis/tests/SSLJedisWithCompleteCredentialsTest.java b/src/test/java/redis/clients/jedis/tests/SSLJedisWithCompleteCredentialsTest.java index 86b85bbc7a..87d434a0bd 100644 --- a/src/test/java/redis/clients/jedis/tests/SSLJedisWithCompleteCredentialsTest.java +++ b/src/test/java/redis/clients/jedis/tests/SSLJedisWithCompleteCredentialsTest.java @@ -1,13 +1,5 @@ package redis.clients.jedis.tests; -import org.junit.BeforeClass; -import org.junit.Test; -import redis.clients.jedis.HostAndPort; -import redis.clients.jedis.Jedis; -import redis.clients.jedis.JedisShardInfo; -import redis.clients.jedis.exceptions.JedisConnectionException; -import redis.clients.jedis.tests.utils.RedisVersionUtil; - import javax.net.ssl.*; import java.io.FileInputStream; import java.io.InputStream; @@ -18,6 +10,16 @@ import java.security.cert.CertificateException; import java.security.cert.X509Certificate; +import org.junit.BeforeClass; +import org.junit.Test; + +import redis.clients.jedis.DefaultJedisClientConfig; +import redis.clients.jedis.HostAndPort; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisShardInfo; +import redis.clients.jedis.exceptions.JedisConnectionException; +import redis.clients.jedis.tests.utils.RedisVersionUtil; + import static org.junit.Assert.*; /** @@ -26,7 +28,6 @@ * This test is only executed when the server/cluster is Redis 6. or more. */ public class SSLJedisWithCompleteCredentialsTest { - private static HostAndPort hnp = HostAndPortUtil.getRedisServers().get(0); @BeforeClass public static void prepare() { @@ -36,9 +37,22 @@ public static void prepare() { SSLJedisTest.setupTrustStore(); } - /** - * Tests opening a default SSL/TLS connection to redis using "rediss://" scheme url. - */ + @Test + public void connectWithSsl() { + try (Jedis jedis = new Jedis("localhost", 6390, true)) { + jedis.auth("acljedis", "fizzbuzz"); + assertEquals("PONG", jedis.ping()); + } + } + + @Test + public void connectWithConfig() { + try (Jedis jedis = new Jedis(new HostAndPort("localhost", 6390), DefaultJedisClientConfig.builder().withSsl(true).build())) { + jedis.auth("acljedis", "fizzbuzz"); + assertEquals("PONG", jedis.ping()); + } + } + @Test public void connectWithUrl() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. @@ -52,11 +66,8 @@ public void connectWithUrl() { } } - /** - * Tests opening a default SSL/TLS connection to redis using "rediss://" scheme url. - */ @Test - public void connectWithUrlAndCompleteCredentials() { + public void connectWithCompleteCredentialsUrl() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. try (Jedis jedis = new Jedis("rediss://default:foobared@localhost:6390")) { assertEquals("PONG", jedis.ping()); @@ -66,12 +77,8 @@ public void connectWithUrlAndCompleteCredentials() { } } - - /** - * Tests opening a default SSL/TLS connection to redis. - */ @Test - public void connectWithoutShardInfo() { + public void connectWithUri() { // The "rediss" scheme instructs jedis to open a SSL/TLS connection. try (Jedis jedis = new Jedis(URI.create("rediss://localhost:6390"))) { jedis.auth("acljedis", "fizzbuzz"); @@ -79,6 +86,17 @@ public void connectWithoutShardInfo() { } } + @Test + public void connectWithCompleteCredentialsUri() { + // The "rediss" scheme instructs jedis to open a SSL/TLS connection. + try (Jedis jedis = new Jedis(URI.create("rediss://default:foobared@localhost:6390"))) { + assertEquals("PONG", jedis.ping()); + } + try (Jedis jedis = new Jedis(URI.create("rediss://acljedis:fizzbuzz@localhost:6390"))) { + assertEquals("PONG", jedis.ping()); + } + } + /** * Tests opening an SSL/TLS connection to redis. * NOTE: This test relies on a feature that is only available as of Java 7 and later. @@ -99,10 +117,9 @@ public void connectWithShardInfo() throws Exception { shardInfo.setUser("acljedis"); shardInfo.setPassword("fizzbuzz"); - Jedis jedis = new Jedis(shardInfo); - assertEquals("PONG", jedis.ping()); - jedis.disconnect(); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + assertEquals("PONG", jedis.ping()); + } } /** @@ -129,8 +146,7 @@ public void connectWithShardInfoByIpAddress() throws Exception { shardInfo.setUser("acljedis"); shardInfo.setPassword("fizzbuzz"); - Jedis jedis = new Jedis(shardInfo); - try { + try (Jedis jedis = new Jedis(shardInfo)) { assertEquals("PONG", jedis.ping()); fail("The code did not throw the expected JedisConnectionException."); } catch (JedisConnectionException e) { @@ -139,12 +155,6 @@ public void connectWithShardInfoByIpAddress() throws Exception { assertEquals("Unexpected second inner exception.", CertificateException.class, e.getCause().getCause().getClass()); } - - try { - jedis.close(); - } catch (Throwable e1) { - // Expected. - } } /** @@ -162,10 +172,9 @@ public void connectWithShardInfoAndCustomHostnameVerifier() { shardInfo.setUser("acljedis"); shardInfo.setPassword("fizzbuzz"); - Jedis jedis = new Jedis(shardInfo); - assertEquals("PONG", jedis.ping()); - jedis.disconnect(); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + assertEquals("PONG", jedis.ping()); + } } /** @@ -182,10 +191,9 @@ public void connectWithShardInfoAndCustomSocketFactory() throws Exception { shardInfo.setUser("acljedis"); shardInfo.setPassword("fizzbuzz"); - Jedis jedis = new Jedis(shardInfo); - assertEquals("PONG", jedis.ping()); - jedis.disconnect(); - jedis.close(); + try (Jedis jedis = new Jedis(shardInfo)) { + assertEquals("PONG", jedis.ping()); + } } /** @@ -205,20 +213,13 @@ public void connectWithShardInfoAndCustomHostnameVerifierByIpAddress() { shardInfo.setUser("acljedis"); shardInfo.setPassword("fizzbuzz"); - Jedis jedis = new Jedis(shardInfo); - try { + try (Jedis jedis = new Jedis(shardInfo)) { assertEquals("PONG", jedis.ping()); fail("The code did not throw the expected JedisConnectionException."); } catch (JedisConnectionException e) { assertEquals("The JedisConnectionException does not contain the expected message.", "The connection to '127.0.0.1' failed ssl/tls hostname verification.", e.getMessage()); } - - try { - jedis.close(); - } catch (Throwable e1) { - // Expected. - } } /** @@ -238,8 +239,7 @@ public void connectWithShardInfoAndEmptyTrustStore() throws Exception { shardInfo.setUser("acljedis"); shardInfo.setPassword("fizzbuzz"); - Jedis jedis = new Jedis(shardInfo); - try { + try (Jedis jedis = new Jedis(shardInfo)) { assertEquals("PONG", jedis.ping()); fail("The code did not throw the expected JedisConnectionException."); } catch (JedisConnectionException e) { @@ -250,12 +250,6 @@ public void connectWithShardInfoAndEmptyTrustStore() throws Exception { assertEquals("Unexpected third inner exception.", InvalidAlgorithmParameterException.class, e.getCause().getCause().getCause().getClass()); } - - try { - jedis.close(); - } catch (Throwable e1) { - // Expected. - } } /** diff --git a/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java b/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java index a74e0c4e84..eb2c8a2070 100644 --- a/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java +++ b/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolTest.java @@ -136,49 +136,6 @@ public void checkFailedJedisServer() { pool.destroy(); } - @Test - public void shouldReturnActiveShardsWhenOneGoesOffline() { - GenericObjectPoolConfig redisConfig = new GenericObjectPoolConfig(); - redisConfig.setTestOnBorrow(false); - ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards); - ShardedJedis jedis = pool.getResource(); - // fill the shards - for (int i = 0; i < 1000; i++) { - jedis.set("a-test-" + i, "0"); - } - jedis.close(); - // check quantity for each shard - Jedis j = new Jedis(shards.get(0)); - j.connect(); - Long c1 = j.dbSize(); - j.disconnect(); - j = new Jedis(shards.get(1)); - j.connect(); - Long c2 = j.dbSize(); - j.disconnect(); - // shutdown shard 2 and check thay the pool returns an instance with c1 - // items on one shard - // alter shard 1 and recreate pool - pool.destroy(); - shards.set(1, new JedisShardInfo("localhost", 1234)); - pool = new ShardedJedisPool(redisConfig, shards); - jedis = pool.getResource(); - long actual = 0; - long fails = 0; - for (int i = 0; i < 1000; i++) { - try { - jedis.get("a-test-" + i); - actual++; - } catch (RuntimeException e) { - fails++; - } - } - jedis.close(); - pool.destroy(); - assertEquals(Long.valueOf(actual), c1); - assertEquals(Long.valueOf(fails), c2); - } - @Test public void startWithUrlString() { Jedis j = new Jedis("localhost", 6380); diff --git a/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolWithCompleteCredentialsTest.java b/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolWithCompleteCredentialsTest.java index 5aeb3440a1..be257f596a 100644 --- a/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolWithCompleteCredentialsTest.java +++ b/src/test/java/redis/clients/jedis/tests/ShardedJedisPoolWithCompleteCredentialsTest.java @@ -145,49 +145,6 @@ public void checkFailedJedisServer() { pool.destroy(); } - @Test - public void shouldReturnActiveShardsWhenOneGoesOffline() { - GenericObjectPoolConfig redisConfig = new GenericObjectPoolConfig(); - redisConfig.setTestOnBorrow(false); - ShardedJedisPool pool = new ShardedJedisPool(redisConfig, shards); - ShardedJedis jedis = pool.getResource(); - // fill the shards - for (int i = 0; i < 1000; i++) { - jedis.set("a-test-" + i, "0"); - } - jedis.close(); - // check quantity for each shard - Jedis j = new Jedis(shards.get(0)); - j.connect(); - Long c1 = j.dbSize(); - j.disconnect(); - j = new Jedis(shards.get(1)); - j.connect(); - Long c2 = j.dbSize(); - j.disconnect(); - // shutdown shard 2 and check thay the pool returns an instance with c1 - // items on one shard - // alter shard 1 and recreate pool - pool.destroy(); - shards.set(1, new JedisShardInfo("localhost", 1234)); - pool = new ShardedJedisPool(redisConfig, shards); - jedis = pool.getResource(); - long actual = 0; - long fails = 0; - for (int i = 0; i < 1000; i++) { - try { - jedis.get("a-test-" + i); - actual++; - } catch (RuntimeException e) { - fails++; - } - } - jedis.close(); - pool.destroy(); - assertEquals(Long.valueOf(actual), c1); - assertEquals(Long.valueOf(fails), c2); - } - @Test public void startWithUrlString() { Jedis j = new Jedis("localhost", 6380); diff --git a/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java b/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java index 50743fcf9b..14e0abf4de 100644 --- a/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java +++ b/src/test/java/redis/clients/jedis/tests/ShardedJedisTest.java @@ -270,9 +270,9 @@ public void testMasterSlaveShardingConsistency() { Hashing.MURMUR_HASH); List otherShards = new ArrayList(3); - otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT)); - otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1)); - otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2)); + otherShards.add(new JedisShardInfo("127.0.0.1", Protocol.DEFAULT_PORT)); + otherShards.add(new JedisShardInfo("127.0.0.1", Protocol.DEFAULT_PORT + 1)); + otherShards.add(new JedisShardInfo("127.0.0.1", Protocol.DEFAULT_PORT + 2)); Sharded sharded2 = new Sharded(otherShards, Hashing.MURMUR_HASH); @@ -294,9 +294,9 @@ public void testMasterSlaveShardingConsistencyWithShardNaming() { Hashing.MURMUR_HASH); List otherShards = new ArrayList(3); - otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT, "HOST2:1234")); - otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 1, "HOST3:1234")); - otherShards.add(new JedisShardInfo("otherhost", Protocol.DEFAULT_PORT + 2, "HOST1:1234")); + otherShards.add(new JedisShardInfo("127.0.0.1", Protocol.DEFAULT_PORT, "HOST2:1234")); + otherShards.add(new JedisShardInfo("127.0.0.1", Protocol.DEFAULT_PORT + 1, "HOST3:1234")); + otherShards.add(new JedisShardInfo("127.0.0.1", Protocol.DEFAULT_PORT + 2, "HOST1:1234")); Sharded sharded2 = new Sharded(otherShards, Hashing.MURMUR_HASH); diff --git a/src/test/java/redis/clients/jedis/tests/UdsTest.java b/src/test/java/redis/clients/jedis/tests/UdsTest.java index 244fd37623..f95f1d944e 100644 --- a/src/test/java/redis/clients/jedis/tests/UdsTest.java +++ b/src/test/java/redis/clients/jedis/tests/UdsTest.java @@ -1,15 +1,16 @@ package redis.clients.jedis.tests; +import java.io.File; +import java.io.IOException; +import java.net.Socket; import org.junit.Test; import org.newsclub.net.unix.AFUNIXSocket; import org.newsclub.net.unix.AFUNIXSocketAddress; + import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisSocketFactory; import redis.clients.jedis.Protocol; - -import java.io.File; -import java.io.IOException; -import java.net.Socket; +import redis.clients.jedis.exceptions.JedisConnectionException; import static org.junit.Assert.assertEquals; @@ -27,10 +28,14 @@ private static class UdsJedisSocketFactory implements JedisSocketFactory { private static final File UDS_SOCKET = new File("/tmp/redis_uds.sock"); @Override - public Socket createSocket() throws IOException { - Socket socket = AFUNIXSocket.newStrictInstance(); - socket.connect(new AFUNIXSocketAddress(UDS_SOCKET), Protocol.DEFAULT_TIMEOUT); - return socket; + public Socket createSocket() throws JedisConnectionException { + try { + Socket socket = AFUNIXSocket.newStrictInstance(); + socket.connect(new AFUNIXSocketAddress(UDS_SOCKET), Protocol.DEFAULT_TIMEOUT); + return socket; + } catch (IOException ioe) { + throw new JedisConnectionException("Failed to create UDS connection.", ioe); + } } @Override diff --git a/src/test/java/redis/clients/jedis/tests/commands/AccessControlListCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/AccessControlListCommandsTest.java index 266de77626..9e0bed850d 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/AccessControlListCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/AccessControlListCommandsTest.java @@ -107,7 +107,7 @@ public void createUserAndPasswords() { assertEquals("OK", status); // create a new client to try to authenticate - Jedis jedis2 = new Jedis("localhost"); + Jedis jedis2 = new Jedis(); String authResult = null; // the user is just created without any permission the authentication should fail @@ -168,7 +168,7 @@ public void aclSetUserWithAnyPassword() { assertEquals("OK", status); // connect with this new user and try to get/set keys - Jedis jedis2 = new Jedis("localhost"); + Jedis jedis2 = new Jedis(); String authResult = jedis2.auth(USER_ZZZ, "any password"); assertEquals("OK", authResult); @@ -193,7 +193,7 @@ public void aclExcudeSingleCommand() { assertEquals("OK", status); // connect with this new user and try to get/set keys - Jedis jedis2 = new Jedis("localhost"); + Jedis jedis2 = new Jedis(); String authResult = jedis2.auth(USER_ZZZ, "any password"); assertEquals("OK", authResult); @@ -238,7 +238,7 @@ public void basicPermissionsTest() { String authResult = jedis.aclSetUser(USER_ZZZ, "on", "+acl"); // connect with this new user and try to get/set keys - Jedis jedis2 = new Jedis("localhost"); + Jedis jedis2 = new Jedis(); jedis2.auth(USER_ZZZ, USER_ZZZ_PASSWORD); String result = null; diff --git a/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java b/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java index 0308a5d579..e3aac9667e 100644 --- a/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java +++ b/src/test/java/redis/clients/jedis/tests/commands/ControlCommandsTest.java @@ -84,7 +84,7 @@ public void run() { Thread.sleep(100); } catch (InterruptedException e) { } - Jedis j = new Jedis("localhost"); + Jedis j = new Jedis(); j.auth("foobared"); for (int i = 0; i < 5; i++) { j.incr("foobared");