Skip to content

Commit 2813d6b

Browse files
committed
Converting spy to mock
1 parent 7d8f193 commit 2813d6b

File tree

1 file changed

+10
-35
lines changed

1 file changed

+10
-35
lines changed

clients/src/test/java/org/apache/kafka/clients/admin/KafkaAdminClientTest.java

Lines changed: 10 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,6 @@
325325
import static org.mockito.Mockito.doThrow;
326326
import static org.mockito.Mockito.mock;
327327
import static org.mockito.Mockito.mockStatic;
328-
import static org.mockito.Mockito.spy;
329328
import static org.mockito.Mockito.when;
330329

331330
/**
@@ -11802,29 +11801,16 @@ public void testOutOfMemoryErrorPropagation() throws Exception {
1180211801
AdminClientConfig.RETRY_BACKOFF_MS_CONFIG, "100")) {
1180311802
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
1180411803

11805-
// Create a spy of MetadataResponse that throws OutOfMemoryError when topicMetadata() is accessed
11806-
// The AdminClient calls response.topicMetadata() in listTopics handleResponse(), which will trigger the OOM
11807-
MetadataResponseData data = new MetadataResponseData();
11808-
MetadataResponse realResponse = new MetadataResponse(data, ApiKeys.METADATA.latestVersion());
11809-
MetadataResponse spyResponse = spy(realResponse);
11804+
OutOfMemoryError oomError = new OutOfMemoryError("Simulated OOM during response handling");
11805+
MetadataResponse mockResponse = mock(MetadataResponse.class);
11806+
doThrow(oomError).when(mockResponse).topicMetadata();
1181011807

11811-
// Configure the spy to throw OutOfMemoryError when topicMetadata() is called
11812-
// This simulates an OOM occurring during response processing
11813-
doThrow(new OutOfMemoryError("Simulated OOM during response handling"))
11814-
.when(spyResponse).topicMetadata();
11815-
11816-
// Prepare the mocked response that will throw OOM
11817-
env.kafkaClient().prepareResponse(spyResponse);
11808+
env.kafkaClient().prepareResponse(mockResponse);
1181811809

1181911810
// Make the listTopics call - this will internally trigger a metadata request
1182011811
ListTopicsResult result = env.adminClient().listTopics(new ListTopicsOptions().timeoutMs(10000));
1182111812

11822-
// The OOM should propagate as-is, not be wrapped in TimeoutException
11823-
// We expect ExecutionException wrapping the OutOfMemoryError (this is standard Future behavior)
11824-
ExecutionException exception = assertThrows(ExecutionException.class, () -> result.names().get());
11825-
assertInstanceOf(OutOfMemoryError.class, exception.getCause(),
11826-
"Expected OutOfMemoryError to be propagated, but got: " + exception.getCause());
11827-
assertEquals("Simulated OOM during response handling", exception.getCause().getMessage());
11813+
TestUtils.assertFutureThrows(OutOfMemoryError.class, result.names());
1182811814
}
1182911815
}
1183011816

@@ -11841,18 +11827,11 @@ public void testOutOfMemoryErrorNotMaskedOnTimeout() throws Exception {
1184111827
AdminClientConfig.REQUEST_TIMEOUT_MS_CONFIG, "1000")) {
1184211828
env.kafkaClient().setNodeApiVersions(NodeApiVersions.create());
1184311829

11844-
// Create a spy that throws OOM when accessed
11845-
MetadataResponseData data = new MetadataResponseData();
11846-
MetadataResponse realResponse = new MetadataResponse(data, ApiKeys.METADATA.latestVersion());
11847-
MetadataResponse spyResponse = spy(realResponse);
11848-
11849-
// Configure spy to throw OOM when topicMetadata() is accessed
11850-
// This is the actual method called by AdminClient during response processing
11851-
doThrow(new OutOfMemoryError("Simulated OOM during response handling with timeout"))
11852-
.when(spyResponse).topicMetadata();
11830+
OutOfMemoryError oomError = new OutOfMemoryError("Simulated OOM during response handling with timeout");
11831+
MetadataResponse mockResponse = mock(MetadataResponse.class);
11832+
doThrow(oomError).when(mockResponse).topicMetadata();
1185311833

11854-
// Prepare the response
11855-
env.kafkaClient().prepareResponse(spyResponse);
11834+
env.kafkaClient().prepareResponse(mockResponse);
1185611835

1185711836
// Make the call with a short timeout
1185811837
ListTopicsResult result = env.adminClient().listTopics(new ListTopicsOptions().timeoutMs(1000));
@@ -11861,11 +11840,7 @@ public void testOutOfMemoryErrorNotMaskedOnTimeout() throws Exception {
1186111840
// the OOM error is still propagated instead of being masked by TimeoutException
1186211841
time.sleep(1500);
1186311842

11864-
// Even with timeout expired, OOM should still propagate as-is, not wrapped in TimeoutException
11865-
ExecutionException exception = assertThrows(ExecutionException.class, () -> result.names().get());
11866-
assertInstanceOf(OutOfMemoryError.class, exception.getCause(),
11867-
"Expected OutOfMemoryError to be propagated even on timeout, but got: " + exception.getCause());
11868-
assertEquals("Simulated OOM during response handling with timeout", exception.getCause().getMessage());
11843+
TestUtils.assertFutureThrows(OutOfMemoryError.class, result.names());
1186911844
}
1187011845
}
1187111846
}

0 commit comments

Comments
 (0)