Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,19 @@ public void onRemove(ProcessDefinition<M, Pid> watchedProcess) throws DaemonExce
}

if (jobletStatusManager.exists(identifier)) {
JobletStatus status;
try {
status = jobletStatusManager.getStatus(identifier);
} catch (Exception e) {
try {
failureCallback.callback(jobletConfig);
} catch (Exception callbackException) {
throw new DaemonException("Failure callback triggered due to an exception in jobletStatusManager. Failure callback, however, also failed.", callbackException);
}
throw e;
}

try {
JobletStatus status = jobletStatusManager.getStatus(identifier);
switch (status) {
case DONE:
LOG.info("Process succeeded - PID: " + watchedProcess.getPid());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package com.liveramp.daemon_lib.utils;

import java.io.IOException;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.liveramp.daemon_lib.JobletCallback;
import com.liveramp.daemon_lib.JobletConfig;
import com.liveramp.daemon_lib.executors.processes.ProcessDefinition;
import com.liveramp.daemon_lib.executors.processes.ProcessMetadata;
import com.liveramp.daemon_lib.tracking.JobletStatus;
import com.liveramp.daemon_lib.tracking.JobletStatusManager;

import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@SuppressWarnings("unchecked")
@RunWith(MockitoJUnitRunner.class)
public class TestJobletProcessHandler {
private final String IDENTIFIER = "id";

@Mock
private JobletCallback successCallback;

@Mock
private JobletCallback failureCallback;

@Mock
private JobletConfigStorage jobletConfigStorage;

@Mock
private JobletStatusManager jobletStatusManager;

@Mock
private ProcessDefinition processDefinition;

@Mock
private ProcessMetadata processMetadata;

@Mock
private JobletConfig jobletConfig;

private JobletProcessHandler jobletProcessHandler;

@Before
public void setup() throws IOException, ClassNotFoundException {
when(processDefinition.getMetadata()).thenReturn(processMetadata);
when(processMetadata.getIdentifier()).thenReturn(IDENTIFIER);

when(jobletConfigStorage.loadConfig(IDENTIFIER)).thenReturn(jobletConfig);

when(jobletStatusManager.exists(IDENTIFIER)).thenReturn(true);

jobletProcessHandler = new JobletProcessHandler<>(successCallback, failureCallback, jobletConfigStorage, jobletStatusManager);
}


@Test
public void testOnRemoveAttemptsSuccessCallbackWhenStatusIsDone() throws DaemonException {
when(jobletStatusManager.getStatus(IDENTIFIER)).thenReturn(JobletStatus.DONE);
jobletProcessHandler.onRemove(processDefinition);
verify(successCallback, times(1)).callback(jobletConfig);
}

@Test
public void testOnRemoveAttemptsFailureCallbackWhenStatusIsInProgress() throws DaemonException {
when(jobletStatusManager.getStatus(IDENTIFIER)).thenReturn(JobletStatus.IN_PROGRESS);
jobletProcessHandler.onRemove(processDefinition);
verify(failureCallback, times(1)).callback(jobletConfig);
}

@Test
public void testOnRemoveAttemptsFailureCallbackOnExceptionThenReThrows() throws DaemonException {
when(jobletStatusManager.getStatus(IDENTIFIER)).thenThrow(new IllegalArgumentException());
try {
jobletProcessHandler.onRemove(processDefinition);
Assert.fail();
} catch (IllegalArgumentException ignored) {

}
verify(failureCallback, times(1)).callback(jobletConfig);
}

@Test
public void testOnRemoveAttemptsFailureCallbackOnExceptionAndStillFails() throws DaemonException {
when(jobletStatusManager.getStatus(IDENTIFIER)).thenThrow(new IllegalArgumentException());
doThrow(new RuntimeException()).when(failureCallback).callback(jobletConfig);
try {
jobletProcessHandler.onRemove(processDefinition);
Assert.fail();
} catch (DaemonException ignored) {

}
}

}