Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: deadlock for batch request content once it passes a certain size #1688

Merged
merged 1 commit into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public BatchRequestContent createNewBatchFromFailedRequests (@Nonnull Map<String
*/
@Nonnull
public InputStream getBatchRequestContent() throws IOException {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
JsonWriter writer = new JsonWriter(new OutputStreamWriter(outputStream, StandardCharsets.UTF_8));
writer.beginObject();
writer.name(CoreConstants.BatchRequest.REQUESTS);
Expand All @@ -172,11 +172,9 @@ public InputStream getBatchRequestContent() throws IOException {
writer.endArray();
writer.endObject();
writer.flush();
PipedInputStream in = new PipedInputStream();
try(final PipedOutputStream out = new PipedOutputStream(in)) {
outputStream.writeTo(out);
return in;
}
final ByteArrayInputStream out = new ByteArrayInputStream(outputStream.toByteArray());
outputStream.close();
return out;
}
private static final String AUTHORIZATION_HEADER_KEY = "authorization";
private void writeBatchRequestStep(BatchRequestStep requestStep, JsonWriter writer) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.microsoft.graph.core.CoreConstants;
import com.microsoft.graph.core.content.BatchRequestContent;
import com.microsoft.graph.core.models.BatchRequestStep;
import com.microsoft.kiota.HttpMethod;
import com.microsoft.kiota.RequestInformation;
import com.microsoft.kiota.authentication.AnonymousAuthenticationProvider;
import okhttp3.MediaType;
Expand All @@ -12,9 +13,16 @@
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.ByteArrayInputStream;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

class BatchRequestBuilderTest {

Expand All @@ -36,4 +44,26 @@ void BatchRequestBuilder_DefaultBuilderTest() throws IOException {
assertEquals(client.getRequestAdapter(), batchRequestBuilder.getRequestAdapter());

}
@Test
void BatchContentDoesNotDeadlockOnLargeContent() throws IOException {
final BaseClient client = new BaseClient(new AnonymousAuthenticationProvider(), "https://localhost");
final BatchRequestContent batchRequestContent = new BatchRequestContent(client);
final List<InputStream> streamsToClose = new ArrayList<>();
for (int i = 0; i < 20; i++) {
final RequestInformation requestInformation = new RequestInformation();
requestInformation.httpMethod = HttpMethod.POST;
requestInformation.setUri(URI.create("https://graph.microsoft.com/v1.0/me/"));
final String payload = "{\"displayName\": \"Test\", \"lastName\": \"User\", \"mailNickname\": \"testuser\", \"userPrincipalName\": \"testUser\", \"passwordProfile\": {\"forceChangePasswordNextSignIn\": true, \"password\": \"password\"}, \"accountEnabled\": true}";
final InputStream content = new ByteArrayInputStream(payload.getBytes(StandardCharsets.UTF_8));
streamsToClose.add(content);
requestInformation.setStreamContent(content, CoreConstants.MimeTypeNames.APPLICATION_JSON);
batchRequestContent.addBatchRequestStep(requestInformation);
}
BatchRequestBuilder batchRequestBuilder = new BatchRequestBuilder(client.getRequestAdapter());
RequestInformation requestInformation = batchRequestBuilder.toPostRequestInformation(batchRequestContent);
assertNotNull(requestInformation);
for (final InputStream inputStream : streamsToClose) {
inputStream.close();
}
}
}