I am trying to figure out how to unit test the callback methods of a FutureCallback<HttpClient>
, but can't quite figure it out.
I have the following code:
val latch = CountDownLatch(10)
httpClient.execute(httpPost, object: FutureCallback<HttpResponse> {
override fun completed(response: HttpResponse) {
val outputStream = ByteArrayOutputStream()
response.entity.writeTo(outputStream)
result = DataObj.parseFrom(outputStream.toByteArray())
} catch (e: IOException) {
failed(e)
} catch (e: InvalidProtocolBufferException) {
e.printStackTrace()
}
}
latch.countDown()
}
override fun failed(e: Exception) {
logger.error(e.localizedMessage, e)
latch.countDown()
}
override fun cancelled() {
logger.error("Request cancelled.")
latch.countDown()
}
})
I'm unsure on how I can force the FutureCallback to excecute either completed, failed, or cancelled. Therefore the latch.countDown()
is never called, meaning the test never ends.
What is the correct method to test this kind of asynchronous function?