Search in sources :

Example 16 with TransportException

use of com.nextdoor.bender.ipc.TransportException in project bender by Nextdoor.

the class FileTransport method sendBatch.

@Override
public void sendBatch(TransportBuffer buffer) throws TransportException {
    FileTransportBuffer buf = (FileTransportBuffer) buffer;
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(new File(this.filename));
        out.write(buf.getInternalBuffer().toByteArray());
    } catch (IOException e) {
        throw new TransportException("unable to write to file", e);
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
            }
        }
    }
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) TransportException(com.nextdoor.bender.ipc.TransportException)

Example 17 with TransportException

use of com.nextdoor.bender.ipc.TransportException in project bender by Nextdoor.

the class ElasticSearchTransporterTest method testErrorsResponse.

@Test(expected = TransportException.class)
public void testErrorsResponse() throws TransportException, IOException {
    byte[] respPayload = getResponse().getBytes(StandardCharsets.UTF_8);
    HttpClient client = getMockClientWithResponse(respPayload, ContentType.APPLICATION_JSON, HttpStatus.SC_OK);
    ElasticSearchTransport transport = new ElasticSearchTransport(client, false);
    try {
        transport.sendBatch("foo".getBytes());
    } catch (Exception e) {
        assertEquals("es index failure count is 1", e.getCause().getMessage());
        throw e;
    }
}
Also used : HttpClient(org.apache.http.client.HttpClient) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) TransportException(com.nextdoor.bender.ipc.TransportException) Test(org.junit.Test)

Example 18 with TransportException

use of com.nextdoor.bender.ipc.TransportException in project bender by Nextdoor.

the class ElasticSearchTransporterTest method testGzipErrorsResponse.

@Test(expected = TransportException.class)
public void testGzipErrorsResponse() throws TransportException, IOException {
    byte[] respPayload = getResponse().getBytes(StandardCharsets.UTF_8);
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    GZIPOutputStream os = new GZIPOutputStream(baos);
    os.write(respPayload);
    os.close();
    byte[] compressedResponse = baos.toByteArray();
    HttpClient client = getMockClientWithResponse(compressedResponse, ContentType.DEFAULT_BINARY, HttpStatus.SC_OK);
    ElasticSearchTransport transport = new ElasticSearchTransport(client, true);
    try {
        transport.sendBatch("foo".getBytes());
    } catch (Exception e) {
        assertEquals("es call failed because expected failure", e.getCause().getMessage());
        throw e;
    }
}
Also used : GZIPOutputStream(java.util.zip.GZIPOutputStream) HttpClient(org.apache.http.client.HttpClient) ByteArrayOutputStream(java.io.ByteArrayOutputStream) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) TransportException(com.nextdoor.bender.ipc.TransportException) Test(org.junit.Test)

Example 19 with TransportException

use of com.nextdoor.bender.ipc.TransportException in project bender by Nextdoor.

the class S3TransporterTest method testAmazonClientException.

