Skip to content

Commit

Permalink
fix: added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Oct 11, 2023
1 parent 0cdf3ff commit 704fbc8
Showing 1 changed file with 65 additions and 2 deletions.
67 changes: 65 additions & 2 deletions src/test/java/io/supertokens/test/CronjobTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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.*;

Expand Down Expand Up @@ -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<List<TenantIdentifier>> 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<TenantIdentifier> tenants = new ArrayList<>();
tenants.add(new TenantIdentifier(null, null, null));
List<List<TenantIdentifier>> 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();

Expand Down Expand Up @@ -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 = {"../"};
Expand Down

0 comments on commit 704fbc8

Please sign in to comment.