diff --git a/java/client/src/test/java/glide/api/RedisClientTest.java b/java/client/src/test/java/glide/api/RedisClientTest.java index 15deb678cd..76c03d123e 100644 --- a/java/client/src/test/java/glide/api/RedisClientTest.java +++ b/java/client/src/test/java/glide/api/RedisClientTest.java @@ -156,8 +156,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())) @@ -198,8 +198,8 @@ public void echo_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())) @@ -240,9 +240,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( @@ -264,8 +264,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 @@ -283,8 +283,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())) @@ -305,8 +305,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); @@ -325,9 +325,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 @@ -353,8 +354,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); @@ -382,8 +383,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); @@ -401,8 +402,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); @@ -423,8 +424,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())) @@ -446,8 +447,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())) @@ -469,8 +470,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())) @@ -492,8 +493,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())) @@ -516,8 +517,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())) @@ -539,8 +540,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())) @@ -563,8 +564,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())) @@ -586,8 +587,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())) @@ -608,9 +609,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())) @@ -637,7 +637,7 @@ public void invokeScript_returns_success() { testResponse.complete(payload); // match on protobuf request - when(commandManager.submitScript(eq(script), eq(List.of()), eq(List.of()), any())) + when(commandManager.submitScript(eq(script), eq(List.of()), eq(List.of()), any())) .thenReturn(testResponse); // exercise @@ -664,7 +664,7 @@ public void invokeScript_with_ScriptOptions_returns_success() { testResponse.complete(payload); // match on protobuf request - when(commandManager.submitScript( + when(commandManager.submitScript( eq(script), eq(List.of("key1", "key2")), eq(List.of("arg1", "arg2")), any())) .thenReturn(testResponse); @@ -683,8 +683,8 @@ public void pttl_returns_success() { String key = "testKey"; long pttl = 999000L; - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(pttl); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(pttl); // match on protobuf request when(commandManager.submitNewCommand(eq(PTTL), eq(new String[] {key}), any())) @@ -724,9 +724,9 @@ public void persist_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); @@ -745,9 +745,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); @@ -769,9 +769,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); @@ -791,11 +791,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 @@ -816,8 +816,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())) @@ -839,11 +839,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 @@ -863,11 +863,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); @@ -888,11 +888,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); @@ -912,8 +912,8 @@ 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())) @@ -936,8 +936,8 @@ 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( @@ -960,8 +960,8 @@ public void strlen_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(Strlen), eq(new String[] {key}), any())) @@ -985,8 +985,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); @@ -1010,8 +1010,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); @@ -1058,8 +1058,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); // match on protobuf request when(commandManager.submitNewCommand(eq(HashDel), eq(args), any())) @@ -1130,8 +1130,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())) @@ -1155,8 +1155,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())) @@ -1181,8 +1181,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())) @@ -1206,8 +1206,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( @@ -1232,8 +1232,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( @@ -1258,8 +1258,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())) @@ -1282,8 +1282,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())) @@ -1307,8 +1307,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())) @@ -1333,8 +1333,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())) @@ -1383,8 +1383,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())) @@ -1407,8 +1407,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); @@ -1432,8 +1432,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); @@ -1456,8 +1456,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())) @@ -1480,8 +1480,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())) @@ -1505,8 +1505,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())) @@ -1530,8 +1530,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())) @@ -1579,8 +1579,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())) @@ -1602,8 +1602,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())) @@ -1625,8 +1625,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())) @@ -1653,8 +1653,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())) @@ -1688,8 +1688,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())) @@ -1732,8 +1732,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())) @@ -1767,8 +1767,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())) @@ -1787,8 +1787,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())) @@ -1806,8 +1806,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())) @@ -1825,8 +1825,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())) @@ -1845,8 +1845,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())) @@ -1865,9 +1865,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( @@ -1887,8 +1887,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( @@ -1912,8 +1912,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())) @@ -1936,8 +1936,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())) @@ -2107,7 +2107,6 @@ public void zrange_by_score_with_reverse_returns_success() { String key = "testKey"; RangeByScore rangeByScore = new RangeByScore(new ScoreBoundary(3, false), InfScoreBound.NEGATIVE_INFINITY); - boolean reversed = true; String[] arguments = new String[] {key, rangeByScore.getStart(), rangeByScore.getEnd(), "BYSCORE", "REV"}; String[] value = new String[] {"two", "one"}; @@ -2448,8 +2447,8 @@ public void type_returns_success() { String[] arguments = new String[] {key}; String value = "none"; - 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(Type), eq(arguments), any())) @@ -2468,9 +2467,9 @@ public void type_returns_success() { @Test public void time_returns_success() { // setup - CompletableFuture testResponse = mock(CompletableFuture.class); + CompletableFuture testResponse = new CompletableFuture<>(); String[] payload = new String[] {"UnixTime", "ms"}; - when(testResponse.get()).thenReturn(payload); + testResponse.complete(payload); // match on protobuf request when(commandManager.submitNewCommand(eq(Time), eq(new String[0]), any())) diff --git a/java/client/src/test/java/glide/api/RedisClusterClientTest.java b/java/client/src/test/java/glide/api/RedisClusterClientTest.java index fde9adc3ac..0a92568d6a 100644 --- a/java/client/src/test/java/glide/api/RedisClusterClientTest.java +++ b/java/client/src/test/java/glide/api/RedisClusterClientTest.java @@ -39,7 +39,6 @@ import response.ResponseOuterClass.ConstantResponse; import response.ResponseOuterClass.Response; -@SuppressWarnings("unchecked,resource") public class RedisClusterClientTest { RedisClusterClient service; @@ -62,10 +61,10 @@ public void setUp() { public void custom_command_returns_single_value() { var commandManager = new TestCommandManager(null); - var client = new TestClient(commandManager, "TEST"); - - var value = client.customCommand(TEST_ARGS).get(); - assertEquals("TEST", value.getSingleValue()); + try (var client = new TestClient(commandManager, "TEST")) { + var value = client.customCommand(TEST_ARGS).get(); + assertEquals("TEST", value.getSingleValue()); + } } @Test @@ -74,10 +73,10 @@ public void custom_command_returns_multi_value() { var commandManager = new TestCommandManager(null); var data = Map.of("key1", "value1", "key2", "value2"); - var client = new TestClient(commandManager, data); - - var value = client.customCommand(TEST_ARGS).get(); - assertEquals(data, value.getMultiValue()); + try (var client = new TestClient(commandManager, data)) { + var value = client.customCommand(TEST_ARGS).get(); + assertEquals(data, value.getMultiValue()); + } } @Test @@ -87,10 +86,10 @@ public void custom_command_with_single_node_route_returns_single_value() { var commandManager = new TestCommandManager(null); var data = Map.of("key1", "value1", "key2", "value2"); - var client = new TestClient(commandManager, data); - - var value = client.customCommand(TEST_ARGS, RANDOM).get(); - assertEquals(data, value.getSingleValue()); + try (var client = new TestClient(commandManager, data)) { + var value = client.customCommand(TEST_ARGS, RANDOM).get(); + assertEquals(data, value.getSingleValue()); + } } @Test @@ -99,10 +98,10 @@ public void custom_command_with_multi_node_route_returns_multi_value() { var commandManager = new TestCommandManager(null); var data = Map.of("key1", "value1", "key2", "value2"); - var client = new TestClient(commandManager, data); - - var value = client.customCommand(TEST_ARGS, ALL_NODES).get(); - assertEquals(data, value.getMultiValue()); + try (var client = new TestClient(commandManager, data)) { + var value = client.customCommand(TEST_ARGS, ALL_NODES).get(); + assertEquals(data, value.getMultiValue()); + } } @Test @@ -112,10 +111,10 @@ public void custom_command_returns_single_value_on_constant_response() { new TestCommandManager( Response.newBuilder().setConstantResponse(ConstantResponse.OK).build()); - var client = new TestClient(commandManager, "OK"); - - var value = client.customCommand(TEST_ARGS, ALL_NODES).get(); - assertEquals("OK", value.getSingleValue()); + try (var client = new TestClient(commandManager, "OK")) { + var value = client.customCommand(TEST_ARGS, ALL_NODES).get(); + assertEquals("OK", value.getSingleValue()); + } } private static class TestClient extends RedisClusterClient { @@ -129,8 +128,13 @@ public TestClient(CommandManager commandManager, Object objectToReturn) { @Override protected T handleRedisResponse(Class classType, boolean isNullable, Response response) { - return (T) object; + @SuppressWarnings("unchecked") + T returnValue = (T) object; + return returnValue; } + + @Override + public void close() {} } private static class TestCommandManager extends CommandManager { @@ -153,8 +157,8 @@ public CompletableFuture submitCommandToChannel( @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())) @@ -175,7 +179,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 @@ -195,8 +199,8 @@ public void ping_with_message_returns_success() { @Test public void ping_with_route_returns_success() { // setup - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn("PONG"); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete("PONG"); Route route = ALL_NODES; @@ -286,12 +290,12 @@ public void echo_with_route_returns_success() { @Test 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"); - when(testResponse.get()).thenReturn(ClusterValue.of(testPayload)); + CompletableFuture> testResponse = new CompletableFuture<>(); + testResponse.complete(ClusterValue.of(testPayload)); when(commandManager.>submitNewCommand(eq(Info), eq(new String[0]), any())) .thenReturn(testResponse); @@ -309,10 +313,10 @@ public void info_returns_string() { @Test public void info_with_route_returns_string() { // setup - CompletableFuture> testResponse = mock(CompletableFuture.class); Map testClusterValue = Map.of("addr1", "addr1 result", "addr2", "addr2 result"); Route route = ALL_NODES; - when(testResponse.get()).thenReturn(ClusterValue.of(testClusterValue)); + CompletableFuture> testResponse = new CompletableFuture<>(); + testResponse.complete(ClusterValue.of(testClusterValue)); when(commandManager.>submitNewCommand( eq(Info), eq(new String[0]), eq(route), any())) .thenReturn(testResponse); @@ -333,9 +337,10 @@ public void info_with_route_returns_string() { public void info_with_route_with_infoOptions_returns_string() { // setup String[] infoArguments = new String[] {"ALL", "DEFAULT"}; - CompletableFuture> testResponse = mock(CompletableFuture.class); Map testClusterValue = Map.of("addr1", "addr1 result", "addr2", "addr2 result"); - when(testResponse.get()).thenReturn(ClusterValue.of(testClusterValue)); + CompletableFuture> testResponse = new CompletableFuture<>(); + testResponse.complete(ClusterValue.of(testClusterValue)); + Route route = ALL_PRIMARIES; when(commandManager.>submitNewCommand( eq(Info), eq(infoArguments), eq(route), any())) @@ -364,11 +369,12 @@ public void info_with_single_node_route_returns_single_value() { var commandManager = new TestCommandManager(null); var data = "info string"; - var client = new TestClient(commandManager, data); - - var value = client.info(RANDOM).get(); - assertAll( - () -> assertTrue(value.hasSingleData()), () -> assertEquals(data, value.getSingleValue())); + try (var client = new TestClient(commandManager, data)) { + var value = client.info(RANDOM).get(); + assertAll( + () -> assertTrue(value.hasSingleData()), + () -> assertEquals(data, value.getSingleValue())); + } } @Test @@ -377,11 +383,11 @@ public void info_with_multi_node_route_returns_multi_value() { var commandManager = new TestCommandManager(null); var data = Map.of("key1", "value1", "key2", "value2"); - var client = new TestClient(commandManager, data); - - var value = client.info(ALL_NODES).get(); - assertAll( - () -> assertTrue(value.hasMultiData()), () -> assertEquals(data, value.getMultiValue())); + try (var client = new TestClient(commandManager, data)) { + var value = client.info(ALL_NODES).get(); + assertAll( + () -> assertTrue(value.hasMultiData()), () -> assertEquals(data, value.getMultiValue())); + } } @Test @@ -390,11 +396,12 @@ public void info_with_options_and_single_node_route_returns_single_value() { var commandManager = new TestCommandManager(null); var data = "info string"; - var client = new TestClient(commandManager, data); - - var value = client.info(InfoOptions.builder().build(), RANDOM).get(); - assertAll( - () -> assertTrue(value.hasSingleData()), () -> assertEquals(data, value.getSingleValue())); + try (var client = new TestClient(commandManager, data)) { + var value = client.info(InfoOptions.builder().build(), RANDOM).get(); + assertAll( + () -> assertTrue(value.hasSingleData()), + () -> assertEquals(data, value.getSingleValue())); + } } @Test @@ -403,19 +410,19 @@ public void info_with_options_and_multi_node_route_returns_multi_value() { var commandManager = new TestCommandManager(null); var data = Map.of("key1", "value1", "key2", "value2"); - var client = new TestClient(commandManager, data); - - var value = client.info(InfoOptions.builder().build(), ALL_NODES).get(); - assertAll( - () -> assertTrue(value.hasMultiData()), () -> assertEquals(data, value.getMultiValue())); + try (var client = new TestClient(commandManager, data)) { + var value = client.info(InfoOptions.builder().build(), ALL_NODES).get(); + assertAll( + () -> assertTrue(value.hasMultiData()), () -> assertEquals(data, value.getMultiValue())); + } } @SneakyThrows @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())) @@ -435,10 +442,11 @@ public void clientId_with_multi_node_route_returns_success() { var commandManager = new TestCommandManager(null); var data = Map.of("n1", 42L); - var client = new TestClient(commandManager, data); + try (var client = new TestClient(commandManager, data)) { + var value = client.clientId(ALL_NODES).get(); - var value = client.clientId(ALL_NODES).get(); - assertEquals(data, value.getMultiValue()); + assertEquals(data, value.getMultiValue()); + } } @Test @@ -446,18 +454,18 @@ public void clientId_with_multi_node_route_returns_success() { public void clientId_with_single_node_route_returns_success() { var commandManager = new TestCommandManager(null); - var client = new TestClient(commandManager, 42L); - - var value = client.clientId(RANDOM).get(); - assertEquals(42, value.getSingleValue()); + try (var client = new TestClient(commandManager, 42L)) { + var value = client.clientId(RANDOM).get(); + assertEquals(42, value.getSingleValue()); + } } @SneakyThrows @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())) @@ -476,10 +484,10 @@ public void clientGetName_returns_success() { public void clientGetName_with_single_node_route_returns_success() { var commandManager = new TestCommandManager(null); - var client = new TestClient(commandManager, "TEST"); - - var value = client.clientGetName(RANDOM).get(); - assertEquals("TEST", value.getSingleValue()); + try (var client = new TestClient(commandManager, "TEST")) { + var value = client.clientGetName(RANDOM).get(); + assertEquals("TEST", value.getSingleValue()); + } } @Test @@ -488,18 +496,18 @@ public void clientGetName_with_multi_node_route_returns_success() { var commandManager = new TestCommandManager(null); var data = Map.of("n1", "TEST"); - var client = new TestClient(commandManager, data); - - var value = client.clientGetName(ALL_NODES).get(); - assertEquals(data, value.getMultiValue()); + try (var client = new TestClient(commandManager, data)) { + var value = client.clientGetName(ALL_NODES).get(); + assertEquals(data, value.getMultiValue()); + } } @SneakyThrows @Test public void configRewrite_without_route_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())) @@ -518,8 +526,8 @@ public void configRewrite_without_route_returns_success() { @Test public void configRewrite_with_route_returns_success() { // setup - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(OK); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(OK); Route route = ALL_NODES; @@ -541,8 +549,8 @@ public void configRewrite_with_route_returns_success() { @Test public void configResetStat_without_route_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())) @@ -561,8 +569,8 @@ public void configResetStat_without_route_returns_success() { @Test public void configResetStat_with_route_returns_success() { // setup - CompletableFuture testResponse = mock(CompletableFuture.class); - when(testResponse.get()).thenReturn(OK); + CompletableFuture testResponse = new CompletableFuture<>(); + testResponse.complete(OK); Route route = ALL_NODES; @@ -585,9 +593,9 @@ public void configResetStat_with_route_returns_success() { @Test public void configGet_returns_success() { // setup - CompletableFuture> testResponse = mock(CompletableFuture.class); var testPayload = Map.of("timeout", "1000"); - when(testResponse.get()).thenReturn(testPayload); + CompletableFuture> testResponse = new CompletableFuture<>(); + testResponse.complete(testPayload); // match on protobuf request when(commandManager.>submitNewCommand( @@ -610,11 +618,12 @@ public void configGet_with_single_node_route_returns_single_value() { var commandManager = new TestCommandManager(null); var data = Map.of("timeout", "1000", "maxmemory", "1GB"); - var client = new TestClient(commandManager, data); - - var value = client.configGet(TEST_ARGS, RANDOM).get(); - assertAll( - () -> assertTrue(value.hasSingleData()), () -> assertEquals(data, value.getSingleValue())); + try (var client = new TestClient(commandManager, data)) { + var value = client.configGet(TEST_ARGS, RANDOM).get(); + assertAll( + () -> assertTrue(value.hasSingleData()), + () -> assertEquals(data, value.getSingleValue())); + } } @Test @@ -623,19 +632,19 @@ public void configGet_with_multi_node_route_returns_multi_value() { var commandManager = new TestCommandManager(null); var data = Map.of("node1", Map.of("timeout", "1000", "maxmemory", "1GB")); - var client = new TestClient(commandManager, data); - - var value = client.configGet(TEST_ARGS, ALL_NODES).get(); - assertAll( - () -> assertTrue(value.hasMultiData()), () -> assertEquals(data, value.getMultiValue())); + try (var client = new TestClient(commandManager, data)) { + var value = client.configGet(TEST_ARGS, ALL_NODES).get(); + assertAll( + () -> assertTrue(value.hasMultiData()), () -> assertEquals(data, value.getMultiValue())); + } } @SneakyThrows @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( @@ -654,8 +663,8 @@ public void configSet_returns_success() { @Test public void configSet_with_route_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( 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..3ce2052582 100644 --- a/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java +++ b/java/client/src/test/java/glide/connectors/resources/ThreadPoolResourceAllocatorTest.java @@ -3,9 +3,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.mockito.internal.verification.VerificationModeFactory.times; import io.netty.channel.EventLoop; import io.netty.channel.EventLoopGroup; @@ -15,8 +13,6 @@ public class ThreadPoolResourceAllocatorTest { - ThreadPoolResourceAllocator service; - @BeforeEach public void init() { var threadPoolResource = ThreadPoolResourceAllocator.getOrCreate(() -> null); @@ -30,19 +26,19 @@ public void init() { public void getOrCreate_returns_default_after_repeated_calls() { ThreadPoolResource mockedThreadPoolResource = mock(ThreadPoolResource.class); EventLoopGroup mockedEventLoopGroup = mock(EventLoop.class); - Supplier threadPoolSupplier = mock(Supplier.class); + + Supplier threadPoolSupplier = () -> mockedThreadPoolResource; 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(); // teardown when(mockedEventLoopGroup.isShuttingDown()).thenReturn(true); @@ -52,19 +48,19 @@ 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); - Supplier threadPoolSupplier = mock(Supplier.class); + + Supplier threadPoolSupplier = () -> mockedThreadPoolResource; 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(); // teardown when(mockedEventLoopGroup.isShuttingDown()).thenReturn(true); diff --git a/java/client/src/test/java/glide/ffi/FfiTest.java b/java/client/src/test/java/glide/ffi/FfiTest.java index c0af584ac0..73c9082c20 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); @@ -98,8 +98,8 @@ public void redisValueToJavaValue_Map() { long[] values = {1L, 2L, 3L}; long ptr = FfiTest.createLeakedMap(keys, values); Object mapValue = RedisValueResolver.valueFromPointer(ptr); - assertTrue(mapValue instanceof HashMap); - HashMap result = (HashMap) mapValue; + assertTrue(mapValue instanceof HashMap); + HashMap result = (HashMap) mapValue; assertAll( () -> assertEquals(1L, result.get(12L)), () -> assertEquals(2L, result.get(14L)), @@ -134,8 +134,8 @@ public void redisValueToJavaValue_Set() { long[] array = {1L, 2L, 2L}; long ptr = FfiTest.createLeakedLongSet(array); Object longSetValue = RedisValueResolver.valueFromPointer(ptr); - assertTrue(longSetValue instanceof HashSet); - HashSet result = (HashSet) longSetValue; + assertTrue(longSetValue instanceof HashSet); + HashSet result = (HashSet) longSetValue; assertAll( () -> assertTrue(result.contains(1L)), () -> assertTrue(result.contains(2L)), 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); diff --git a/java/integTest/src/test/java/glide/SharedClientTests.java b/java/integTest/src/test/java/glide/SharedClientTests.java index fa343db818..e5d11b2546 100644 --- a/java/integTest/src/test/java/glide/SharedClientTests.java +++ b/java/integTest/src/test/java/glide/SharedClientTests.java @@ -88,7 +88,8 @@ private static Stream clientAndDataSize() { @MethodSource("clientAndDataSize") public void client_can_handle_concurrent_workload(BaseClient client, int valueSize) { ExecutorService executorService = Executors.newCachedThreadPool(); - CompletableFuture[] futures = new CompletableFuture[100]; + @SuppressWarnings("unchecked") + CompletableFuture[] futures = new CompletableFuture[100]; for (int i = 0; i < 100; i++) { futures[i] = diff --git a/java/integTest/src/test/java/glide/SharedCommandTests.java b/java/integTest/src/test/java/glide/SharedCommandTests.java index 2477549b72..3017847a45 100644 --- a/java/integTest/src/test/java/glide/SharedCommandTests.java +++ b/java/integTest/src/test/java/glide/SharedCommandTests.java @@ -49,6 +49,7 @@ import lombok.SneakyThrows; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -1127,7 +1128,7 @@ public void zadd_and_zaddIncr_with_NX_XX(BaseClient client) { assertEquals(0, client.zadd(key, membersScores, onlyIfExistsOptions).get()); assertEquals(3, client.zadd(key, membersScores, onlyIfDoesNotExistOptions).get()); - assertEquals(null, client.zaddIncr(key, "one", 5, onlyIfDoesNotExistOptions).get()); + assertNull(client.zaddIncr(key, "one", 5, onlyIfDoesNotExistOptions).get()); assertEquals(6, client.zaddIncr(key, "one", 5, onlyIfExistsOptions).get()); } @@ -1156,25 +1157,24 @@ public void zadd_and_zaddIncr_with_GT_LT(BaseClient client) { assertEquals(1, client.zadd(key, membersScores, scoreGreaterThanOptions, true).get()); assertEquals(0, client.zadd(key, membersScores, scoreLessThanOptions, true).get()); assertEquals(7, client.zaddIncr(key, "one", -3, scoreLessThanOptions).get()); - assertEquals(null, client.zaddIncr(key, "one", -3, scoreGreaterThanOptions).get()); + assertNull(client.zaddIncr(key, "one", -3, scoreGreaterThanOptions).get()); } - @SneakyThrows - @ParameterizedTest - @MethodSource("getClients") - public void zadd_illegal_arguments(BaseClient client) { + // TODO move to another class + @Test + public void zadd_illegal_arguments() { ZaddOptions existsGreaterThanOptions = ZaddOptions.builder() .conditionalChange(ZaddOptions.ConditionalChange.ONLY_IF_DOES_NOT_EXIST) .updateOptions(ZaddOptions.UpdateOptions.SCORE_GREATER_THAN_CURRENT) .build(); - assertThrows(IllegalArgumentException.class, () -> existsGreaterThanOptions.toArgs()); + assertThrows(IllegalArgumentException.class, existsGreaterThanOptions::toArgs); ZaddOptions existsLessThanOptions = ZaddOptions.builder() .conditionalChange(ZaddOptions.ConditionalChange.ONLY_IF_DOES_NOT_EXIST) .updateOptions(ZaddOptions.UpdateOptions.SCORE_LESS_THAN_CURRENT) .build(); - assertThrows(IllegalArgumentException.class, () -> existsLessThanOptions.toArgs()); + assertThrows(IllegalArgumentException.class, existsLessThanOptions::toArgs); ZaddOptions options = ZaddOptions.builder() .conditionalChange(ZaddOptions.ConditionalChange.ONLY_IF_DOES_NOT_EXIST)