Skip to content

Commit

Permalink
[0eSjZq6Q] Fix S3 failing flaky tests (neo4j/apoc#294) (#3448)
Browse files Browse the repository at this point in the history
  • Loading branch information
vga91 authored Feb 13, 2023
1 parent c3e2a29 commit 551bd72
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions core/src/test/java/apoc/util/s3/S3TestUtil.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package apoc.util.s3;

import com.amazonaws.AmazonClientException;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.S3Object;
import com.amazonaws.services.s3.model.S3ObjectInputStream;
import com.amazonaws.util.IOUtils;
import org.neo4j.test.assertion.Assert;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import static org.junit.Assert.assertEquals;

Expand All @@ -21,7 +24,7 @@ public class S3TestUtil {
* @param s3Url String containing url to S3 bucket.
* @return the s3 string object
*/
public static String readS3FileToString(String s3Url) {
public static String readS3FileToString(String s3Url) throws AmazonClientException {
try {
S3Object s3object = getS3Object(s3Url);
S3ObjectInputStream inputStream = s3object.getObjectContent();
Expand All @@ -31,17 +34,27 @@ public static String readS3FileToString(String s3Url) {
}
}

public static S3Object getS3Object(String s3Url) throws MalformedURLException {
public static S3Object getS3Object(String s3Url) throws MalformedURLException, AmazonClientException {
S3Params s3Params = S3ParamsExtractor.extract(new URL(s3Url));
S3Aws s3Aws = new S3Aws(s3Params, s3Params.getRegion());
AmazonS3 s3Client = s3Aws.getClient();

S3Object s3object = s3Client.getObject(s3Params.getBucket(), s3Params.getKey());
return s3object;

return s3Client.getObject(s3Params.getBucket(), s3Params.getKey());
}

public static void assertStringFileEquals(String expected, String s3Url) {
final String actual = readS3FileToString(s3Url);
assertEquals(expected, actual);
Assert.assertEventually(() -> {
final String actual;
try {
actual = readS3FileToString(s3Url);
} catch (AmazonClientException e) {
if (e.getMessage().contains("The specified key does not exist")) {
return false;
}
throw e;
}
assertEquals(expected, actual);
return true;
}, v -> v, 30L, TimeUnit.SECONDS);
}
}

0 comments on commit 551bd72

Please sign in to comment.