I have a handler which is executing a store operation on a database using Hibernate Reactive and then uploading a blob to an azure storage container using the synchronous client.
I read I have to wrap all the blocking calls in an executeBlocking() method, so I did so for when I upload a blob. Only problem is that then my tests stop working because the thread is blocked. The test do work when I don’t wrap the call to the blob storage in the executeBlocking() method.
This is the method I have right now:
public Uni<File> downloadSingle(String blobName) {
return Uni.createFrom().emitter(emitter -> {
try {
Path path = Files.createTempDirectory(UUID.randomUUID().toString());
vertx.executeBlocking(() ->
blobContainerClient.getBlobClient(blobName).downloadToFile(path + "/" + blobName),
false
)
.onComplete(result -> emitter.complete(path.toFile()))
.onFailure(emitter::fail);
} catch (UncheckedIOException | IOException e) {
emitter.fail(new BlobProblem(
new ProblemDetail(
"BLOB_DOWNLOAD_PROBLEM",
"A problem occurred while download a blob.",
Response.Status.INTERNAL_SERVER_ERROR.getStatusCode(),
List.of()
)
));
}
});
}
And this is my test:
@Test
@RunOnVertxContext
void BlobHandlerTest_errorsShouldBeHandledWhenDownloadingABlob() {
when(blobContainerClient.getBlobClient(any())).thenReturn(blobClient);
when(blobClient.downloadToFile(any())).thenThrow(new UncheckedIOException(new IOException()));
blobHandler
.downloadSingle("blob-name")
.subscribe()
.withSubscriber(UniAssertSubscriber.create())
.awaitSubscription()
.assertSubscribed()
.awaitFailure()
.assertFailed()
.assertFailedWith(BlobProblem.class);
}
Does anyone know if I need to do anything different?
PS. I know there’s also an async client for the Azure SDK. Unfortunately I wasn’t able to make that work.