From 704fbc8ca5a0633def7bcc0ca2167b9bf161a6d3 Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Wed, 11 Oct 2023 15:27:13 +0530 Subject: [PATCH] fix: added tests --- .../java/io/supertokens/test/CronjobTest.java | 67 ++++++++++++++++++- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/src/test/java/io/supertokens/test/CronjobTest.java b/src/test/java/io/supertokens/test/CronjobTest.java index 606ea745b..b4d30d83b 100644 --- a/src/test/java/io/supertokens/test/CronjobTest.java +++ b/src/test/java/io/supertokens/test/CronjobTest.java @@ -22,7 +22,6 @@ import io.supertokens.cronjobs.CronTask; import io.supertokens.cronjobs.CronTaskTest; import io.supertokens.cronjobs.Cronjobs; -import io.supertokens.cronjobs.deleteExpiredDashboardSessions.DeleteExpiredDashboardSessions; import io.supertokens.cronjobs.syncCoreConfigWithDb.SyncCoreConfigWithDb; import io.supertokens.exceptions.QuitProgramException; import io.supertokens.featureflag.EE_FEATURES; @@ -31,7 +30,6 @@ import io.supertokens.multitenancy.MultitenancyHelper; import io.supertokens.pluginInterface.STORAGE_TYPE; import io.supertokens.pluginInterface.Storage; -import io.supertokens.pluginInterface.authRecipe.AuthRecipeUserInfo; import io.supertokens.pluginInterface.multitenancy.*; import io.supertokens.pluginInterface.multitenancy.exceptions.TenantOrAppNotFoundException; import io.supertokens.storageLayer.StorageLayer; @@ -41,8 +39,10 @@ import org.junit.Test; import org.junit.rules.TestRule; import org.reflections.Reflections; +import org.w3c.dom.css.Counter; import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; import static org.junit.Assert.*; @@ -308,6 +308,49 @@ protected void doTaskPerStorage(Storage storage) throws Exception { } } + static class CounterCronJob extends CronTask { + private static final String RESOURCE_ID = "io.supertokens.test.CronjobTest.CounterCronJob"; + private static AtomicInteger count = new AtomicInteger(); + + private CounterCronJob(Main main, List> tenantsInfo) { + super("CounterCronJob", main, tenantsInfo, false); + } + + public static CounterCronJob getInstance(Main main) { + try { + return (CounterCronJob) main.getResourceDistributor() + .getResource(new TenantIdentifier(null, null, null), RESOURCE_ID); + } catch (TenantOrAppNotFoundException e) { + List tenants = new ArrayList<>(); + tenants.add(new TenantIdentifier(null, null, null)); + List> finalList = new ArrayList<>(); + finalList.add(tenants); + return (CounterCronJob) main.getResourceDistributor() + .setResource(new TenantIdentifier(null, null, null), RESOURCE_ID, + new CounterCronJob(main, finalList)); + } + } + + @Override + public int getIntervalTimeSeconds() { + return 1; + } + + @Override + public int getInitialWaitTimeSeconds() { + return 0; + } + + @Override + protected void doTaskPerStorage(Storage storage) throws Exception { + count.incrementAndGet(); + } + + public int getCount() { + return count.get(); + } + } + @Rule public TestRule watchman = Utils.getOnFailure(); @@ -780,6 +823,26 @@ public void testThatCoreAutomaticallySyncsToConfigChangesInDb() throws Exception assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); } + @Test + public void testThatReAddingSameCronTaskDoesNotScheduleMoreExecutors() throws Exception { + String[] args = {"../"}; + + TestingProcessManager.TestingProcess process = TestingProcessManager.start(args); + assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STARTED)); + + assertEquals(normalCronjobCounter, 0); + for (int i=0; i<10; i++) { + Cronjobs.addCronjob(process.getProcess(), CounterCronJob.getInstance(process.getProcess())); + Thread.sleep(50); + } + + Thread.sleep(5000); + assertTrue(CounterCronJob.getInstance(process.getProcess()).getCount() > 3 && CounterCronJob.getInstance(process.getProcess()).getCount() < 8); + + process.kill(); + assertNotNull(process.checkOrWaitForEvent(ProcessState.PROCESS_STATE.STOPPED)); + } + @Test public void testThatNoCronJobIntervalIsMoreThanADay() throws Exception { String[] args = {"../"};