Skip to content

Commit

Permalink
Java: fix type assertions (valkey-io#2637)
Browse files Browse the repository at this point in the history
fix type assertions

Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand authored Dec 10, 2024
1 parent 891f514 commit 085964c
Show file tree
Hide file tree
Showing 12 changed files with 265 additions and 243 deletions.
19 changes: 10 additions & 9 deletions java/client/src/test/java/glide/ExceptionHandlingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static glide.ffi.resolvers.SocketListenerResolver.getSocket;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mockStatic;
Expand Down Expand Up @@ -94,7 +95,7 @@ public void channel_is_closed_when_failed_to_connect() {
var exception = assertThrows(ExecutionException.class, future::get);
// a ClosingException thrown from CallbackDispatcher::completeRequest and then
// rethrown by ConnectionManager::exceptionHandler
assertTrue(exception.getCause() instanceof ClosingException);
assertInstanceOf(ClosingException.class, exception.getCause());
assertTrue(channelHandler.wasClosed);
}

Expand All @@ -110,7 +111,7 @@ public void channel_is_closed_when_disconnected_on_command() {
var exception = assertThrows(ExecutionException.class, future::get);
// a ClosingException thrown from CallbackDispatcher::completeRequest and then
// rethrown by CommandManager::exceptionHandler
assertTrue(exception.getCause() instanceof ClosingException);
assertInstanceOf(ClosingException.class, exception.getCause());
// check the channel
assertTrue(channelHandler.wasClosed);
}
Expand All @@ -127,7 +128,7 @@ public void channel_is_not_closed_when_error_was_in_command_pipeline() {
var exception = assertThrows(ExecutionException.class, future::get);
// a RequestException thrown from CallbackDispatcher::completeRequest and then
// rethrown by CommandManager::exceptionHandler
assertTrue(exception.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, exception.getCause());
// check the channel
assertFalse(channelHandler.wasClosed);
}
Expand All @@ -144,8 +145,8 @@ public void command_manager_rethrows_non_GlideException_too() {
var exception = assertThrows(ExecutionException.class, future::get);
// a IOException thrown from CallbackDispatcher::completeRequest and then wrapped
// by a RuntimeException and rethrown by CommandManager::exceptionHandler
assertTrue(exception.getCause() instanceof RuntimeException);
assertTrue(exception.getCause().getCause() instanceof IOException);
assertInstanceOf(RuntimeException.class, exception.getCause());
assertInstanceOf(IOException.class, exception.getCause().getCause());
// check the channel
assertFalse(channelHandler.wasClosed);
}
Expand All @@ -163,8 +164,8 @@ public void connection_manager_rethrows_non_GlideException_too() {
var exception = assertThrows(ExecutionException.class, future::get);
// a IOException thrown from CallbackDispatcher::completeRequest and then wrapped
// by a RuntimeException and rethrown by ConnectionManager::exceptionHandler
assertTrue(exception.getCause() instanceof RuntimeException);
assertTrue(exception.getCause().getCause() instanceof IOException);
assertInstanceOf(RuntimeException.class, exception.getCause());
assertInstanceOf(IOException.class, exception.getCause().getCause());
// check the channel
assertTrue(channelHandler.wasClosed);
}
Expand All @@ -186,7 +187,7 @@ public void close_connection_on_response_with_closing_error() {
var exception = assertThrows(ExecutionException.class, future1::get);
// a ClosingException thrown from CallbackDispatcher::completeRequest and then
// rethrown by CommandManager::exceptionHandler
assertTrue(exception.getCause() instanceof ClosingException);
assertInstanceOf(ClosingException.class, exception.getCause());
// check the channel
assertTrue(channelHandler.wasClosed);

Expand Down Expand Up @@ -254,7 +255,7 @@ public void close_connection_on_response_without_error_but_with_incorrect_callba
var exception = assertThrows(ExecutionException.class, future1::get);
// a ClosingException thrown from CallbackDispatcher::completeRequest and then
// rethrown by CommandManager::exceptionHandler
assertTrue(exception.getCause() instanceof ClosingException);
assertInstanceOf(ClosingException.class, exception.getCause());
assertEquals(
exception.getCause().getMessage(), "Client is in an erroneous state and should close");
// check the channel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -150,7 +151,7 @@ public Response.Builder commandRequest(CommandRequest request) {

var exception = assertThrows(ExecutionException.class, () -> testConnection().get(1, SECONDS));
assertAll(
() -> assertTrue(exception.getCause() instanceof ClosingException),
() -> assertInstanceOf(ClosingException.class, exception.getCause()),
() -> assertEquals("You shall not pass!", exception.getCause().getMessage()));
}

Expand All @@ -161,7 +162,7 @@ public void rethrow_error_on_read_when_malformed_packet_received() {

var exception = assertThrows(ExecutionException.class, () -> testConnection().get(1, SECONDS));
assertAll(
() -> assertTrue(exception.getCause() instanceof ClosingException),
() -> assertInstanceOf(ClosingException.class, exception.getCause()),
() ->
assertTrue(
exception
Expand All @@ -178,7 +179,7 @@ public void rethrow_error_if_UDS_channel_closed() {
try {
var exception =
assertThrows(ExecutionException.class, () -> client.customCommand(new String[0]).get());
assertTrue(exception.getCause() instanceof ClosingException);
assertInstanceOf(ClosingException.class, exception.getCause());
} finally {
// restart mock to let other tests pass if this one failed
startRustCoreLibMock(null);
Expand Down
9 changes: 5 additions & 4 deletions java/client/src/test/java/glide/ffi/FfiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -84,7 +85,7 @@ public void respValueToJavaValue_Okay() {
public void respValueToJavaValue_Int(Long input) {
long ptr = FfiTest.createLeakedInt(input);
Object longValue = GlideValueResolver.valueFromPointer(ptr);
assertTrue(longValue instanceof Long);
assertInstanceOf(Long.class, longValue);
assertEquals(input, longValue);
}

Expand All @@ -109,7 +110,7 @@ public void respValueToJavaValue_Array() {
long[] array = {1L, 2L, 3L};
long ptr = FfiTest.createLeakedLongArray(array);
Object longArrayValue = GlideValueResolver.valueFromPointer(ptr);
assertTrue(longArrayValue instanceof Object[]);
assertInstanceOf(Object[].class, longArrayValue);
Object[] result = (Object[]) longArrayValue;
assertArrayEquals(new Object[] {1L, 2L, 3L}, result);
}
Expand All @@ -120,7 +121,7 @@ public void respValueToJavaValue_Map() {
long[] values = {1L, 2L, 3L};
long ptr = FfiTest.createLeakedMap(keys, values);
Object mapValue = GlideValueResolver.valueFromPointer(ptr);
assertTrue(mapValue instanceof HashMap<?, ?>);
assertInstanceOf(HashMap.class, mapValue);
HashMap<?, ?> result = (HashMap<?, ?>) mapValue;
assertAll(
() -> assertEquals(1L, result.get(12L)),
Expand Down Expand Up @@ -156,7 +157,7 @@ public void respValueToJavaValue_Set() {
long[] array = {1L, 2L, 2L};
long ptr = FfiTest.createLeakedLongSet(array);
Object longSetValue = GlideValueResolver.valueFromPointer(ptr);
assertTrue(longSetValue instanceof HashSet<?>);
assertInstanceOf(HashSet.class, longSetValue);
HashSet<?> result = (HashSet<?>) longSetValue;
assertAll(
() -> assertTrue(result.contains(1L)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -131,7 +132,7 @@ public void submitNewCommand_return_String_result() {
Object respPointer = result.get();

// verify
assertTrue(respPointer instanceof String);
assertInstanceOf(String.class, respPointer);
assertEquals(testString, respPointer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import static glide.api.models.configuration.StandaloneSubscriptionConfiguration.PubSubChannelMode.EXACT;
import static glide.api.models.configuration.StandaloneSubscriptionConfiguration.PubSubChannelMode.PATTERN;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -245,7 +245,7 @@ public void connection_on_empty_response_throws_ClosingException() {
ExecutionException.class,
() -> connectionManager.connectToValkey(glideClientConfiguration).get());

assertTrue(executionException.getCause() instanceof ClosingException);
assertInstanceOf(ClosingException.class, executionException.getCause());
assertEquals("Unexpected empty data in response", executionException.getCause().getMessage());
verify(channel).close();
}
Expand All @@ -265,7 +265,7 @@ public void connection_on_resp_pointer_throws_ClosingException() {
ExecutionException.class,
() -> connectionManager.connectToValkey(glideClientConfiguration).get());

assertTrue(executionException.getCause() instanceof ClosingException);
assertInstanceOf(ClosingException.class, executionException.getCause());
assertEquals("Unexpected data in response", executionException.getCause().getMessage());
verify(channel).close();
}
Expand Down
7 changes: 4 additions & 3 deletions java/integTest/src/test/java/glide/ErrorHandlingTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import static glide.TestUtilities.commonClientConfig;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -33,7 +34,7 @@ public void basic_client_tries_to_connect_to_wrong_address() {
.build())
.get());
assertAll(
() -> assertTrue(exception.getCause() instanceof ClosingException),
() -> assertInstanceOf(ClosingException.class, exception.getCause()),
() -> assertTrue(exception.getCause().getMessage().contains("Connection refused")));
}

Expand All @@ -46,7 +47,7 @@ public void basic_client_tries_wrong_command() {
ExecutionException.class,
() -> regularClient.customCommand(new String[] {"pewpew"}).get());
assertAll(
() -> assertTrue(exception.getCause() instanceof RequestException),
() -> assertInstanceOf(RequestException.class, exception.getCause()),
() -> assertTrue(exception.getCause().getMessage().contains("unknown command")));
}
}
Expand All @@ -60,7 +61,7 @@ public void basic_client_tries_wrong_command_arguments() {
ExecutionException.class,
() -> regularClient.customCommand(new String[] {"ping", "pang", "pong"}).get());
assertAll(
() -> assertTrue(exception.getCause() instanceof RequestException),
() -> assertInstanceOf(RequestException.class, exception.getCause()),
() ->
assertTrue(exception.getCause().getMessage().contains("wrong number of arguments")));
}
Expand Down
3 changes: 2 additions & 1 deletion java/integTest/src/test/java/glide/SharedClientTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import static glide.api.BaseClient.OK;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

Expand Down Expand Up @@ -173,7 +174,7 @@ public void inflight_requests_limit(boolean clusterMode, int inflightRequestsLim
responses.get(inflightRequestsLimit).get(100, TimeUnit.MILLISECONDS);
fail("Expected the last request to throw an exception");
} catch (ExecutionException e) {
assertTrue(e.getCause() instanceof RequestException);
assertInstanceOf(RequestException.class, e.getCause());
assertTrue(e.getCause().getMessage().contains("maximum inflight requests"));
}

Expand Down
Loading

0 comments on commit 085964c

Please sign in to comment.