Commit 6b1ef83a by Michael Schmid

Added new unit tests

parent 5e54ea41
......@@ -37,6 +37,8 @@ public class JobContext implements JobContextInterface {
currentProcessor.get().setJob(null);
currentProcessor = Optional.empty();
return taskContext.acceptNotification(time);
} else if (executionTime < 0) {
throw new RuntimeException("Job was executed for longer than its WCET!");
}
return Optional.empty();
......
......@@ -44,6 +44,8 @@ public class TaskletContext {
}
currentJob = Optional.empty();
return true;
} else if (executionTime < 0) {
throw new RuntimeException("Tasklet was executed for longer than its WCET!");
}
return false;
}
......
package mvd.jester.simulator;
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.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.AdditionalMatchers.gt;
import static org.mockito.AdditionalMatchers.lt;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import mvd.jester.model.Task;
import mvd.jester.simulator.internals.ProcessorContext;
import mvd.jester.simulator.internals.TaskContextInterface;
import mvd.jester.simulator.internals.maiabertogna.JobContext;
import mvd.jester.simulator.internals.maiabertogna.TaskContext;
/**
* TestProcessorContext
*/
public class TestProcessorContext {
@Test
@DisplayName("Check if the job execution is updated correctly.")
void testUpdateExecution() {
for (int run = 0; run < 100; ++run) {
long jobWcet = ThreadLocalRandom.current().nextLong(1, 10);
TaskContext task = mock(TaskContext.class);
JobContext job = mock(JobContext.class);
when(job.updateExecution(lt(jobWcet))).thenReturn(Optional.empty());
when(job.updateExecution(jobWcet)).thenReturn(Optional.of(task));
when(job.updateExecution(gt(jobWcet))).thenThrow(RuntimeException.class);
ProcessorContext processor = new ProcessorContext(1);
assertFalse(processor.updateExecution(0).isPresent());
processor.setJob(job);
for (int i = 0; i < jobWcet; ++i) {
assertFalse(processor.updateExecution(i).isPresent());
}
Optional<TaskContextInterface> tci = processor.updateExecution(jobWcet);
assertTrue(tci.isPresent());
assertTrue(tci.get().equals(task));
assertThrows(RuntimeException.class, () -> processor.updateExecution(jobWcet + 1));
verify(job, times((int) jobWcet + 2)).updateExecution(anyLong());
}
}
@Test
@DisplayName("Check if the processor correctly accepts jobs")
void testAcceptTask() {
for (int run = 0; run < 100; ++run) {
long firstPeriod = ThreadLocalRandom.current().nextLong(100, 1000);
long secondPeriod = ThreadLocalRandom.current().nextLong(100, 1000);
JobContext firstJob = mock(JobContext.class);
TaskContext firstTaskContext = mock(TaskContext.class);
Task firstTask = mock(Task.class);
when(firstJob.getTaskContext()).thenReturn(firstTaskContext);
when(firstJob.prepareJob(anyLong())).thenReturn(true);
when(firstTaskContext.getTask()).thenReturn(firstTask);
when(firstTaskContext.getNextJob()).thenReturn(Optional.of(firstJob));
when(firstTask.getPeriod()).thenReturn((long) firstPeriod);
JobContext secondJob = mock(JobContext.class);
TaskContext secondTaskContext = mock(TaskContext.class);
Task secondTask = mock(Task.class);
when(secondJob.getTaskContext()).thenReturn(secondTaskContext);
when(secondJob.prepareJob(anyLong())).thenReturn(true);
when(secondTaskContext.getTask()).thenReturn(secondTask);
when(secondTaskContext.getNextJob()).thenReturn(Optional.of(secondJob));
when(secondTask.getPeriod()).thenReturn((long) secondPeriod);
ProcessorContext processor = new ProcessorContext(1);
assertTrue(processor.acceptTask(secondTaskContext, run));
if (firstPeriod < secondPeriod) {
assertTrue(processor.acceptTask(firstTaskContext, run));
} else {
assertFalse(processor.acceptTask(firstTaskContext, run));
}
assertFalse(processor.acceptTask(secondTaskContext, run));
int time = firstPeriod < secondPeriod ? 1 : 0;
verify(firstJob, times(time)).prepareJob(anyLong());
verify(firstJob, times(time)).setCurrentProcessor(processor);
verify(firstTaskContext, times(time)).getNextJob();
verify(secondJob, times(1)).prepareJob(anyLong());
verify(secondJob, times(1)).setCurrentProcessor(processor);
verify(secondJob, times(time)).setCurrentProcessor(null);
verify(secondTaskContext, times(1)).getNextJob();
}
}
}
package mvd.jester.simulator.maiabertogna;
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.anyLong;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
......@@ -25,19 +28,18 @@ public class TestJobContext {
@DisplayName("Check if execution of Job is updated correctly.")
public void testUpdateExecution() {
for (int run = 0; run < 100; ++run) {
long numberOfJobs = ThreadLocalRandom.current().nextLong(1, 10);
long jobWcet = ThreadLocalRandom.current().nextLong(20, 50);
Segment s = new Segment(jobWcet, numberOfJobs);
Segment s = mock(Segment.class);
when(s.getJobWcet()).thenReturn(jobWcet);
SegmentContext sc = mock(SegmentContext.class);
when(sc.getSegment()).thenReturn(s);
TaskContext tc = mock(TaskContext.class);
when(tc.acceptNotification(anyLong())).thenReturn(Optional.of(tc));
JobContext job = new JobContext(tc, sc);
ProcessorContext p = new ProcessorContext(0);
ProcessorContext p = mock(ProcessorContext.class);
job.setCurrentProcessor(p);
p.setJob(job);
assertTrue(job.checkExecutionTime());
......@@ -51,6 +53,9 @@ public class TestJobContext {
assertFalse(p.getJob().isPresent());
assertFalse(job.getCurrentProcessor().isPresent());
assertFalse(job.checkExecutionTime());
verify(p, times(1)).setJob(null);
assertThrows(RuntimeException.class, () -> job.updateExecution(1));
}
}
}
package mvd.jester.simulator.schmidmottok;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.AdditionalMatchers.not;
import static org.mockito.AdditionalMatchers.gt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Optional;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import mvd.jester.model.Segment;
import mvd.jester.simulator.internals.ProcessorContext;
import mvd.jester.simulator.internals.schmidmottok.JobContext;
import mvd.jester.simulator.internals.schmidmottok.SegmentContext;
import mvd.jester.simulator.internals.schmidmottok.TaskContext;
import mvd.jester.simulator.internals.schmidmottok.TaskletContext;;
/**
* TestJobContext
......@@ -12,7 +28,72 @@ public class TestJobContext {
@Test
@DisplayName("Check if the job is prepared correctly.")
public void testPrepareJob() {
assertTrue(true);
TaskContext tc = mock(TaskContext.class);
TaskletContext tlc = mock(TaskletContext.class);
SegmentContext sc = mock(SegmentContext.class);
Segment s = mock(Segment.class);
when(sc.getSegment()).thenReturn(s);
when(s.getJobWcet()).thenReturn((long) 20);
when(sc.getNextTasklet()).thenReturn(Optional.of(tlc))
.thenReturn(Optional.ofNullable(null));
JobContext jc = new JobContext(tc, sc);
assertTrue(jc.prepareJob(0));
assertTrue(jc.prepareJob(1));
jc.setCurrentTasklet(null);
assertFalse(jc.prepareJob(2));
}
@Test
@DisplayName("Check if the execution time is checked correctly.")
void testCheckExecutionTime() {
TaskContext tc = mock(TaskContext.class);
TaskletContext tlc = mock(TaskletContext.class);
SegmentContext sc = mock(SegmentContext.class);
Segment s = mock(Segment.class);
when(sc.getSegment()).thenReturn(s);
when(s.getJobWcet()).thenReturn((long) 20);
when(tlc.checkExecutionTime()).thenReturn(true, false);
JobContext jc = new JobContext(tc, sc);
assertFalse(jc.checkExecutionTime());
jc.setCurrentTasklet(tlc);
assertTrue(jc.checkExecutionTime());
assertFalse(jc.checkExecutionTime());
}
@Test
@DisplayName("Check if the execution is updated correctly.")
void checkUpdateExecution() {
TaskContext tc = mock(TaskContext.class);
TaskletContext tlc = mock(TaskletContext.class);
SegmentContext sc = mock(SegmentContext.class);
Segment s = mock(Segment.class);
ProcessorContext pc = mock(ProcessorContext.class);
when(sc.getSegment()).thenReturn(s);
when(sc.getNextTasklet()).thenReturn(Optional.ofNullable(tlc))
.thenReturn(Optional.ofNullable(null));
when(s.getJobWcet()).thenReturn((long) 20);
when(tlc.checkExecutionTime()).thenReturn(true, false);
when(tlc.updateExecution(not(eq(0)))).thenReturn(true);
when(tlc.updateExecution(0)).thenReturn(false);
when(tc.acceptNotification(not(eq(1)))).thenReturn(Optional.ofNullable(null));
when(tc.acceptNotification(1)).thenReturn(Optional.ofNullable(tc));
JobContext jc = new JobContext(tc, sc);
jc.setCurrentProcessor(pc);
assertFalse(jc.updateExecution(0).isPresent());
assertFalse(jc.updateExecution(0).isPresent());
jc.setCurrentTasklet(tlc);
assertTrue(jc.updateExecution(1).isPresent());
verify(tlc, times(2)).updateExecution(anyLong());
verify(tc, times(2)).acceptNotification(anyLong());
verify(sc, times(2)).getNextTasklet();
}
}
......@@ -80,10 +80,9 @@ public class TestSegmentContext {
SegmentContext sc = new SegmentContext(tc, s, 4);
for (int i = 0; i < jobWcet; ++i) {
Optional<TaskletContext> tci = sc.getNextTasklet();
assertTrue(tci.isPresent());
tci.get().setCurrentJob(new JobContext(tc, sc));
tci.get().setCurrentJob(mock(JobContext.class));
}
assertFalse(sc.getNextTasklet().isPresent());
......@@ -93,7 +92,7 @@ public class TestSegmentContext {
for (int i = 0; i < jobWcet; ++i) {
Optional<TaskletContext> tasklet = sc.getNextTasklet();
assertTrue(tasklet.isPresent());
tasklet.get().setCurrentJob(new JobContext(tc, sc));
tasklet.get().setCurrentJob(mock(JobContext.class));
for (int j = 0; j < numberOfJobs; ++j) {
tasklet.get().updateExecution(j);
}
......
......@@ -58,8 +58,7 @@ public class TestTaskContext {
@DisplayName("Check if the next job is returned correctly.")
public void testGetNextJob() {
for (int run = 0; run < 100; ++run) {
// long numberOfJobs = ThreadLocalRandom.current().nextLong(1, 10);
// long numberOfSegments = ThreadLocalRandom.current().nextLong(3, 10);
long numberOfProcessors = ThreadLocalRandom.current().nextLong(2, 10);
Set<Segment> segments = new LinkedHashSet<>();
segments.add(new Segment(5, 1));
......@@ -67,7 +66,7 @@ public class TestTaskContext {
segments.add(new Segment(15, 1));
Task t = new Task(100, segments);
TaskContext tc = new TaskContext(t, 1, 0);
TaskContext tc = new TaskContext(t, numberOfProcessors, 0);
Optional<JobContextInterface> job = tc.getNextJob();
assertTrue(job.isPresent());
......@@ -76,19 +75,25 @@ public class TestTaskContext {
tc.acceptNotification(0);
for (int i = 0; i < 9; ++i) {
for (int i = 0; i < numberOfProcessors - 1; ++i) {
job = tc.getNextJob();
assertTrue(job.isPresent());
job.get().setCurrentProcessor(new ProcessorContext(i));
tc.acceptNotification(0);
}
job = tc.getNextJob();
assertTrue(job.isPresent());
job.get().setCurrentProcessor(new ProcessorContext(20));
job.get().setCurrentProcessor(new ProcessorContext(10));
assertFalse(tc.getNextJob().isPresent());
job.get().setCurrentProcessor(null);
assertTrue(tc.getNextJob().isPresent());
job.get().setCurrentProcessor(new ProcessorContext(10));
assertFalse(tc.getNextJob().isPresent());
for (int i = 0; i < 10; ++i) {
tc.acceptNotification(0);
}
assertTrue(tc.getNextJob().isPresent());
}
}
......
package mvd.jester.simulator.schmidmottok;
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.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.concurrent.ThreadLocalRandom;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import mvd.jester.model.Segment;
import mvd.jester.simulator.internals.schmidmottok.JobContext;
import mvd.jester.simulator.internals.schmidmottok.SegmentContext;
import mvd.jester.simulator.internals.schmidmottok.TaskContext;
import mvd.jester.simulator.internals.schmidmottok.TaskletContext;
/**
* TestTaskletContext
*/
public class TestTaskletContext {
@Test
@DisplayName("Check if the execution of the tasklet is updated correctly.")
void testUpdateExecution() {
for (int run = 0; run < 100; ++run) {
long taskletWcet = ThreadLocalRandom.current().nextLong(20, 50);
Segment s = mock(Segment.class);
when(s.getTaskletWcet()).thenReturn(taskletWcet);
SegmentContext sc = mock(SegmentContext.class);
when(sc.getSegment()).thenReturn(s);
TaskContext tc = mock(TaskContext.class);
JobContext jc = mock(JobContext.class);
TaskletContext tasklet = new TaskletContext(tc, sc);
tasklet.setCurrentJob(jc);
assertTrue(tasklet.checkExecutionTime());
for (int i = 0; i < taskletWcet - 1; ++i) {
assertFalse(tasklet.updateExecution(i));
}
assertTrue(tasklet.updateExecution(taskletWcet));
assertFalse(tasklet.getCurrentJob().isPresent());
assertFalse(tasklet.checkExecutionTime());
verify(jc, times(1)).setCurrentTasklet(null);
assertThrows(RuntimeException.class, () -> tasklet.updateExecution(1));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment