Skip to content

Commit

Permalink
fixup! Create JobKitWatchdog, clean SpoolerTest and BackgroundService…
Browse files Browse the repository at this point in the history
…EventTest #140
  • Loading branch information
hdsdi3g committed Jun 29, 2023
1 parent 8c9ed0a commit 8443547
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ public void run() {

}

// TODO add back to ok alert event ! + supervisable

void shutdown() {
shutdown.set(true);
log.debug("Close JobKitWatchDog");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,71 +16,191 @@
*/
package tv.hd3g.jobkit.engine;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
import static org.mockito.MockitoAnnotations.openMocks;

import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.Mock;

import net.datafaker.Faker;
import tv.hd3g.jobkit.engine.watchdog.JobWatchdogPolicy;
import tv.hd3g.jobkit.engine.watchdog.JobWatchdogPolicyWarning;
import tv.hd3g.jobkit.engine.watchdog.WatchableBackgroundService;
import tv.hd3g.jobkit.engine.watchdog.WatchableSpoolJobState;

class JobKitWatchdogTest {

static Faker faker = net.datafaker.Faker.instance();
private static Faker faker = net.datafaker.Faker.instance();

JobKitWatchdog w;

FlatScheduledExecutorService sch;
String serviceName;
String spoolName;
long timedInterval;
String commandName;
long startedDate;
long createdIndex;

@Mock
ScheduledExecutorService sch;
@Mock
SupervisableEvents supervisableEvents;
@Mock
JobWatchdogPolicy policy;

/*
private final List<> policies;
*/
@Mock
WatchableSpoolJob job;
@Mock
WatchableSpoolJob waitJob;
@Mock
SpoolExecutor spoolExecutor;
@Mock
Optional<StackTraceElement> creator;
@Captor
ArgumentCaptor<Runnable> run;
@Captor
ArgumentCaptor<WatchableSpoolJobState> activeJobCaptor;
@Captor
ArgumentCaptor<Set<WatchableSpoolJobState>> queuedJobsCaptor;
@Captor
ArgumentCaptor<Set<WatchableBackgroundService>> relativeBackgroundServicesCaptor;

@BeforeEach
void init() throws Exception {
openMocks(this).close();
sch = new FlatScheduledExecutorService();
// sch = new FlatScheduledExecutorService();
w = new JobKitWatchdog(supervisableEvents, sch);
serviceName = faker.numerify("serviceName###");
spoolName = faker.numerify("spoolName###");
timedInterval = faker.random().nextLong(1000, 100000);
when(policy.getDescription()).thenReturn(faker.numerify("description###"));

commandName = faker.numerify("commandName###");
createdIndex = faker.random().nextLong();
startedDate = System.currentTimeMillis();

when(job.getCommandName()).thenReturn(commandName);
when(job.getSpoolName()).thenReturn(spoolName);
when(job.getCreatedIndex()).thenReturn(createdIndex);
when(job.getCreator()).thenReturn(creator);
when(job.getExecutorReferer()).thenReturn(spoolExecutor);

when(waitJob.getCommandName()).thenReturn(commandName);
when(waitJob.getSpoolName()).thenReturn(spoolName);
when(waitJob.getCreatedIndex()).thenReturn(createdIndex + 1l);
when(waitJob.getCreator()).thenReturn(creator);
when(waitJob.getExecutorReferer()).thenReturn(spoolExecutor);

// assertTrue(MockUtil.isMock());
// MockUtil.resetMock(toolRunner);
// @MockBean
// @Captor ArgumentCaptor<>
// Mockito.doThrow(new Exception()).when();
}

@AfterEach
void end() {
verifyNoMoreInteractions(supervisableEvents, policy);
}

@Test
void testAddPolicies() {
verify(policy, atLeast(0)).getDescription();
verifyNoMoreInteractions(sch, supervisableEvents, policy, spoolExecutor, creator);
}

@Test
void testGetPolicies() {
void testAddGetPolicies() {
final var actual = w.getPolicies();
assertEquals(List.of(), actual);
assertThrows(UnsupportedOperationException.class, () -> actual.add(policy));
w.addPolicies(policy);
assertTrue(w.getPolicies().contains(policy));
assertFalse(actual.contains(policy));
}

@Test
void testRefreshBackgroundService() {
}
@Nested
class OkPolicy {// TODO KoPolicy + with services

@BeforeEach
void init() throws Exception {
w.addPolicies(policy);
}

@ParameterizedTest
@ValueSource(booleans = { true, false })
void testRefreshBackgroundService_enabled(final boolean enabled) {// NOSONAR S2699
w.refreshBackgroundService(serviceName, spoolName, enabled, timedInterval);
verify(sch, times(1)).execute(run.capture());
run.getValue().run();
verify(sch, times(1)).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
}

@Test
void testAddJob() {
w.addJob(job);
verify(sch, times(1)).execute(run.capture());
run.getValue().run();
verify(sch, times(1)).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));
}

@Test
void testAddStartJob() throws JobWatchdogPolicyWarning {
w.addJob(job);
w.addJob(waitJob);
w.startJob(job, startedDate);
verify(sch, times(3)).execute(run.capture());
run.getValue().run();
verify(policy, times(1)).isStatusOk(eq(spoolName), activeJobCaptor.capture(), queuedJobsCaptor.capture());

final var activeJob = activeJobCaptor.getValue();
assertEquals(commandName, activeJob.commandName());
assertTrue(activeJob.createdDate() <= startedDate + 100);
assertEquals(createdIndex, activeJob.createdIndex());
assertTrue(activeJob.getRunTime().isPresent());
assertEquals(startedDate, activeJob.startedDate().get());
assertEquals(creator, activeJob.creator());

final var queuedJobs = queuedJobsCaptor.getValue();
assertFalse(queuedJobs.isEmpty());
final var queuedJob = queuedJobs.stream().findFirst().get();
assertEquals(commandName, queuedJob.commandName());
assertTrue(queuedJob.createdDate() <= startedDate + 100);
assertEquals(createdIndex + 1, queuedJob.createdIndex());
assertFalse(queuedJob.getRunTime().isPresent());
assertFalse(queuedJob.startedDate().isPresent());
assertEquals(creator, queuedJob.creator());

verify(sch, times(1)).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));// TODO put/check timeing
}

@Test
void testAddStartEndJob() throws JobWatchdogPolicyWarning {
w.addJob(job);
w.startJob(job, startedDate);
w.endJob(job);

verify(sch, times(3)).execute(run.capture());
run.getValue().run();
verify(sch, times(1)).schedule(any(Runnable.class), anyLong(), any(TimeUnit.class));// TODO put/check timeing
}

@Test
void testAddJob() {
}

@Test
void testStartJob() {
}

@Test
void testEndJob() {
}

@Test
Expand Down

0 comments on commit 8443547

Please sign in to comment.