diff --git a/java/client/src/test/java/glide/api/RedisClientTest.java b/java/client/src/test/java/glide/api/RedisClientTest.java index 2aee44867d..de67a2e959 100644 --- a/java/client/src/test/java/glide/api/RedisClientTest.java +++ b/java/client/src/test/java/glide/api/RedisClientTest.java @@ -105,8 +105,8 @@ public void customCommand_returns_success() { Object value = "testValue"; String cmd = "GETSTRING"; String[] arguments = new String[] {cmd, key}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(CustomCommand), eq(arguments), any())) @@ -125,8 +125,8 @@ public void customCommand_returns_success() { @Test public void ping_returns_success() { // setup - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn("PONG"); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete("PONG"); // match on protobuf request when(commandManager.submitNewCommand(eq(Ping), eq(new String[0]), any())) @@ -147,7 +147,7 @@ public void ping_with_message_returns_success() { // setup String message = "RETURN OF THE PONG"; String[] arguments = new String[] {message}; - CompletableFuture testResponse = new CompletableFuture(); + CompletableFuture testResponse = new CompletableFuture<>(); testResponse.complete(message); // match on protobuf request @@ -167,9 +167,9 @@ public void ping_with_message_returns_success() { @Test public void select_returns_success() { // setup - CompletableFuture testResponse = mock(CompletableFuture.class); - Long index = 5L; - when(testResponse.get()).thenReturn(OK); + CompletableFuture testResponse = new CompletableFuture<>(); + long index = 5L; + testResponse.complete(OK); // match on protobuf request when(commandManager.submitNewCommand( @@ -191,8 +191,8 @@ public void del_returns_long_success() { // setup String[] keys = new String[] {"testKey1", "testKey2"}; Long numberDeleted = 1L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(numberDeleted); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(numberDeleted); when(commandManager.submitNewCommand(eq(Del), eq(keys), any())).thenReturn(testResponse); // exercise @@ -210,8 +210,8 @@ public void unlink_returns_long_success() { // setup String[] keys = new String[] {"testKey1", "testKey2"}; Long numberUnlinked = 1L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(numberUnlinked); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(numberUnlinked); // match on protobuf request when(commandManager.submitNewCommand(eq(Unlink), eq(keys), any())) @@ -232,8 +232,8 @@ public void get_returns_success() { // setup String key = "testKey"; String value = "testValue"; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); when(commandManager.submitNewCommand(eq(GetString), eq(new String[] {key}), any())) .thenReturn(testResponse); @@ -252,9 +252,10 @@ public void set_returns_success() { // setup String key = "testKey"; String value = "testValue"; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(null); - when(commandManager.submitNewCommand(eq(SetString), eq(new String[] {key, value}), any())) + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(null); + when(commandManager.submitNewCommand( + eq(SetString), eq(new String[] {key, value}), any())) .thenReturn(testResponse); // exercise @@ -280,8 +281,8 @@ public void set_with_SetOptions_OnlyIfExists_returns_success() { .build(); String[] arguments = new String[] {key, value, ONLY_IF_EXISTS.getRedisApi(), "KEEPTTL"}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(null); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(null); when(commandManager.submitNewCommand(eq(SetString), eq(arguments), any())) .thenReturn(testResponse); @@ -309,8 +310,8 @@ public void set_with_SetOptions_OnlyIfDoesNotExist_returns_success() { new String[] { key, value, ONLY_IF_DOES_NOT_EXIST.getRedisApi(), RETURN_OLD_VALUE, "EXAT", "60" }; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); when(commandManager.submitNewCommand(eq(SetString), eq(arguments), any())) .thenReturn(testResponse); @@ -328,8 +329,8 @@ public void exists_returns_long_success() { // setup String[] keys = new String[] {"testKey1", "testKey2"}; Long numberExisting = 1L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(numberExisting); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(numberExisting); when(commandManager.submitNewCommand(eq(Exists), eq(keys), any())) .thenReturn(testResponse); @@ -350,8 +351,8 @@ public void expire_returns_success() { long seconds = 10L; String[] arguments = new String[] {key, Long.toString(seconds)}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(true); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(Boolean.TRUE); // match on protobuf request when(commandManager.submitNewCommand(eq(Expire), eq(arguments), any())) @@ -373,8 +374,8 @@ public void expire_with_expireOptions_returns_success() { long seconds = 10L; String[] arguments = new String[] {key, Long.toString(seconds), "NX"}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(false); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(Boolean.FALSE); // match on protobuf request when(commandManager.submitNewCommand(eq(Expire), eq(arguments), any())) @@ -396,8 +397,8 @@ public void expireAt_returns_success() { long unixSeconds = 100000L; String[] arguments = new String[] {key, Long.toString(unixSeconds)}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(true); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(Boolean.TRUE); // match on protobuf request when(commandManager.submitNewCommand(eq(ExpireAt), eq(arguments), any())) @@ -419,8 +420,8 @@ public void expireAt_with_expireOptions_returns_success() { long unixSeconds = 100000L; String[] arguments = new String[] {key, Long.toString(unixSeconds), "XX"}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(false); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(Boolean.FALSE); // match on protobuf request when(commandManager.submitNewCommand(eq(ExpireAt), eq(arguments), any())) @@ -443,8 +444,8 @@ public void pexpire_returns_success() { long milliseconds = 50000L; String[] arguments = new String[] {key, Long.toString(milliseconds)}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(true); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(Boolean.TRUE); // match on protobuf request when(commandManager.submitNewCommand(eq(PExpire), eq(arguments), any())) @@ -466,8 +467,8 @@ public void pexpire_with_expireOptions_returns_success() { long milliseconds = 50000L; String[] arguments = new String[] {key, Long.toString(milliseconds), "LT"}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(false); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(Boolean.FALSE); // match on protobuf request when(commandManager.submitNewCommand(eq(PExpire), eq(arguments), any())) @@ -490,8 +491,8 @@ public void pexpireAt_returns_success() { long unixMilliseconds = 999999L; String[] arguments = new String[] {key, Long.toString(unixMilliseconds)}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(true); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(Boolean.TRUE); // match on protobuf request when(commandManager.submitNewCommand(eq(PExpireAt), eq(arguments), any())) @@ -513,8 +514,8 @@ public void pexpireAt_with_expireOptions_returns_success() { long unixMilliseconds = 999999L; String[] arguments = new String[] {key, Long.toString(unixMilliseconds), "GT"}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(false); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(Boolean.FALSE); // match on protobuf request when(commandManager.submitNewCommand(eq(PExpireAt), eq(arguments), any())) @@ -535,9 +536,8 @@ public void ttl_returns_success() { // setup String key = "testKey"; long ttl = 999L; - - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(ttl); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(ttl); // match on protobuf request when(commandManager.submitNewCommand(eq(TTL), eq(new String[] {key}), any())) @@ -555,9 +555,9 @@ public void ttl_returns_success() { @Test public void info_returns_success() { // setup - CompletableFuture testResponse = mock(CompletableFuture.class); String testPayload = "Key: Value"; - when(testResponse.get()).thenReturn(testPayload); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(testPayload); when(commandManager.submitNewCommand(eq(Info), eq(new String[0]), any())) .thenReturn(testResponse); @@ -576,9 +576,9 @@ public void info_with_multiple_InfoOptions_returns_success() { // setup String[] arguments = new String[] {InfoOptions.Section.ALL.toString(), InfoOptions.Section.DEFAULT.toString()}; - CompletableFuture testResponse = mock(CompletableFuture.class); String testPayload = "Key: Value"; - when(testResponse.get()).thenReturn(testPayload); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(testPayload); when(commandManager.submitNewCommand(eq(Info), eq(arguments), any())) .thenReturn(testResponse); @@ -600,9 +600,9 @@ public void info_with_multiple_InfoOptions_returns_success() { @Test public void info_with_empty_InfoOptions_returns_success() { // setup - CompletableFuture testResponse = mock(CompletableFuture.class); String testPayload = "Key: Value"; - when(testResponse.get()).thenReturn(testPayload); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(testPayload); when(commandManager.submitNewCommand(eq(Info), eq(new String[0]), any())) .thenReturn(testResponse); @@ -622,11 +622,11 @@ public void mget_returns_success() { String[] keys = {"key1", null, "key2"}; String[] values = {"value1", null, "value2"}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(values); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(values); // match on protobuf request - when(commandManager.submitNewCommand(eq(MGet), eq(keys), any())) + when(commandManager.submitNewCommand(eq(MGet), eq(keys), any())) .thenReturn(testResponse); // exercise @@ -647,8 +647,8 @@ public void mset_returns_success() { keyValueMap.put("key2", "value2"); String[] args = {"key1", "value1", "key2", "value2"}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(OK); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(OK); // match on protobuf request when(commandManager.submitNewCommand(eq(MSet), eq(args), any())) @@ -670,11 +670,11 @@ public void incr_returns_success() { String key = "testKey"; Long value = 10L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request - when(commandManager.submitNewCommand(eq(Incr), eq(new String[] {key}), any())) + when(commandManager.submitNewCommand(eq(Incr), eq(new String[] {key}), any())) .thenReturn(testResponse); // exercise @@ -694,11 +694,11 @@ public void incrBy_returns_success() { long amount = 1L; Long value = 10L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request - when(commandManager.submitNewCommand( + when(commandManager.submitNewCommand( eq(IncrBy), eq(new String[] {key, Long.toString(amount)}), any())) .thenReturn(testResponse); @@ -719,11 +719,11 @@ public void incrByFloat_returns_success() { double amount = 1.1; Double value = 10.1; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request - when(commandManager.submitNewCommand( + when(commandManager.submitNewCommand( eq(IncrByFloat), eq(new String[] {key, Double.toString(amount)}), any())) .thenReturn(testResponse); @@ -743,11 +743,11 @@ public void decr_returns_success() { String key = "testKey"; Long value = 10L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request - when(commandManager.submitNewCommand(eq(Decr), eq(new String[] {key}), any())) + when(commandManager.submitNewCommand(eq(Decr), eq(new String[] {key}), any())) .thenReturn(testResponse); // exercise @@ -767,11 +767,11 @@ public void decrBy_returns_success() { long amount = 1L; Long value = 10L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request - when(commandManager.submitNewCommand( + when(commandManager.submitNewCommand( eq(DecrBy), eq(new String[] {key, Long.toString(amount)}), any())) .thenReturn(testResponse); @@ -793,8 +793,8 @@ public void hget_success() { String[] args = new String[] {key, field}; String value = "value"; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); when(commandManager.submitNewCommand(eq(HashGet), eq(args), any())) .thenReturn(testResponse); @@ -818,8 +818,8 @@ public void hset_success() { String[] args = new String[] {key, "field1", "value1", "field2", "value2"}; Long value = 2L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); when(commandManager.submitNewCommand(eq(HashSet), eq(args), any())) .thenReturn(testResponse); @@ -841,8 +841,8 @@ public void hdel_success() { String[] args = {key, "testField1", "testField2"}; Long value = 2L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); when(commandManager.submitNewCommand(eq(HashDel), eq(args), any())) .thenReturn(testResponse); @@ -864,8 +864,8 @@ public void hmget_success() { String[] args = {"testKey", "testField1", "testField2"}; String[] value = {"testValue1", "testValue2"}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(HashMGet), eq(args), any())) @@ -889,8 +889,8 @@ public void hexists_success() { String[] args = new String[] {key, field}; Boolean value = true; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(HashExists), eq(args), any())) @@ -915,8 +915,8 @@ public void hgetall_success() { value.put("key1", "field1"); value.put("key2", "field2"); - CompletableFuture> testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture> testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.>submitNewCommand(eq(HashGetAll), eq(args), any())) @@ -940,8 +940,8 @@ public void hincrBy_returns_success() { long amount = 1L; Long value = 10L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand( @@ -966,8 +966,8 @@ public void hincrByFloat_returns_success() { double amount = 1.0; Double value = 10.0; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand( @@ -992,8 +992,8 @@ public void lpush_returns_success() { String[] args = new String[] {key, "value1", "value2"}; Long value = 2L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(LPush), eq(args), any())) @@ -1016,8 +1016,8 @@ public void lpop_returns_success() { String[] args = new String[] {key}; String value = "value"; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(LPop), eq(args), any())) @@ -1041,8 +1041,8 @@ public void lpopCount_returns_success() { String[] args = new String[] {key, Long.toString(count)}; String[] value = new String[] {"value1", "value2"}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(LPop), eq(args), any())) @@ -1067,8 +1067,8 @@ public void lrange_returns_success() { String[] args = new String[] {key, Long.toString(start), Long.toString(end)}; String[] value = new String[] {"value1", "value2"}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(LRange), eq(args), any())) @@ -1092,8 +1092,8 @@ public void ltrim_returns_success() { long end = 2L; String[] args = new String[] {key, Long.toString(end), Long.toString(start)}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(OK); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(OK); // match on protobuf request when(commandManager.submitNewCommand(eq(LTrim), eq(args), any())) @@ -1116,8 +1116,8 @@ public void llen_returns_success() { String[] args = new String[] {key}; long value = 2L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(LLen), eq(args), any())).thenReturn(testResponse); @@ -1141,8 +1141,8 @@ public void lrem_returns_success() { String[] args = new String[] {key, Long.toString(count), element}; long value = 2L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(LRem), eq(args), any())).thenReturn(testResponse); @@ -1165,8 +1165,8 @@ public void rpush_returns_success() { String[] args = new String[] {key, "value1", "value2"}; Long value = 2L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(RPush), eq(args), any())) @@ -1189,8 +1189,8 @@ public void rpop_returns_success() { String value = "value"; String[] args = new String[] {key}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(RPop), eq(args), any())) @@ -1214,8 +1214,8 @@ public void rpopCount_returns_success() { String[] args = new String[] {key, Long.toString(count)}; String[] value = new String[] {"value1", "value2"}; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(RPop), eq(args), any())) @@ -1239,8 +1239,8 @@ public void sadd_returns_success() { String[] arguments = ArrayUtils.addFirst(members, key); Long value = 2L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(SAdd), eq(arguments), any())) @@ -1264,8 +1264,8 @@ public void srem_returns_success() { String[] arguments = ArrayUtils.addFirst(members, key); Long value = 2L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(SRem), eq(arguments), any())) @@ -1287,8 +1287,8 @@ public void smembers_returns_success() { String key = "testKey"; Set value = Set.of("testMember"); - CompletableFuture> testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture> testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.>submitNewCommand(eq(SMembers), eq(new String[] {key}), any())) @@ -1310,8 +1310,8 @@ public void scard_returns_success() { String key = "testKey"; Long value = 2L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(SCard), eq(new String[] {key}), any())) @@ -1338,8 +1338,8 @@ public void zadd_noOptions_returns_success() { String[] arguments = ArrayUtils.addFirst(membersScoresArgs, key); Long value = 2L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(Zadd), eq(arguments), any())) @@ -1373,8 +1373,8 @@ public void zadd_withOptions_returns_success() { arguments = ArrayUtils.addAll(arguments, membersScoresArgs); Long value = 2L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(Zadd), eq(arguments), any())) @@ -1417,8 +1417,8 @@ public void zaddIncr_noOptions_returns_success() { String[] arguments = new String[] {key, "INCR", Double.toString(increment), member}; Double value = 3.0; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(Zadd), eq(arguments), any())) @@ -1452,8 +1452,8 @@ public void zaddIncr_withOptions_returns_success() { new String[] {"INCR", Double.toString(increment), member}); Double value = 3.0; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(Zadd), eq(arguments), any())) @@ -1472,8 +1472,8 @@ public void zaddIncr_withOptions_returns_success() { @Test public void clientId_returns_success() { // setup - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(42L); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(42L); // match on protobuf request when(commandManager.submitNewCommand(eq(ClientId), eq(new String[0]), any())) @@ -1491,8 +1491,8 @@ public void clientId_returns_success() { @Test public void clientGetName_returns_success() { // setup - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn("TEST"); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete("TEST"); // match on protobuf request when(commandManager.submitNewCommand(eq(ClientGetName), eq(new String[0]), any())) @@ -1510,8 +1510,8 @@ public void clientGetName_returns_success() { @Test public void configRewrite_returns_success() { // setup - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(OK); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(OK); // match on protobuf request when(commandManager.submitNewCommand(eq(ConfigRewrite), eq(new String[0]), any())) @@ -1530,8 +1530,8 @@ public void configRewrite_returns_success() { @Test public void configResetStat_returns_success() { // setup - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(OK); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(OK); // match on protobuf request when(commandManager.submitNewCommand(eq(ConfigResetStat), eq(new String[0]), any())) @@ -1550,9 +1550,9 @@ public void configResetStat_returns_success() { @Test public void configGet_returns_success() { // setup - CompletableFuture> testResponse = mock(CompletableFuture.class); Map testPayload = Map.of("timeout", "1000"); - when(testResponse.get()).thenReturn(testPayload); + CompletableFuture> testResponse = new CompletableFuture<>(); + testResponse.complete(testPayload); // match on protobuf request when(commandManager.>submitNewCommand( @@ -1572,8 +1572,8 @@ public void configGet_returns_success() { @Test public void configSet_returns_success() { // setup - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(OK); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(OK); // match on protobuf request when(commandManager.submitNewCommand( @@ -1597,8 +1597,8 @@ public void zrem_returns_success() { String[] arguments = ArrayUtils.addFirst(members, key); Long value = 2L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(Zrem), eq(arguments), any())) @@ -1621,8 +1621,8 @@ public void zcard_returns_success() { String[] arguments = new String[] {key}; Long value = 3L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(value); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(value); // match on protobuf request when(commandManager.submitNewCommand(eq(Zcard), eq(arguments), any())) diff --git a/java/client/src/test/java/glide/api/RedisClusterClientTest.java b/java/client/src/test/java/glide/api/RedisClusterClientTest.java index 7a2de60a17..5884bcedb2 100644 --- a/java/client/src/test/java/glide/api/RedisClusterClientTest.java +++ b/java/client/src/test/java/glide/api/RedisClusterClientTest.java @@ -173,7 +173,7 @@ public void ping_with_message_returns_success() { // setup String message = "RETURN OF THE PONG"; String[] arguments = new String[] {message}; - CompletableFuture testResponse = new CompletableFuture(); + CompletableFuture testResponse = new CompletableFuture<>(); testResponse.complete(message); // match on protobuf request @@ -240,7 +240,7 @@ public void ping_with_message_with_route_returns_success() { public void info_returns_string() { // setup CompletableFuture> testResponse = mock(CompletableFuture.class); - Map testPayload = new HashMap(); + Map testPayload = new HashMap<>(); testPayload.put("addr1", "value1"); testPayload.put("addr2", "value2"); testPayload.put("addr3", "value3"); diff --git a/java/client/src/test/java/glide/connection/ConnectionWithGlideMockTests.java b/java/client/src/test/java/glide/connection/ConnectionWithGlideMockTests.java index 331af6fa39..08235ac1fc 100644 --- a/java/client/src/test/java/glide/connection/ConnectionWithGlideMockTests.java +++ b/java/client/src/test/java/glide/connection/ConnectionWithGlideMockTests.java @@ -169,15 +169,16 @@ public void rethrow_error_on_read_when_malformed_packet_received() { @Test @SneakyThrows public void rethrow_error_if_UDS_channel_closed() { - var client = new TestClient(channelHandler); - stopRustCoreLibMock(); - try { - var exception = - assertThrows(ExecutionException.class, () -> client.customCommand(new String[0]).get()); - assertTrue(exception.getCause() instanceof ClosingException); - } finally { - // restart mock to let other tests pass if this one failed - startRustCoreLibMock(null); + try (var client = new TestClient(channelHandler)) { + stopRustCoreLibMock(); + try { + var exception = + assertThrows(ExecutionException.class, () -> client.customCommand(new String[0]).get()); + assertTrue(exception.getCause() instanceof ClosingException); + } finally { + // restart mock to let other tests pass if this one failed + startRustCoreLibMock(null); + } } } diff --git a/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java b/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java index 74f45779ef..3f8867d7ab 100644 --- a/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java +++ b/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java @@ -15,8 +15,6 @@ public class ThreadPoolResourceAllocatorTest { - ThreadPoolResourceAllocator service; - @BeforeEach public void init() { var threadPoolResource = ThreadPoolResourceAllocator.getOrCreate(() -> null); @@ -30,17 +28,19 @@ public void init() { public void getOrCreate_returns_default_after_repeated_calls() { ThreadPoolResource mockedThreadPoolResource = mock(ThreadPoolResource.class); EventLoopGroup mockedEventLoopGroup = mock(EventLoop.class); + @SuppressWarnings("unchecked") Supplier threadPoolSupplier = mock(Supplier.class); when(mockedThreadPoolResource.getEventLoopGroup()).thenReturn(mockedEventLoopGroup); when(mockedEventLoopGroup.isShuttingDown()).thenReturn(false); when(threadPoolSupplier.get()).thenReturn(mockedThreadPoolResource); - ThreadPoolResource theResource = service.getOrCreate(threadPoolSupplier); + ThreadPoolResource theResource = ThreadPoolResourceAllocator.getOrCreate(threadPoolSupplier); assertEquals(mockedThreadPoolResource, theResource); // Ensure that supplier only is invoked once to set up the shared resource - ThreadPoolResource theSameResource = service.getOrCreate(threadPoolSupplier); + ThreadPoolResource theSameResource = + ThreadPoolResourceAllocator.getOrCreate(threadPoolSupplier); assertEquals(mockedThreadPoolResource, theSameResource); verify(threadPoolSupplier, times(1)).get(); @@ -52,17 +52,20 @@ public void getOrCreate_returns_default_after_repeated_calls() { public void getOrCreate_returns_new_thread_pool_after_shutdown() { ThreadPoolResource mockedThreadPoolResource = mock(ThreadPoolResource.class); EventLoopGroup mockedEventLoopGroup = mock(EventLoop.class); + + @SuppressWarnings("unchecked") Supplier threadPoolSupplier = mock(Supplier.class); when(mockedThreadPoolResource.getEventLoopGroup()).thenReturn(mockedEventLoopGroup); when(mockedEventLoopGroup.isShuttingDown()).thenReturn(true); when(threadPoolSupplier.get()).thenReturn(mockedThreadPoolResource); - ThreadPoolResource theResource = service.getOrCreate(threadPoolSupplier); + ThreadPoolResource theResource = ThreadPoolResourceAllocator.getOrCreate(threadPoolSupplier); assertEquals(mockedThreadPoolResource, theResource); // Ensure that supplier only is invoked once to set up the shared resource - ThreadPoolResource theSameResource = service.getOrCreate(threadPoolSupplier); + ThreadPoolResource theSameResource = + ThreadPoolResourceAllocator.getOrCreate(threadPoolSupplier); assertEquals(mockedThreadPoolResource, theSameResource); verify(threadPoolSupplier, times(2)).get(); diff --git a/java/client/src/test/java/glide/ffi/FfiTest.java b/java/client/src/test/java/glide/ffi/FfiTest.java index c0af584ac0..e8cb136c53 100644 --- a/java/client/src/test/java/glide/ffi/FfiTest.java +++ b/java/client/src/test/java/glide/ffi/FfiTest.java @@ -65,7 +65,7 @@ public void redisValueToJavaValue_Okay() { } @ParameterizedTest - @ValueSource(longs = {0L, 100L, 774L, Integer.MAX_VALUE + 1, Integer.MIN_VALUE - 1}) + @ValueSource(longs = {0L, 100L, 774L, Integer.MAX_VALUE + 1L, Integer.MIN_VALUE - 1L}) public void redisValueToJavaValue_Int(Long input) { long ptr = FfiTest.createLeakedInt(input); Object longValue = RedisValueResolver.valueFromPointer(ptr); diff --git a/java/client/src/test/java/glide/managers/ConnectionManagerTest.java b/java/client/src/test/java/glide/managers/ConnectionManagerTest.java index b04dd5b312..79389fcde1 100644 --- a/java/client/src/test/java/glide/managers/ConnectionManagerTest.java +++ b/java/client/src/test/java/glide/managers/ConnectionManagerTest.java @@ -40,21 +40,21 @@ public class ConnectionManagerTest { ChannelHandler channel; - private static String HOST = "aws.com"; - private static int PORT = 9999; + private static final String HOST = "aws.com"; + private static final int PORT = 9999; - private static String USERNAME = "JohnDoe"; - private static String PASSWORD = "Password1"; + private static final String USERNAME = "JohnDoe"; + private static final String PASSWORD = "Password1"; - private static int NUM_OF_RETRIES = 5; - private static int FACTOR = 10; - private static int EXPONENT_BASE = 50; + private static final int NUM_OF_RETRIES = 5; + private static final int FACTOR = 10; + private static final int EXPONENT_BASE = 50; - private static int DATABASE_ID = 1; + private static final int DATABASE_ID = 1; - private static int REQUEST_TIMEOUT = 3; + private static final int REQUEST_TIMEOUT = 3; - private static String CLIENT_NAME = "ClientName"; + private static final String CLIENT_NAME = "ClientName"; @BeforeEach public void setUp() { diff --git a/java/client/src/test/java/glide/utils/RustCoreMock.java b/java/client/src/test/java/glide/utils/RustCoreMock.java index 8ef787948e..b9bc53bae6 100644 --- a/java/client/src/test/java/glide/utils/RustCoreMock.java +++ b/java/client/src/test/java/glide/utils/RustCoreMock.java @@ -21,6 +21,7 @@ import java.nio.file.Files; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; +import lombok.NonNull; import lombok.RequiredArgsConstructor; import lombok.SneakyThrows; import redis_request.RedisRequestOuterClass.RedisRequest; @@ -67,13 +68,6 @@ public static Response.Builder OK() { } } - public abstract static class GlideMockConnectAll extends GlideMockProtobuf { - @Override - public Response connection(ConnectionRequest request) { - return Response.newBuilder().build(); - } - } - /** Thread pool supplied to Netty to perform all async IO. */ private final EventLoopGroup group; @@ -113,7 +107,7 @@ private RustCoreMock() { new ChannelInitializer() { @Override - protected void initChannel(DomainSocketChannel ch) throws Exception { + protected void initChannel(@NonNull DomainSocketChannel ch) { ch.pipeline() // https://netty.io/4.1/api/io/netty/handler/codec/protobuf/ProtobufEncoder.html .addLast("frameDecoder", new ProtobufVarint32FrameDecoder()) @@ -155,7 +149,8 @@ private class UdsServer extends ChannelInboundHandlerAdapter { private final AtomicBoolean anybodyConnected = new AtomicBoolean(false); @Override - public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { + public void channelRead(@NonNull ChannelHandlerContext ctx, @NonNull Object msg) + throws Exception { var buf = (ByteBuf) msg; var bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); @@ -165,7 +160,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception return; } var handler = (GlideMockProtobuf) messageProcessor; - Response response = null; + Response response; if (!anybodyConnected.get()) { var connection = ConnectionRequest.parseFrom(bytes); response = handler.connection(connection); @@ -180,7 +175,7 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception } @Override - public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { cause.printStackTrace(); ctx.close(); failed.setPlain(true);