findify / s3mock

Embedded S3 server for easy mocking
MIT License
386 stars 107 forks source link

S3Mock causes some S3Client calls to block for 30 seconds #186

Open klitoskyriacou opened 1 year ago

klitoskyriacou commented 1 year ago

The following code shows that S3Client.listBuckets(), which responds almost immediately when using the real AWS S3 service, blocks for 30 seconds when using S3Mock:

    @Test
    void testUpload() throws URISyntaxException {
        S3Mock mockS3 = new S3Mock.Builder().withPort(8001).withInMemoryBackend().build();
        mockS3.start();

        S3Client s3Client = S3Client.builder()
                .endpointOverride(new URI("http://localhost:8001"))
                .credentialsProvider(AnonymousCredentialsProvider.create())
                .region(Region.EU_CENTRAL_1)
                .build();
        s3Client.createBucket(CreateBucketRequest.builder().bucket("test-bucket").build());

        PutObjectRequest putObjectRequest = PutObjectRequest.builder()
                .bucket("test-bucket")
                .key("testfolder/testfile.json")
                .metadata(Map.of("contentType", "json"))
                .build();
        s3Client.putObject(putObjectRequest, RequestBody.empty());

        long start = System.nanoTime();
        ListBucketsResponse listBucketsResponse = s3Client.listBuckets();
        long end = System.nanoTime();
        System.out.printf("listBuckets() took %.1f seconds.%n%s%n", (end - start) / 1e9, listBucketsResponse);
    }

When run using mvn test (or in IntelliJ IDEA) the above test gives the following output:

listBuckets() took 30.1 seconds.
ListBucketsResponse(Buckets=[Bucket(Name=test-bucket, CreationDate=2022-09-09T16:20:26Z)], Owner=Owner(DisplayName=root, ID=731966b0-f56d-4e44-bfc9-465984e4f38c))

This test was run on Ubuntu Linux (versions 20.04 and 22.04).

For the full project (consisting of just two files) allowing you to reproduce this issue easily, clone https://github.com/klitoskyriacou/S3MockDemo.