Skip to content

Commit

Permalink
Merge pull request #83 from ballerina-platform/revamp
Browse files Browse the repository at this point in the history
Improve code coverage
  • Loading branch information
kalaiyarasiganeshalingam authored Mar 25, 2021
2 parents e684dc0 + b7bf253 commit 173a013
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 34 deletions.
178 changes: 177 additions & 1 deletion task-ballerina/tests/schedulerTest.bal
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ class Job6 {
}

@test:Config {
groups: ["FrequencyJob"]
groups: ["FrequencyJob"],
dependsOn: [testLogIgnore]
}
function testIgnoreTrigger() returns error? {
JobId id = check scheduleJobRecurByFrequency(new Job6(), 5, maxCount = 10, taskPolicy = { waitingPolicy: IGNORE });
Expand Down Expand Up @@ -412,3 +413,178 @@ 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"],
dependsOn: [testLogIgnore]
}
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 @@ -28,13 +28,10 @@
import org.ballerinalang.stdlib.task.exceptions.SchedulingException;
import org.ballerinalang.stdlib.task.objects.TaskManager;
import org.ballerinalang.stdlib.task.utils.TaskConstants;
import org.ballerinalang.stdlib.task.utils.TaskListener;
import org.ballerinalang.stdlib.task.utils.Utils;
import org.quartz.JobDataMap;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.math.BigDecimal;
import java.util.Random;
Expand All @@ -51,7 +48,6 @@ public class TaskActions {

private static int bound = 1000000;
private static String value = "1000";
private static final Logger LOG = LoggerFactory.getLogger(TaskListener.class);

public static Object configureThread(Environment env, long workerCount, long waitingTimeInMillis) {
try {
Expand Down Expand Up @@ -91,7 +87,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 +103,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 +130,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 +139,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 @@ -21,15 +21,8 @@
* This exception is thrown when scheduling fails.
*/
public class SchedulingException extends Exception {
public SchedulingException() {
super();
}

public SchedulingException(String message) {
super(message);
}

public SchedulingException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ public static TaskManager getInstance() {
public void initializeScheduler(Properties properties, Environment env) throws SchedulingException,
SchedulerException {
getAllRunningJobs();
if (this.scheduler != null) {
this.scheduler.shutdown();
}
if (!triggerInfoMap.isEmpty()) {
rescheduleJobs();
configureScheduler(properties, env);
} else {
this.properties = properties;
Expand All @@ -75,15 +77,18 @@ 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;
setRuntime(env.getRuntime());
} else {
if (this.scheduler == null || this.scheduler.isShutdown()) {
this.scheduler = Utils.initializeScheduler(properties);
setRuntime(env.getRuntime());
}
}
if (this.scheduler == null) {
this.scheduler = Utils.initializeScheduler(properties);
}
setRuntime(env.getRuntime());
return this.scheduler;
}

Expand Down Expand Up @@ -135,8 +140,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 +155,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 +169,18 @@ private boolean isTriggerCompleted(Trigger.TriggerState triggerState) {

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

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

0 comments on commit 173a013

Please sign in to comment.