Search in sources :

Example 1 with ConstantInputStream

use of com.amazonaws.testutils.util.ConstantInputStream in project aws-sdk-android by aws-amplify.

the class S3InterruptIntegrationTest method testUploadInterrupts.

// Interrupting thread doesn't really abort the upload early UNLESS
// the content length is set
@Test
public void testUploadInterrupts() throws InterruptedException {
    final InputStream is = new ConstantInputStream(DATA_SIZE, (byte) 'Z');
    final ObjectMetadata omd = new ObjectMetadata();
    omd.setContentLength(DATA_SIZE);
    final PutObjectRequest req = new PutObjectRequest(TEST_BUCKET, "test", is, omd);
    final Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                s3.putObject(req);
                fail("PUT should have been aborted");
            } catch (final AbortedException expected) {
            // expected
            }
        }
    });
    t.start();
    do {
        t.join(2000);
        if (t.isAlive()) {
            t.interrupt();
            debug("Upload thread interrupted");
        } else {
            debug("Upload thread joined");
        }
    } while (t.isAlive());
    debug("Upload done");
}
Also used : ConstantInputStream(com.amazonaws.testutils.util.ConstantInputStream) InputStream(java.io.InputStream) AbortedException(com.amazonaws.AbortedException) ConstantInputStream(com.amazonaws.testutils.util.ConstantInputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.junit.Test)

Example 2 with ConstantInputStream

use of com.amazonaws.testutils.util.ConstantInputStream in project aws-sdk-android by aws-amplify.

the class S3InterruptIntegrationTest method testUploadInterruptsViaFuture.

@Test
public void testUploadInterruptsViaFuture() throws InterruptedException {
    final InputStream is = new ConstantInputStream(DATA_SIZE, (byte) 'Z');
    final ObjectMetadata omd = new ObjectMetadata();
    omd.setContentLength(DATA_SIZE);
    final PutObjectRequest req = new PutObjectRequest(TEST_BUCKET, "test", is, omd);
    final ExecutorService es = Executors.newSingleThreadExecutor();
    final Boolean[] success = { null };
    final Future<?> f = es.submit(new Runnable() {

        @Override
        public void run() {
            try {
                s3.putObject(req);
                synchronized (success) {
                    success[0] = Boolean.FALSE;
                }
                return;
            } catch (final AbortedException expected) {
            // expected
            }
            synchronized (success) {
                success[0] = Boolean.TRUE;
            }
        }
    });
    Thread.sleep(2000);
    f.cancel(true);
    while (success[0] == null) {
        Thread.sleep(2000);
    }
    es.shutdownNow();
    assertTrue("PUT via future should have been aborted", success[0].booleanValue());
    debug("Upload via future done");
}
Also used : ConstantInputStream(com.amazonaws.testutils.util.ConstantInputStream) InputStream(java.io.InputStream) AbortedException(com.amazonaws.AbortedException) ExecutorService(java.util.concurrent.ExecutorService) ConstantInputStream(com.amazonaws.testutils.util.ConstantInputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.junit.Test)

Example 3 with ConstantInputStream

use of com.amazonaws.testutils.util.ConstantInputStream in project aws-sdk-android by aws-amplify.

the class S3InterruptIntegrationTest method testDownloadInterruptsViaFuture.

@Test
public void testDownloadInterruptsViaFuture() throws InterruptedException, IOException {
    // Put an object to S3
    final InputStream is = new ConstantInputStream(DATA_SIZE, (byte) 'Z');
    final ObjectMetadata omd = new ObjectMetadata();
    omd.setContentLength(DATA_SIZE);
    final PutObjectRequest req = new PutObjectRequest(TEST_BUCKET, "test", is, omd);
    s3.putObject(req);
    final File destfile = CryptoTestUtils.generateRandomAsciiFile(0);
    final ExecutorService es = Executors.newSingleThreadExecutor();
    final Boolean[] success = { null };
    final Future<?> f = es.submit(new Runnable() {

        @Override
        public void run() {
            try {
                final GetObjectRequest req = new GetObjectRequest(TEST_BUCKET, "test");
                s3.getObject(req, destfile);
                synchronized (success) {
                    success[0] = Boolean.FALSE;
                }
                return;
            } catch (final AbortedException expected) {
            }
            synchronized (success) {
                success[0] = Boolean.TRUE;
            }
        }
    });
    Thread.sleep(2000);
    f.cancel(true);
    while (success[0] == null) {
        Thread.sleep(2000);
    }
    es.shutdownNow();
    assertTrue("GET should have been aborted", success[0].booleanValue());
    debug("Download via future done");
}
Also used : ConstantInputStream(com.amazonaws.testutils.util.ConstantInputStream) InputStream(java.io.InputStream) ConstantInputStream(com.amazonaws.testutils.util.ConstantInputStream) AbortedException(com.amazonaws.AbortedException) ExecutorService(java.util.concurrent.ExecutorService) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) File(java.io.File) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.junit.Test)

Example 4 with ConstantInputStream

use of com.amazonaws.testutils.util.ConstantInputStream in project aws-sdk-android by aws-amplify.

