Search in sources :

Example 6 with SinglePartFinishedException

use of com.linkedin.multipart.exceptions.SinglePartFinishedException in project rest.li by linkedin.

the class TestMIMEReaderR2Error method testMidProcessingR2Error.

//This test will verify that, in the middle of normal processing, we are able to handle R2
//errors gracefully. We simulate a pause in the middle of normal processing by counting down the latch
//in the callbacks in the middle of the 2nd part.
@Test
public void testMidProcessingR2Error() throws Exception {
    MimeMultipart multiPartMimeBody = new MimeMultipart();
    multiPartMimeBody.addBodyPart(LARGE_DATA_SOURCE);
    multiPartMimeBody.addBodyPart(LARGE_DATA_SOURCE);
    multiPartMimeBody.addBodyPart(LARGE_DATA_SOURCE);
    multiPartMimeBody.addBodyPart(LARGE_DATA_SOURCE);
    multiPartMimeBody.addBodyPart(LARGE_DATA_SOURCE);
    multiPartMimeBody.addBodyPart(LARGE_DATA_SOURCE);
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    multiPartMimeBody.writeTo(byteArrayOutputStream);
    final ByteString requestPayload = ByteString.copy(byteArrayOutputStream.toByteArray());
    CountDownLatch countDownLatch = executeRequestPartialReadWithException(requestPayload, 1, multiPartMimeBody.getContentType());
    countDownLatch.await(_testTimeout, TimeUnit.MILLISECONDS);
    //When this returns, its partially complete
    //In this point in time let us simulate an R2 error
    _reader.getR2MultiPartMIMEReader().onError(new NullPointerException());
    Assert.assertTrue(_currentMultiPartMIMEReaderCallback._streamError instanceof NullPointerException);
    try {
        _currentMultiPartMIMEReaderCallback._singlePartMIMEReaderCallbacks.get(0)._singlePartMIMEReader.requestPartData();
        Assert.fail();
    } catch (SinglePartFinishedException singlePartFinishedException) {
    //pass
    }
    Assert.assertEquals(_currentMultiPartMIMEReaderCallback._singlePartMIMEReaderCallbacks.size(), 2);
    Assert.assertNull(_currentMultiPartMIMEReaderCallback._singlePartMIMEReaderCallbacks.get(0)._streamError);
    Assert.assertTrue(_currentMultiPartMIMEReaderCallback._singlePartMIMEReaderCallbacks.get(1)._streamError instanceof NullPointerException);
    try {
        _currentMultiPartMIMEReaderCallback._singlePartMIMEReaderCallbacks.get(1)._singlePartMIMEReader.requestPartData();
        Assert.fail();
    } catch (SinglePartFinishedException singlePartFinishedException) {
    //pass
    }
}
Also used : MimeMultipart(javax.mail.internet.MimeMultipart) ByteString(com.linkedin.data.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) SinglePartFinishedException(com.linkedin.multipart.exceptions.SinglePartFinishedException) Test(org.testng.annotations.Test)

Example 7 with SinglePartFinishedException

use of com.linkedin.multipart.exceptions.SinglePartFinishedException in project rest.li by linkedin.

the class TestMIMEReaderClientCallbackExceptions method testMultiPartMIMEReaderCallbackExceptionOnFinished.

@Test(dataProvider = "allTypesOfBodiesDataSource")
public void testMultiPartMIMEReaderCallbackExceptionOnFinished(final int chunkSize, final List<MimeBodyPart> bodyPartList) throws Exception {
    MimeMultipart multiPartMimeBody = new MimeMultipart();
    //Add your body parts
    for (final MimeBodyPart bodyPart : bodyPartList) {
        multiPartMimeBody.addBodyPart(bodyPart);
    }
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    multiPartMimeBody.writeTo(byteArrayOutputStream);
    final ByteString requestPayload = ByteString.copy(byteArrayOutputStream.toByteArray());
    final CountDownLatch countDownLatch = executeRequestPartialReadWithException(requestPayload, chunkSize, multiPartMimeBody.getContentType(), MultiPartMIMEThrowOnFlag.THROW_ON_FINISHED, SinglePartMIMEThrowOnFlag.NO_THROW);
    countDownLatch.await(_testTimeout, TimeUnit.MILLISECONDS);
    Assert.assertTrue(_currentMultiPartMIMEReaderCallback.getStreamError() instanceof IllegalMonitorStateException);
    //Verify this is unusable.
    try {
        _currentMultiPartMIMEReaderCallback.getReader().drainAllParts();
        Assert.fail();
    } catch (MultiPartReaderFinishedException multiPartReaderFinishedException) {
    //pass
    }
    Assert.assertEquals(_currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().size(), 6);
    //callback threw
    for (int i = 0; i < _currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().size(); i++) {
        Assert.assertNull(_currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().get(i).getStreamError());
        //Verify this is unusable.
        try {
            _currentMultiPartMIMEReaderCallback.getSinglePartMIMEReaderCallbacks().get(i).getSinglePartMIMEReader().requestPartData();
            Assert.fail();
        } catch (SinglePartFinishedException singlePartFinishedException) {
        //pass
        }
    }
}
Also used : MultiPartReaderFinishedException(com.linkedin.multipart.exceptions.MultiPartReaderFinishedException) MimeMultipart(javax.mail.internet.MimeMultipart) ByteString(com.linkedin.data.ByteString) ByteArrayOutputStream(java.io.ByteArrayOutputStream) MimeBodyPart(javax.mail.internet.MimeBodyPart) CountDownLatch(java.util.concurrent.CountDownLatch) SinglePartFinishedException(com.linkedin.multipart.exceptions.SinglePartFinishedException) Test(org.testng.annotations.Test)

Aggregations

SinglePartFinishedException (com.linkedin.multipart.exceptions.SinglePartFinishedException)7 Test (org.testng.annotations.Test)7 ByteString (com.linkedin.data.ByteString)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 MimeMultipart (javax.mail.internet.MimeMultipart)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 MimeBodyPart (javax.mail.internet.MimeBodyPart)5 MultiPartReaderFinishedException (com.linkedin.multipart.exceptions.MultiPartReaderFinishedException)4 MultiPartIllegalFormatException (com.linkedin.multipart.exceptions.MultiPartIllegalFormatException)1 StreamBusyException (com.linkedin.multipart.exceptions.StreamBusyException)1 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)1 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)1 HashMap (java.util.HashMap)1 BodyPart (javax.mail.BodyPart)1 Header (javax.mail.Header)1