Skip to content

Commit 199d31c

Browse files
SattvikSattvik
authored andcommitted
fix: tests
1 parent 12037be commit 199d31c

File tree

3 files changed

+44
-7
lines changed

3 files changed

+44
-7
lines changed

src/test/java/io/supertokens/storage/postgresql/test/ConfigTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,18 +120,18 @@ public void testThatInvalidConfigThrowsRightError() throws Exception {
120120
public void testThatMissingConfigFileThrowsError() throws Exception {
121121
String[] args = {"../"};
122122

123+
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
124+
123125
String workerId = System.getProperty("org.gradle.test.worker");
124126
ProcessBuilder pb = new ProcessBuilder("rm", "-r", "config" + workerId + ".yaml");
125127
pb.directory(new File(args[0]));
126128
Process process1 = pb.start();
127129
process1.waitFor();
128130

129-
TestingProcessManager.TestingProcess process = TestingProcessManager.start(args);
130-
131131
ProcessState.EventAndException e = process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.INIT_FAILURE);
132132
assertNotNull(e);
133133
TestCase.assertEquals(e.exception.getMessage(),
134-
"config" + workerId + ".yaml (No such file or directory)");
134+
"../config" + workerId + ".yaml (No such file or directory)");
135135

136136
process.kill();
137137
assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED));

src/test/java/io/supertokens/storage/postgresql/test/Utils.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.junit.rules.TestRule;
2727
import org.junit.rules.TestWatcher;
2828
import org.junit.runner.Description;
29+
import org.junit.runners.model.Statement;
2930
import org.mockito.Mockito;
3031

3132
import javax.net.ssl.HttpsURLConnection;
@@ -35,6 +36,7 @@
3536
import java.nio.file.Paths;
3637
import java.util.Arrays;
3738
import java.util.List;
39+
import java.util.Random;
3840

3941
public abstract class Utils extends Mockito {
4042

@@ -189,4 +191,38 @@ protected void failed(Throwable e, Description description) {
189191
};
190192
}
191193

194+
public static TestRule retryFlakyTest() {
195+
return new TestRule() {
196+
private final int retryCount = 10;
197+
198+
public Statement apply(Statement base, Description description) {
199+
return statement(base, description);
200+
}
201+
202+
private Statement statement(final Statement base, final Description description) {
203+
return new Statement() {
204+
@Override
205+
public void evaluate() throws Throwable {
206+
Throwable caughtThrowable = null;
207+
208+
// implement retry logic here
209+
for (int i = 0; i < retryCount; i++) {
210+
try {
211+
base.evaluate();
212+
return;
213+
} catch (Throwable t) {
214+
caughtThrowable = t;
215+
System.err.println(description.getDisplayName() + ": run " + (i+1) + " failed");
216+
TestingProcessManager.killAll();
217+
Thread.sleep(1000 + new Random().nextInt(3000));
218+
}
219+
}
220+
System.err.println(description.getDisplayName() + ": giving up after " + retryCount + " failures");
221+
throw caughtThrowable;
222+
}
223+
};
224+
}
225+
};
226+
}
227+
192228
}

src/test/java/io/supertokens/storage/postgresql/test/multitenancy/TestForNoCrashDuringStartup.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,8 @@
4141
import io.supertokens.storageLayer.StorageLayer;
4242
import io.supertokens.thirdparty.InvalidProviderConfigException;
4343
import io.supertokens.utils.SemVer;
44-
import org.junit.After;
45-
import org.junit.AfterClass;
46-
import org.junit.Before;
47-
import org.junit.Test;
44+
import org.junit.*;
45+
import org.junit.rules.TestRule;
4846

4947
import java.io.IOException;
5048

@@ -54,6 +52,9 @@
5452
public class TestForNoCrashDuringStartup {
5553
TestingProcessManager.TestingProcess process;
5654

55+
@Rule
56+
public TestRule retryFlaky = Utils.retryFlakyTest();
57+
5758
@AfterClass
5859
public static void afterTesting() {
5960
Utils.afterTesting();

0 commit comments

Comments
 (0)