Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve code coverage #83

Merged
merged 6 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions task-ballerina/tests/schedulerTest.bal
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,177 @@ function testUnscheduleJob() returns error? {
runtime:sleep(4);
test:assertEquals(count18, 3, msg = "Expected count mismatched.");
}


class Job19 {

*Job;

public isolated function execute() {
}
}

@test:Config {
groups: ["FrequencyJob"]
}
isolated function testCivilRecordValidation() {
time:Utc currentUtc = time:utcNow();
time:Civil time = time:utcToCivil(currentUtc);
time:ZoneOffset zoneOffset = {hours: 30, minutes: 0};
time.utcOffset = zoneOffset;
JobId|Error result = scheduleOneTimeJob(new Job19(), time);
if(result is Error) {
test:assertTrue(result.message().includes("Couldn't convert given time to milli seconds"),
msg = "Output mismatched.");
} else {
test:assertFail("Test failed.");
}
}

int count20 = 0;

class Job20 {

*Job;

public function execute() {
count20 = count20 +1;
}
}

@test:Config {
groups: ["WorkerPool"],
dependsOn: [testIgnoreTrigger]
}
function testConfigureWorker() returns error? {
var output = configureWorkerPool(6, 7000);
JobId result = check scheduleJobRecurByFrequency(new Job20(), 1);
runtime:sleep(5);
test:assertTrue((4 < count20 && count20 <= 6), msg = "Expected count mismatched.");
}

int count21 = 0;

class Job21 {

*Job;
int i = 1;

public function execute() {
count21 = count21 +1;
}

isolated function init(int i) {
self.i = i;
}
}

@test:Config {
groups: ["FrequencyJob", "startTime"]
}
function testIntervalJobWithStattTime() returns error? {
time:Utc currentUtc = time:utcNow();
time:Utc newTime = time:utcAddSeconds(currentUtc, 3);
time:Civil startTime = time:utcToCivil(currentUtc);
time:Civil endTime = time:utcToCivil(newTime);

JobId result = check scheduleJobRecurByFrequency(new Job21(1), 1, startTime = startTime);
runtime:sleep(10);
test:assertTrue(count21 > 1, msg = "Expected count mismatched.");
}

int count22 = 0;

class Job22 {

*Job;
int i = 1;

public function execute() {
count22 = count22 +1;
}

isolated function init(int i) {
self.i = i;
}
}

@test:Config {
groups: ["FrequencyJob", "endTime"]
}
function testIntervalJobWithEndTime() returns error? {
time:Utc currentUtc = time:utcNow();
time:Utc newTime = time:utcAddSeconds(currentUtc, 5);
time:Civil startTime = time:utcToCivil(currentUtc);
time:Civil endTime = time:utcToCivil(newTime);

JobId result = check scheduleJobRecurByFrequency(new Job22(1), 1, endTime = endTime);
runtime:sleep(7);
test:assertTrue(count22 >= 4, msg = "Expected count mismatched.");
}

class Job23 {

*Job;

public isolated function execute() {
}
}

@test:Config {
groups: ["FrequencyJob", "negative"]
}
isolated function testConfigurationValidation() returns error? {
JobId result = check scheduleJobRecurByFrequency(new Job23(), 1);
Error? output = configureWorkerPool(-6, 7000);
if(output is Error) {
test:assertTrue(output.message().includes("Cannot create the Scheduler.Thread count must be > 0"));
} else {
test:assertFail("Test failed.");
}
}

@test:Config {
groups: ["FrequencyJob", "negative"]
}
isolated function testMaxCountValidation() {
JobId|Error output = scheduleJobRecurByFrequency(new Job23(), 1, maxCount = -4);
if(output is Error) {
test:assertTrue(output.message().includes("The maxCount should be a positive integer."));
} else {
test:assertFail("Test failed.");
}
}

@test:Config {
groups: ["FrequencyJob"]
}
isolated function testEmptyRunningJobs() returns error? {
JobId[] ids = getRunningJobs();
if (ids.length() > 0) {
foreach JobId i in ids {
var result = unscheduleJob(i);
}
ids = getRunningJobs();
test:assertTrue(ids.length() == 0);
} else {
test:assertTrue(ids.length() == 0);
}
Error? output = configureWorkerPool(7, 7000);
JobId result = check scheduleJobRecurByFrequency(new Job23(), 1);
ids = getRunningJobs();
test:assertTrue(ids.length() == 1);
}

