Skip to content

Commit 06b4f7d

Browse files
authored
Fix unstable tests
1 parent 82cd4cd commit 06b4f7d

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractApiTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import com.cloudinary.api.ApiResponse;
55
import com.cloudinary.api.exceptions.BadRequest;
66
import com.cloudinary.api.exceptions.NotFound;
7-
import com.cloudinary.metadata.StringMetadataField;
7+
import com.cloudinary.test.rules.RetryRule;
88
import com.cloudinary.transformation.TextLayer;
99
import com.cloudinary.utils.ObjectUtils;
1010
import org.junit.*;
@@ -53,6 +53,8 @@ abstract public class AbstractApiTest extends MockableTest {
5353
private static String assetId1;
5454
private static String assetId2;
5555

56+
private static final int SLEEP_TIMEOUT = 5000;
57+
5658

5759
protected Api api;
5860

@@ -137,6 +139,9 @@ public static void tearDownClass() {
137139
@Rule
138140
public TestName currentTest = new TestName();
139141

142+
@Rule
143+
public RetryRule retryRule = new RetryRule();
144+
140145
@Before
141146
public void setUp() {
142147
System.out.println("Running " + this.getClass().getName() + "." + currentTest.getMethodName());
@@ -909,8 +914,10 @@ public void testRestoreDifferentVersionsOfDeletedAsset() throws Exception {
909914
"tags", UPLOAD_TAGS
910915
));
911916
assertEquals(firstUpload.get("public_id"), TEST_RESOURCE_PUBLIC_ID);
917+
Thread.sleep(SLEEP_TIMEOUT);
912918
ApiResponse firstDelete = api.deleteResources(Collections.singletonList(TEST_RESOURCE_PUBLIC_ID), ObjectUtils.emptyMap());
913919
assertTrue(firstDelete.containsKey("deleted"));
920+
Thread.sleep(SLEEP_TIMEOUT);
914921

915922
Map secondUpload = uploader.upload(SRC_TEST_IMAGE,
916923
ObjectUtils.asMap(
@@ -920,13 +927,15 @@ public void testRestoreDifferentVersionsOfDeletedAsset() throws Exception {
920927
"tags", UPLOAD_TAGS
921928
));
922929
assertEquals(secondUpload.get("public_id"), TEST_RESOURCE_PUBLIC_ID);
930+
Thread.sleep(SLEEP_TIMEOUT);
923931
ApiResponse secondDelete = api.deleteResources(Collections.singletonList(TEST_RESOURCE_PUBLIC_ID), ObjectUtils.emptyMap());
924932
assertTrue(secondDelete.containsKey("deleted"));
925-
933+
Thread.sleep(SLEEP_TIMEOUT);
926934
assertNotEquals(firstUpload.get("bytes"), secondUpload.get("bytes"));
927935

928936
ApiResponse getVersionsResp = api.resource(TEST_RESOURCE_PUBLIC_ID, ObjectUtils.asMap("versions", true));
929937
List<Map> versions = (List<Map>) getVersionsResp.get("versions");
938+
Assert.assertTrue(versions.size() > 1);
930939
Object firstAssetVersion = versions.get(0).get("version_id");
931940
Object secondAssetVersion = versions.get(1).get("version_id");
932941

@@ -937,7 +946,7 @@ public void testRestoreDifferentVersionsOfDeletedAsset() throws Exception {
937946
ApiResponse secondVerRestore = api.restore(Collections.singletonList(TEST_RESOURCE_PUBLIC_ID),
938947
ObjectUtils.asMap("versions", Collections.singletonList(secondAssetVersion)));
939948
assertEquals(((Map) secondVerRestore.get(TEST_RESOURCE_PUBLIC_ID)).get("bytes"), secondUpload.get("bytes"));
940-
949+
Thread.sleep(SLEEP_TIMEOUT);
941950
ApiResponse finalDeleteResp = api.deleteResources(Collections.singletonList(TEST_RESOURCE_PUBLIC_ID), ObjectUtils.emptyMap());
942951
assertTrue(finalDeleteResp.containsKey("deleted"));
943952
}
@@ -1157,7 +1166,7 @@ public void testQualityAnalysis() throws Exception {
11571166
public void testDeleteFolder() throws Exception {
11581167
String toDelete = "todelete_" + SUFFIX;
11591168
Map uploadResult = cloudinary.uploader().upload(SRC_TEST_IMAGE, asMap("tags", UPLOAD_TAGS, "folder", toDelete));
1160-
Thread.sleep(5000);
1169+
Thread.sleep(SLEEP_TIMEOUT);
11611170
api.deleteResources(Collections.singletonList(uploadResult.get("public_id").toString()), emptyMap());
11621171
ApiResponse result = api.deleteFolder(toDelete, emptyMap());
11631172
assertTrue(((ArrayList) result.get("deleted")).contains(toDelete));

cloudinary-test-common/src/main/java/com/cloudinary/test/AbstractUploaderTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.cloudinary.*;
44
import com.cloudinary.api.ApiResponse;
55
import com.cloudinary.metadata.StringMetadataField;
6+
import com.cloudinary.test.rules.RetryRule;
67
import com.cloudinary.utils.ObjectUtils;
78
import com.cloudinary.utils.Rectangle;
89
import org.cloudinary.json.JSONArray;
@@ -81,6 +82,9 @@ public static void tearDownClass() {
8182
@Rule
8283
public TestName currentTest = new TestName();
8384

85+
@Rule
86+
public RetryRule retryRule = new RetryRule();
87+
8488
@Before
8589
public void setUp() {
8690
System.out.println("Running " + this.getClass().getName() + "." + currentTest.getMethodName());
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.cloudinary.test.rules;
2+
3+
import org.junit.rules.TestRule;
4+
import org.junit.runner.Description;
5+
import org.junit.runners.model.Statement;
6+
7+
import java.util.Objects;
8+
9+
public class RetryRule implements TestRule {
10+
private int retryCount;
11+
private int delay;
12+
13+
public RetryRule(int retryCount, int delay) {
14+
this.retryCount = retryCount;
15+
this.delay = delay;
16+
}
17+
18+
public RetryRule() {
19+
this.retryCount = 3;
20+
this.delay = 3;
21+
}
22+
23+
public Statement apply(Statement base, Description description) {
24+
return statement(base, description);
25+
}
26+
27+
private Statement statement(final Statement base, final Description description) {
28+
return new Statement() {
29+
@Override
30+
public void evaluate() throws Throwable {
31+
Throwable caughtThrowable = null;
32+
for (int i = 0; i < retryCount; i++) {
33+
try {
34+
base.evaluate();
35+
return;
36+
} catch (Throwable t) {
37+
caughtThrowable = t;
38+
System.err.println(description.getDisplayName() + ": run " + (i + 1) + " failed.");
39+
Thread.sleep(delay * 1000);
40+
}
41+
}
42+
System.err.println(description.getDisplayName() + ": Giving up after " + retryCount + " failures.");
43+
throw Objects.requireNonNull(caughtThrowable);
44+
}
45+
};
46+
}
47+
}

0 commit comments

Comments
 (0)