the class S3InterruptIntegrationTest method testDownloadInterrupts.

@Test
public void testDownloadInterrupts() throws InterruptedException, IOException {
    // Put an object to S3
    final InputStream is = new ConstantInputStream(DATA_SIZE, (byte) 'Z');
    final ObjectMetadata omd = new ObjectMetadata();
    omd.setContentLength(DATA_SIZE);
    final PutObjectRequest req = new PutObjectRequest(TEST_BUCKET, "test", is, omd);
    s3.putObject(req);
    final File destfile = CryptoTestUtils.generateRandomAsciiFile(0);
    final Thread t = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                final GetObjectRequest req = new GetObjectRequest(TEST_BUCKET, "test");
                s3.getObject(req, destfile);
                fail("GET should have been aborted");
            } catch (final AbortedException expected) {
            }
        }
    });
    t.start();
    do {
        t.join(2000);
        if (t.isAlive()) {
            t.interrupt();
            debug("download thread interrupted");
        } else {
            debug("download thread joined");
        }
    } while (t.isAlive());
    debug("Download done");
}
Also used : ConstantInputStream(com.amazonaws.testutils.util.ConstantInputStream) InputStream(java.io.InputStream) AbortedException(com.amazonaws.AbortedException) ConstantInputStream(com.amazonaws.testutils.util.ConstantInputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) File(java.io.File) GetObjectRequest(com.amazonaws.services.s3.model.GetObjectRequest) PutObjectRequest(com.amazonaws.services.s3.model.PutObjectRequest) Test(org.junit.Test)

Example 5 with ConstantInputStream

use of com.amazonaws.testutils.util.ConstantInputStream in project aws-sdk-android by aws-amplify.

the class S3SkipByteDigestIntegrationTest method testDigestWithSkippedBytes.

// https://github.com/aws/aws-sdk-java/issues/232
@Test
public void testDigestWithSkippedBytes() throws IOException {
    if (DEBUG) {
        debug("DATA_SIZE=" + DATA_SIZE);
    }
    final InputStream is = new ConstantInputStream(DATA_SIZE, (byte) 'Z');
    final ObjectMetadata meta = new ObjectMetadata();
    meta.setContentLength(DATA_SIZE);
    s3.putObject(TEST_BUCKET, "key", is, meta);
    {
        // skip some random number of bytes
        final S3Object object = s3.getObject(TEST_BUCKET, "key");
        final InputStream content = object.getObjectContent();
        final int n = rand.nextInt(DATA_SIZE);
        if (DEBUG) {
            debug("n=" + n);
        }
        content.skip(n);
        IOUtils.toByteArray(content);
        content.close();
    }
    {
        // skip 0 bytes;
        final S3Object object = s3.getObject(TEST_BUCKET, "key");
        final InputStream content = object.getObjectContent();
        final int n = 0;
        if (DEBUG) {
            debug("n=" + n);
        }
        content.skip(n);
        IOUtils.toByteArray(content);
        content.close();
    }
    {
        // skip -1 bytes;
        final S3Object object = s3.getObject(TEST_BUCKET, "key");
        final InputStream content = object.getObjectContent();
        final int n = -1;
        if (DEBUG) {
            debug("n=" + n);
        }
        content.skip(n);
        IOUtils.toByteArray(content);
        content.close();
    }
    {
        // skip all bytes
        final S3Object object = s3.getObject(TEST_BUCKET, "key");
        final InputStream content = object.getObjectContent();
        final int n = DATA_SIZE;
        if (DEBUG) {
            debug("n=" + n);
        }
        content.skip(n);
        IOUtils.toByteArray(content);
        content.close();
    }
    {
        // skip more than all bytes
        final S3Object object = s3.getObject(TEST_BUCKET, "key");
        final InputStream content = object.getObjectContent();
        final int n = DATA_SIZE + 100;
        if (DEBUG) {
            debug("n=" + n);
        }
        content.skip(n);
        IOUtils.toByteArray(content);
        content.close();
    }
}
Also used : ConstantInputStream(com.amazonaws.testutils.util.ConstantInputStream) InputStream(java.io.InputStream) S3Object(com.amazonaws.services.s3.model.S3Object) ConstantInputStream(com.amazonaws.testutils.util.ConstantInputStream) ObjectMetadata(com.amazonaws.services.s3.model.ObjectMetadata) Test(org.junit.Test)

Aggregations

ObjectMetadata (com.amazonaws.services.s3.model.ObjectMetadata)5 ConstantInputStream (com.amazonaws.testutils.util.ConstantInputStream)5 InputStream (java.io.InputStream)5 Test (org.junit.Test)5 AbortedException (com.amazonaws.AbortedException)4 PutObjectRequest (com.amazonaws.services.s3.model.PutObjectRequest)4 GetObjectRequest (com.amazonaws.services.s3.model.GetObjectRequest)2 File (java.io.File)2 ExecutorService (java.util.concurrent.ExecutorService)2 S3Object (com.amazonaws.services.s3.model.S3Object)1