@test:Config {
groups: ["FrequencyJob", "negative"]
}
isolated function testUnscheduleJobs() returns error? {
JobId id = {id: 12};
Error? result = unscheduleJob(id);
if(result is Error) {
test:assertTrue(result.message().includes("Invalid job id"));
} else {
test:assertFail("Test failed.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public static Object scheduleIntervalJob(Environment env, BObject job, BDecimal
return jobId;
}

private static Scheduler getScheduler(Environment env) throws SchedulingException {
private static Scheduler getScheduler(Environment env) throws SchedulingException, SchedulerException {
return TaskManager.getInstance().getScheduler(Utils.createSchedulerProperties(
TaskConstants.QUARTZ_THREAD_COUNT_VALUE, TaskConstants.QUARTZ_THRESHOLD_VALUE), env);
}
Expand All @@ -107,7 +107,7 @@ private static JobDataMap getJobDataMap(BObject job, String errorPolicy, String
public static Object unscheduleJob(Long jobId) {
try {
TaskManager.getInstance().unScheduleJob(Math.toIntExact(jobId));
} catch (SchedulerException e) {
} catch (SchedulerException | SchedulingException e) {
return Utils.createTaskError(e.getMessage());
}
return null;
Expand All @@ -134,7 +134,7 @@ public static Object resumeAllJobs() {
public static Object pauseJob(Long jobId) {
try {
TaskManager.getInstance().pauseJob(Math.toIntExact(jobId));
} catch (SchedulerException e) {
} catch (SchedulerException | SchedulingException e) {
return Utils.createTaskError(e.getMessage());
}
return null;
Expand All @@ -143,7 +143,7 @@ public static Object pauseJob(Long jobId) {
public static Object resumeJob(Long jobId) {
try {
TaskManager.getInstance().resumeJob(Math.toIntExact(jobId));
} catch (SchedulerException e) {
} catch (SchedulerException | SchedulingException e) {
return Utils.createTaskError(e.getMessage());
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public void initializeScheduler(Properties properties, Environment env) throws S
SchedulerException {
getAllRunningJobs();
if (!triggerInfoMap.isEmpty()) {
rescheduleJobs();
configureScheduler(properties, env);
} else {
this.properties = properties;
Expand All @@ -75,12 +74,13 @@ public void rescheduleJobs() throws SchedulerException {
}
}

public Scheduler getScheduler(Properties properties, Environment env) throws SchedulingException {
public Scheduler getScheduler(Properties properties, Environment env) throws SchedulingException,
SchedulerException {
if (isConfiguredSchFactory) {
this.scheduler = Utils.initializeScheduler(this.properties);
isConfiguredSchFactory = false;
}
if (this.scheduler == null) {
if (this.scheduler == null || this.scheduler.isShutdown()) {
this.scheduler = Utils.initializeScheduler(properties);
}
setRuntime(env.getRuntime());
Expand Down Expand Up @@ -135,8 +135,8 @@ private void startScheduler () throws SchedulerException {
}
}

public void unScheduleJob(Integer jobId) throws SchedulerException {
this.scheduler.unscheduleJob(this.triggerInfoMap.get(jobId).getKey());
public void unScheduleJob(Integer jobId) throws SchedulerException, SchedulingException {
this.scheduler.unscheduleJob(getTrigger(jobId).getKey());
if (getAllRunningJobs().isEmpty()) {
this.scheduler.shutdown();
}
Expand All @@ -150,12 +150,12 @@ public void resume() throws SchedulerException {
this.scheduler.resumeAll();
}

public void pauseJob(Integer jobId) throws SchedulerException {
this.scheduler.pauseJob(this.triggerInfoMap.get(jobId).getJobKey());
public void pauseJob(Integer jobId) throws SchedulerException, SchedulingException {
this.scheduler.pauseJob(getTrigger(jobId).getJobKey());
}

public void resumeJob(Integer jobId) throws SchedulerException {
this.scheduler.resumeJob(this.triggerInfoMap.get(jobId).getJobKey());
public void resumeJob(Integer jobId) throws SchedulerException, SchedulingException {
this.scheduler.resumeJob(getTrigger(jobId).getJobKey());
}

private boolean isTriggerCompleted(Trigger.TriggerState triggerState) {
Expand All @@ -164,14 +164,21 @@ private boolean isTriggerCompleted(Trigger.TriggerState triggerState) {

private void configureScheduler(Properties properties, Environment env) throws SchedulerException,
SchedulingException {
if (!triggerInfoMap.isEmpty()) {
rescheduleJobs();
}
if (this.scheduler != null) {
this.scheduler.shutdown();
}
this.scheduler = Utils.initializeScheduler(properties);
setRuntime(env.getRuntime());
if (!triggerInfoMap.isEmpty()) {
rescheduleJobs();
}
}

private Trigger getTrigger(Integer jobId) throws SchedulingException {
if (this.triggerInfoMap.get(jobId) == null) {
throw new SchedulingException("Invalid job id: " + jobId);
} else {
return this.triggerInfoMap.get(jobId);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public static Scheduler initializeScheduler(Properties properties) throws Schedu
GroupMatcher.triggerGroupEquals(TaskConstants.LOG));
return scheduler;
} catch (SchedulerException e) {
throw new SchedulingException("Cannot create the Scheduler.", e);
throw new SchedulingException("Cannot create the Scheduler." + e.getMessage());
}
}

Expand Down