From 64196a43906e12f3bc8e84ca28c602e1439b91f8 Mon Sep 17 00:00:00 2001 From: Mark Paluch Date: Sun, 2 Aug 2015 21:52:49 +0200 Subject: [PATCH] Expose codec/uri methods on RedisClient #108 --- .../com/lambdaworks/redis/RedisClient.java | 137 ++++++++++------- .../redis/protocol/CommandHandler.java | 1 - .../lambdaworks/redis/RedisClientTest.java | 141 ++++++++++++++++++ 3 files changed, 222 insertions(+), 57 deletions(-) create mode 100644 src/test/java/com/lambdaworks/redis/RedisClientTest.java diff --git a/src/main/java/com/lambdaworks/redis/RedisClient.java b/src/main/java/com/lambdaworks/redis/RedisClient.java index b471c60026..e151f057d6 100644 --- a/src/main/java/com/lambdaworks/redis/RedisClient.java +++ b/src/main/java/com/lambdaworks/redis/RedisClient.java @@ -22,9 +22,9 @@ import com.lambdaworks.redis.pubsub.RedisPubSubConnectionImpl; /** - * A scalable thread-safe Redis client. Multiple threads may share one connection provided they - * avoid blocking and transactional operations such as BLPOP and MULTI/EXEC. - * + * A scalable thread-safe Redis client. Multiple threads may share one connection if they avoid + * blocking and transactional operations such as BLPOP and MULTI/EXEC. + * * @author Will Glozer * @author Mark Paluch */ @@ -33,7 +33,7 @@ public class RedisClient extends AbstractRedisClient { private final RedisURI redisURI; /** - * Creates a uri-less RedisClient. You can connect to different redis servers but you must supply a {@link RedisURI} on + * Creates a uri-less RedisClient. You can connect to different Redis servers but you must supply a {@link RedisURI} on * connecting. Methods without having a {@link RedisURI} will fail with a {@link java.lang.IllegalStateException}. */ public RedisClient() { @@ -103,8 +103,8 @@ public RedisConnectionPool> pool(int maxIdle, in * @param codec the codec to use * @param maxIdle max idle connections in pool * @param maxActive max active connections in pool - * @param Key type. - * @param Value type. + * @param Key type + * @param Value type * @return a new {@link RedisConnectionPool} instance */ @SuppressWarnings("unchecked") @@ -178,8 +178,8 @@ public RedisConnectionPool> asyncPool(int m * @param codec the codec to use * @param maxIdle max idle connections in pool * @param maxActive max active connections in pool - * @param Key type. - * @param Value type. + * @param Key type + * @param Value type * @return a new {@link RedisConnectionPool} instance */ public RedisConnectionPool> asyncPool(final RedisCodec codec, int maxIdle, @@ -214,7 +214,7 @@ public void resourceClosed(Object resource) { } /** - * Open a new synchronous connection to the redis server that treats keys and values as UTF-8 strings. + * Open a connection to a Redis server that treats keys and values as UTF-8 strings. * * @return A new connection. */ @@ -224,25 +224,24 @@ public RedisConnection connect() { } /** - * Open a new synchronous connection to the redis server. Use the supplied {@link RedisCodec codec} to encode/decode keys - * and values. + * Open a new synchronous connection to a Redis server. Use the supplied {@link RedisCodec codec} to encode/decode keys and + * values. * - * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} - * @param Key type. - * @param Value type. - * @return A new connection. + * @param codec Use this codec to encode/decode keys and values, must note be {@literal null} + * @param Key type + * @param Value type + * @return A new Redis connection. */ - @SuppressWarnings("unchecked") public RedisConnection connect(RedisCodec codec) { checkForRedisURI(); - checkArgument(codec != null, "RedisCodec must not be null"); return connect(codec, this.redisURI); } /** - * Open a new synchronous connection to the supplied {@link RedisURI} that treats keys and values as UTF-8 strings. + * Open a new synchronous to a Redis server using the supplied {@link RedisURI} that treats keys and values as UTF-8 + * strings. * - * @param redisURI the redis server to connect to, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} * @return A new connection. */ @SuppressWarnings({ "unchecked", "rawtypes" }) @@ -256,11 +255,13 @@ private void checkValidRedisURI(RedisURI redisURI) { } /** - * Open a new synchronous connection to the supplied {@link RedisURI} and the supplied {@link RedisCodec codec} to - * encode/decode keys. + * Open a new synchronous connection to a Redis server using the supplied {@link RedisURI} and the supplied + * {@link RedisCodec codec} to encode/decode keys. * * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} - * @param redisURI the redis server to connect to, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} + * @param Key type + * @param Value type * @return A new connection. */ public RedisConnection connect(RedisCodec codec, RedisURI redisURI) { @@ -269,7 +270,7 @@ public RedisConnection connect(RedisCodec codec, RedisURI redisURI) } /** - * Open a new asynchronous connection to the redis server that treats keys and values as UTF-8 strings. + * Open a new asynchronous connection to a Redis server that treats keys and values as UTF-8 strings. * * @return A new connection. */ @@ -278,24 +279,24 @@ public RedisAsyncConnection connectAsync() { } /** - * Open a new asynchronous connection to the redis server. Use the supplied {@link RedisCodec codec} to encode/decode keys - * and values. + * Open a new asynchronous connection to a Redis server. Use the supplied {@link RedisCodec codec} to encode/decode keys and + * values. * * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} - * @param Key type. - * @param Value type. + * @param Key type + * @param Value type * @return A new connection. */ public RedisAsyncConnection connectAsync(RedisCodec codec) { checkForRedisURI(); - checkArgument(codec != null, "RedisCodec must not be null"); return connectAsync(codec, redisURI); } /** - * Open a new asynchronous connection to the supplied {@link RedisURI} that treats keys and values as UTF-8 strings. + * Open a new asynchronous connection to a Redis server using the supplied {@link RedisURI} that treats keys and values as + * UTF-8 strings. * - * @param redisURI the redis server to connect to, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} * @return A new connection. */ public RedisAsyncConnection connectAsync(RedisURI redisURI) { @@ -304,11 +305,13 @@ public RedisAsyncConnection connectAsync(RedisURI redisURI) { } /** - * Open a new asynchronous connection to the supplied {@link RedisURI} and the supplied {@link RedisCodec codec} to - * encode/decode keys. + * Open a new asynchronous connection to a Redis server using the supplied {@link RedisURI} and the supplied + * {@link RedisCodec codec} to encode/decode keys. * * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} - * @param redisURI the redis server to connect to, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} + * @param Key type + * @param Value type * @return A new connection. */ public RedisAsyncConnection connectAsync(RedisCodec codec, RedisURI redisURI) { @@ -351,19 +354,20 @@ private void connectAsync(CommandHandler handler, RedisAsyncConnect } /** - * Open a new pub/sub connection to the redis server that treats keys and values as UTF-8 strings. + * Open a new pub/sub connection to the Redis server that treats keys and values as UTF-8 strings. * - * @return A new connection. + * @return A new connection */ public RedisPubSubConnection connectPubSub() { return connectPubSub(newStringStringCodec()); } /** - * Open a new pub/sub connection to the supplied {@link RedisURI} that treats keys and values as UTF-8 strings. + * Open a new pub/sub connection to the Redis server using the supplied {@link RedisURI} that treats keys and values as + * UTF-8 strings. * - * @param redisURI the redis server to connect to, must not be {@literal null} - * @return A new connection. + * @param redisURI the Redis server to connect to, must not be {@literal null} + * @return A new pub/sub connection */ public RedisPubSubConnection connectPubSub(RedisURI redisURI) { checkValidRedisURI(redisURI); @@ -371,13 +375,13 @@ public RedisPubSubConnection connectPubSub(RedisURI redisURI) { } /** - * Open a new pub/sub connection to the redis server. Use the supplied {@link RedisCodec codec} to encode/decode keys and - * values. + * Open a new pub/sub connection to the Redis server using the supplied {@link RedisURI} and use the supplied + * {@link RedisCodec codec} to encode/decode keys and values. * * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} - * @param Key type. - * @param Value type. - * @return A new pub/sub connection. + * @param Key type + * @param Value type + * @return A new pub/sub connection */ public RedisPubSubConnection connectPubSub(RedisCodec codec) { checkForRedisURI(); @@ -385,18 +389,19 @@ public RedisPubSubConnection connectPubSub(RedisCodec codec) } /** - * Open a new pub/sub connection to the supplied {@link RedisURI} and the supplied {@link RedisCodec codec} to encode/decode - * keys. - * + * Open a new pub/sub connection to the Redis server using the supplied {@link RedisURI} and use the supplied + * {@link RedisCodec codec} to encode/decode keys and values. + * * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} - * * @param redisURI the redis server to connect to, must not be {@literal null} + * @param Key type + * @param Value type * @return A new connection. */ public RedisPubSubConnection connectPubSub(RedisCodec codec, RedisURI redisURI) { - checkArgument(codec != null, "RedisCodec must not be null"); checkArgument(redisURI != null, "RedisURI must not be null"); + Queue> queue = new ArrayDeque>(); PubSubCommandHandler handler = new PubSubCommandHandler(clientOptions, queue, codec); RedisPubSubConnectionImpl connection = newRedisPubSubConnectionImpl(handler, codec, timeout, unit); @@ -407,7 +412,8 @@ public RedisPubSubConnection connectPubSub(RedisCodec codec, } /** - * Creates an asynchronous connection to Sentinel. You must supply a valid RedisURI containing one or more sentinels. + * Creates an asynchronous connection to Redis Sentinel that treats keys and values as UTF-8 strings. You must supply a + * valid RedisURI containing one or more sentinels. * * @return a new connection. */ @@ -416,31 +422,50 @@ public RedisSentinelAsyncConnection connectSentinelAsync() { } /** - * Creates an asynchronous connection to Sentinel. You must supply a valid RedisURI containing one or more sentinels. + * Creates an asynchronous connection to Redis Sentinela nd use the supplied {@link RedisCodec codec} to encode/decode keys + * and values. You must supply a valid RedisURI containing one or more sentinels. * * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} - * @param Key type. - * @param Value type. + * @param Key type + * @param Value type * @return a new connection. */ public RedisSentinelAsyncConnection connectSentinelAsync(RedisCodec codec) { checkForRedisURI(); - checkArgument(codec != null, "RedisCodec must not be null"); return connectSentinelAsyncImpl(codec, redisURI); } /** - * Creates an asynchronous connection to Sentinel. You must supply a valid RedisURI containing a redis host or one or more - * sentinels. + * Creates an asynchronous connection to Redis Sentinel using the supplied {@link RedisURI} that treats keys and values as + * UTF-8 strings. You must supply a valid RedisURI containing a redis host or one or more sentinels. * - * @param redisURI the redis server to connect to, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} * @return A new connection. */ public RedisSentinelAsyncConnection connectSentinelAsync(RedisURI redisURI) { return connectSentinelAsyncImpl(newStringStringCodec(), redisURI); } + /** + * Creates an asynchronous connection to Redis Sentinel using the supplied {@link RedisURI} and use the supplied + * {@link RedisCodec codec} to encode/decode keys and values. You must supply a valid RedisURI containing a redis host or + * one or more sentinels. + * + * @param codec Use this codec to encode/decode keys and values, must not be {@literal null} + * @param redisURI the Redis server to connect to, must not be {@literal null} + * @param Key type + * @param Value type + * @return A new connection. + */ + public RedisSentinelAsyncConnection connectSentinelAsync(RedisCodec codec, RedisURI redisURI) { + return connectSentinelAsyncImpl(codec, redisURI); + } + private RedisSentinelAsyncConnection connectSentinelAsyncImpl(RedisCodec codec, RedisURI redisURI) { + + checkArgument(codec != null, "RedisCodec must not be null"); + checkArgument(redisURI != null, "RedisURI must not be null"); + Queue> queue = new ArrayDeque>(); ConnectionBuilder connectionBuilder = ConnectionBuilder.connectionBuilder(); diff --git a/src/main/java/com/lambdaworks/redis/protocol/CommandHandler.java b/src/main/java/com/lambdaworks/redis/protocol/CommandHandler.java index 74596b0a18..e79322ecc9 100644 --- a/src/main/java/com/lambdaworks/redis/protocol/CommandHandler.java +++ b/src/main/java/com/lambdaworks/redis/protocol/CommandHandler.java @@ -293,7 +293,6 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) private void queueCommand(ChannelPromise promise, RedisCommand cmd) throws Exception { if (cmd.isCancelled()) { - System.out.println("cancelled"); return; } diff --git a/src/test/java/com/lambdaworks/redis/RedisClientTest.java b/src/test/java/com/lambdaworks/redis/RedisClientTest.java new file mode 100644 index 0000000000..75fcf0b793 --- /dev/null +++ b/src/test/java/com/lambdaworks/redis/RedisClientTest.java @@ -0,0 +1,141 @@ +package com.lambdaworks.redis; + +import static com.lambdaworks.redis.RedisURI.Builder.redis; + +import org.junit.Test; + +import com.lambdaworks.redis.codec.Utf8StringCodec; + +/** + * @author Mark Paluch + */ +public class RedisClientTest extends AbstractCommandTest { + + /* + * Pool/Sync + */ + @Test + public void poolClientUri() throws Exception { + client.pool().close(); + } + + @Test + public void poolClientUriConfig() throws Exception { + client.pool(1, 1).close(); + } + + @Test + public void poolCodecClientUriConfig() throws Exception { + client.pool(new Utf8StringCodec(), 1, 1).close(); + } + + /* + * Pool/Async + */ + @Test + public void asyncPoolClientUri() throws Exception { + client.asyncPool().close(); + } + + @Test + public void asyncPoolClientUriConfig() throws Exception { + client.asyncPool(1, 1).close(); + } + + @Test + public void asyncPoolCodecClientUriConfig() throws Exception { + client.asyncPool(new Utf8StringCodec(), 1, 1).close(); + } + + /* + * Standalone/Stateful + */ + @Test + public void connectClienturi() throws Exception { + client.connect().close(); + } + + @Test + public void connectCodecClientUri() throws Exception { + client.connect(new Utf8StringCodec()).close(); + } + + @Test + public void connectOwnUri() throws Exception { + client.connect(redis(host, port).build()).close(); + } + + @Test + public void connectCodecOwnUri() throws Exception { + client.connect(new Utf8StringCodec(), redis(host, port).build()).close(); + } + + /* + * Deprecated: Standalone/Async + */ + @Test + public void connectAsyncClientUri() throws Exception { + client.connectAsync().close(); + } + + @Test + public void connectAsyncCodecClientUri() throws Exception { + client.connectAsync(new Utf8StringCodec()).close(); + } + + @Test + public void connectAsyncOwnUri() throws Exception { + client.connectAsync(redis(host, port).build()).close(); + } + + @Test + public void connectAsyncCodecOwnUri() throws Exception { + client.connectAsync(new Utf8StringCodec(), redis(host, port).build()).close(); + } + + /* + * Standalone/PubSub Stateful + */ + @Test + public void connectPubSubClientUri() throws Exception { + client.connectPubSub().close(); + } + + @Test + public void connectPubSubCodecClientUri() throws Exception { + client.connectPubSub(new Utf8StringCodec()).close(); + } + + @Test + public void connectPubSubOwnUri() throws Exception { + client.connectPubSub(redis(host, port).build()).close(); + } + + @Test + public void connectPubSubCodecOwnUri() throws Exception { + client.connectPubSub(new Utf8StringCodec(), redis(host, port).build()).close(); + } + + /* + * Deprecated: Sentinel/Async + */ + @Test + public void connectSentinelAsyncClientUri() throws Exception { + client.connectSentinelAsync().close(); + } + + @Test + public void connectSentinelAsyncCodecClientUri() throws Exception { + client.connectSentinelAsync(new Utf8StringCodec()).close(); + } + + @Test + public void connectSentineAsynclOwnUri() throws Exception { + client.connectSentinelAsync(redis(host, port).build()).close(); + } + + @Test + public void connectSentinelAsyncCodecOwnUri() throws Exception { + client.connectSentinelAsync(new Utf8StringCodec(), redis(host, port).build()).close(); + } +}