Skip to content

Commit

Permalink
fix: deadlock for batch request content once it passes a certain size
Browse files Browse the repository at this point in the history
Signed-off-by: Vincent Biret <[email protected]>
  • Loading branch information
baywet committed Jul 31, 2024
1 parent 1ed68ec commit 3c70728
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 6 deletions.
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();
}
}
}

0 comments on commit 3c70728

Please sign in to comment.