@Test(expected = TransportException.class)
public void testAmazonClientException() throws TransportException, IllegalStateException, IOException {
    /*
     * Create mock client, requets, and replies
     */
    AmazonS3Client mockClient = mock(AmazonS3Client.class);
    UploadPartResult uploadResult = new UploadPartResult();
    uploadResult.setETag("foo");
    doThrow(new AmazonClientException("expected")).when(mockClient).uploadPart(any(UploadPartRequest.class));
    InitiateMultipartUploadResult initUploadResult = new InitiateMultipartUploadResult();
    initUploadResult.setUploadId("123");
    doReturn(initUploadResult).when(mockClient).initiateMultipartUpload(any(InitiateMultipartUploadRequest.class));
    /*
     * Fill buffer with mock data
     */
    S3TransportBuffer buffer = new S3TransportBuffer(1000, false, new S3TransportSerializer());
    InternalEvent mockIevent = mock(InternalEvent.class);
    doReturn("foo").when(mockIevent).getSerialized();
    /*
     * Create transport
     */
    Map<String, MultiPartUpload> multiPartUploads = new HashMap<String, MultiPartUpload>(0);
    S3Transport transport = new S3Transport(mockClient, "bucket", "basepath", false, multiPartUploads);
    /*
     * Do actual test
     */
    buffer.add(mockIevent);
    LinkedHashMap<String, String> partitions = new LinkedHashMap<String, String>();
    partitions.put(S3Transport.FILENAME_KEY, "a_filename");
    ArgumentCaptor<UploadPartRequest> argument = ArgumentCaptor.forClass(UploadPartRequest.class);
    try {
        transport.sendBatch(buffer, partitions, new TestContext());
    } catch (Exception e) {
        assertEquals(e.getCause().getClass(), AmazonClientException.class);
        throw e;
    }
}
Also used : InitiateMultipartUploadResult(com.amazonaws.services.s3.model.InitiateMultipartUploadResult) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) AmazonClientException(com.amazonaws.AmazonClientException) TestContext(com.nextdoor.bender.aws.TestContext) InitiateMultipartUploadRequest(com.amazonaws.services.s3.model.InitiateMultipartUploadRequest) UploadPartRequest(com.amazonaws.services.s3.model.UploadPartRequest) TransportException(com.nextdoor.bender.ipc.TransportException) IOException(java.io.IOException) AmazonClientException(com.amazonaws.AmazonClientException) InternalEvent(com.nextdoor.bender.InternalEvent) LinkedHashMap(java.util.LinkedHashMap) UploadPartResult(com.amazonaws.services.s3.model.UploadPartResult) AmazonS3Client(com.amazonaws.services.s3.AmazonS3Client) Test(org.junit.Test)

Example 20 with TransportException

use of com.nextdoor.bender.ipc.TransportException in project bender by Nextdoor.

the class HttpTransportTest method testErrorsResponse.

@Test(expected = TransportException.class)
public void testErrorsResponse() throws TransportException, IOException {
    byte[] respPayload = "resp".getBytes(StandardCharsets.UTF_8);
    HttpClient client = getMockClientWithResponse(respPayload, ContentType.APPLICATION_JSON, HttpStatus.SC_INTERNAL_SERVER_ERROR, false);
    HttpTransport transport = new HttpTransport(client, "", false, 1, 1);
    try {
        transport.sendBatch("foo".getBytes());
    } catch (Exception e) {
        assertEquals("http transport call failed because \"expected failure\" payload response \"resp\"", e.getCause().getMessage());
        throw e;
    }
}
Also used : HttpClient(org.apache.http.client.HttpClient) TransportException(com.nextdoor.bender.ipc.TransportException) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

TransportException (com.nextdoor.bender.ipc.TransportException)20 IOException (java.io.IOException)16 Test (org.junit.Test)10 TestContext (com.nextdoor.bender.aws.TestContext)5 IpcSenderService (com.nextdoor.bender.ipc.IpcSenderService)5 AmazonClientException (com.amazonaws.AmazonClientException)3 InitiateMultipartUploadRequest (com.amazonaws.services.s3.model.InitiateMultipartUploadRequest)3 InitiateMultipartUploadResult (com.amazonaws.services.s3.model.InitiateMultipartUploadResult)3 UploadPartRequest (com.amazonaws.services.s3.model.UploadPartRequest)3 UploadPartResult (com.amazonaws.services.s3.model.UploadPartResult)3 RetriesExhaustedException (com.evanlennick.retry4j.exception.RetriesExhaustedException)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)3 SerializationException (com.nextdoor.bender.serializer.SerializationException)3 ArrayList (java.util.ArrayList)3 HttpClient (org.apache.http.client.HttpClient)3 Context (com.amazonaws.services.lambda.runtime.Context)2 AmazonS3Client (com.amazonaws.services.s3.AmazonS3Client)2 ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)2 CallExecutor (com.evanlennick.retry4j.CallExecutor)2 UnexpectedException (com.evanlennick.retry4j.exception.UnexpectedException)2