Skip to content

Commit

Permalink
Merge pull request #42 from kagkarlsson/fix_fetch_executions_by_taskname
Browse files Browse the repository at this point in the history
Fixed bug in Fetch ScheduledExecutions by taskname
  • Loading branch information
kagkarlsson authored Nov 9, 2018
2 parents b589a45 + 7d72c7c commit bf800b4
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.github.kagkarlsson.scheduler;

import com.github.kagkarlsson.scheduler.task.Execution;
import com.github.kagkarlsson.scheduler.task.Task;
import com.github.kagkarlsson.scheduler.task.TaskInstance;
import com.github.kagkarlsson.scheduler.task.TaskInstanceId;
import org.slf4j.Logger;
Expand All @@ -24,6 +25,8 @@
import javax.sql.DataSource;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;

Expand All @@ -42,14 +45,21 @@ public interface SchedulerClient {
class Builder {

private final DataSource dataSource;
private List<Task<?>> knownTasks;
private final Serializer serializer = Serializer.DEFAULT_JAVA_SERIALIZER;
private String tableName = JdbcTaskRepository.DEFAULT_TABLE_NAME;

private Builder(DataSource dataSource) {
private Builder(DataSource dataSource, List<Task<?>> knownTasks) {
this.dataSource = dataSource;
this.knownTasks = knownTasks;
}
public static Builder create(DataSource dataSource) {
return new Builder(dataSource);

public static Builder create(DataSource dataSource, Task<?> ... knownTasks) {
return new Builder(dataSource, Arrays.asList(knownTasks));
}

public static Builder create(DataSource dataSource, List<Task<?>> knownTasks) {
return new Builder(dataSource, knownTasks);
}

public Builder tableName(String tableName) {
Expand All @@ -58,7 +68,7 @@ public Builder tableName(String tableName) {
}

public SchedulerClient build() {
TaskResolver taskResolver = new TaskResolver(new ArrayList<>());
TaskResolver taskResolver = new TaskResolver(knownTasks);
TaskRepository taskRepository = new JdbcTaskRepository(dataSource, tableName, taskResolver, new SchedulerClientName(), serializer);

return new StandardSchedulerClient(taskRepository);
Expand Down Expand Up @@ -131,7 +141,7 @@ public void getScheduledExecutions(Consumer<ScheduledExecution<Object>> consumer

@Override
public <T> void getScheduledExecutionsForTask(String taskName, Class<T> dataClass, Consumer<ScheduledExecution<T>> consumer) {
taskRepository.getScheduledExecutions(execution -> new ScheduledExecution<>(dataClass, execution));
taskRepository.getScheduledExecutions(taskName, execution -> consumer.accept(new ScheduledExecution<>(dataClass, execution)));
}

private void notifyListeners(ClientEvent.EventType eventType, TaskInstanceId taskInstanceId, Instant executionTime) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,17 @@
import com.github.kagkarlsson.scheduler.task.ExecutionContext;
import com.github.kagkarlsson.scheduler.task.helper.OneTimeTask;
import com.github.kagkarlsson.scheduler.task.TaskInstance;
import com.google.common.collect.Lists;
import org.hamcrest.CoreMatchers;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

import java.time.Instant;
import java.util.concurrent.atomic.AtomicInteger;

import static java.time.Duration.ofSeconds;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;

public class SchedulerClientTest {
Expand All @@ -24,45 +27,76 @@ public class SchedulerClientTest {

private ManualScheduler scheduler;
private SettableClock settableClock;
private OneTimeTask<Void> oneTimeTask;
private OneTimeTask<Void> oneTimeTaskA;

private TestTasks.CountingHandler<Void> onetimeTaskHandler;
private TestTasks.CountingHandler<Void> onetimeTaskHandlerA;
private TestTasks.CountingHandler<Void> onetimeTaskHandlerB;
private ScheduleAnotherTaskHandler<Void> scheduleAnother;
private OneTimeTask<Void> scheduleAnotherTask;
private OneTimeTask<Void> oneTimeTaskB;

@Before
public void setUp() {
settableClock = new SettableClock();
onetimeTaskHandler = new TestTasks.CountingHandler<>();
oneTimeTask = TestTasks.oneTime("OneTime", Void.class, onetimeTaskHandler);
onetimeTaskHandlerA = new TestTasks.CountingHandler<>();
onetimeTaskHandlerB = new TestTasks.CountingHandler<>();
oneTimeTaskA = TestTasks.oneTime("OneTimeA", Void.class, onetimeTaskHandlerA);
oneTimeTaskB = TestTasks.oneTime("OneTimeB", Void.class, onetimeTaskHandlerB);

scheduleAnother = new ScheduleAnotherTaskHandler<>(oneTimeTask.instance("secondTask"), settableClock.now().plusSeconds(1));
scheduleAnother = new ScheduleAnotherTaskHandler<>(oneTimeTaskA.instance("secondTask"), settableClock.now().plusSeconds(1));
scheduleAnotherTask = TestTasks.oneTime("ScheduleAnotherTask", Void.class, scheduleAnother);

scheduler = TestHelper.createManualScheduler(DB.getDataSource(), oneTimeTask, scheduleAnotherTask).clock(settableClock).start();
scheduler = TestHelper.createManualScheduler(DB.getDataSource(), oneTimeTaskA, oneTimeTaskB, scheduleAnotherTask).clock(settableClock).start();
}

@Test
public void client_should_be_able_to_schedule_executions() {
SchedulerClient client = SchedulerClient.Builder.create(DB.getDataSource()).build();
client.schedule(oneTimeTask.instance("1"), settableClock.now());
client.schedule(oneTimeTaskA.instance("1"), settableClock.now());

scheduler.runAnyDueExecutions();
assertThat(onetimeTaskHandler.timesExecuted, CoreMatchers.is(1));
assertThat(onetimeTaskHandlerA.timesExecuted, CoreMatchers.is(1));
}

@Test
public void should_be_able_to_schedule_other_executions_from_an_executionhandler() {
scheduler.schedule(scheduleAnotherTask.instance("1"), settableClock.now());
scheduler.runAnyDueExecutions();
assertThat(scheduleAnother.timesExecuted, CoreMatchers.is(1));
assertThat(onetimeTaskHandler.timesExecuted, CoreMatchers.is(0));
assertThat(onetimeTaskHandlerA.timesExecuted, CoreMatchers.is(0));

scheduler.tick(ofSeconds(1));
scheduler.runAnyDueExecutions();
assertThat(onetimeTaskHandler.timesExecuted, CoreMatchers.is(1));
assertThat(onetimeTaskHandlerA.timesExecuted, CoreMatchers.is(1));
}

@Test
public void client_should_be_able_to_fetch_executions_for_task() {
SchedulerClient client = SchedulerClient.Builder.create(DB.getDataSource(), oneTimeTaskA, oneTimeTaskB).build();
client.schedule(oneTimeTaskA.instance("1"), settableClock.now());
client.schedule(oneTimeTaskA.instance("2"), settableClock.now());
client.schedule(oneTimeTaskB.instance("10"), settableClock.now());
client.schedule(oneTimeTaskB.instance("11"), settableClock.now());
client.schedule(oneTimeTaskB.instance("12"), settableClock.now());

assertThat(countAllExecutions(client), is(5));
assertThat(countExecutionsForTask(client, oneTimeTaskA.getName(), Void.class), is(2));
assertThat(countExecutionsForTask(client, oneTimeTaskB.getName(), Void.class), is(3));
}

private int countAllExecutions(SchedulerClient client) {
AtomicInteger counter = new AtomicInteger(0);
client.getScheduledExecutions((ScheduledExecution<Object> execution) -> {counter.incrementAndGet();});
return counter.get();
}

private <T> int countExecutionsForTask(SchedulerClient client, String taskName, Class<T> dataClass) {
AtomicInteger counter = new AtomicInteger(0);
client.getScheduledExecutionsForTask(taskName, dataClass, (ScheduledExecution<T> execution) -> {counter.incrementAndGet();});
return counter.get();
}


public static class ScheduleAnotherTaskHandler<T> implements ExecutionHandlerWithExternalCompletion<T> {
public int timesExecuted = 0;
private final TaskInstance<Void> secondTask;
Expand Down
2 changes: 2 additions & 0 deletions todo.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

Todo?
---------------
- implement a Optional<ScheduledExecution> SchedulerClient.get(taskInstance) method for getting a single executions

- double check DeadExecutionsTest is working properly

- proper async test for when you enableImmediateExecution
Expand Down

0 comments on commit bf800b4

Please sign in to comment.