diff --git a/src/main/java/mvd/jester/TestEnvironment.java b/src/main/java/mvd/jester/TestEnvironment.java index 3875f08..7315db5 100644 --- a/src/main/java/mvd/jester/TestEnvironment.java +++ b/src/main/java/mvd/jester/TestEnvironment.java @@ -79,28 +79,30 @@ public class TestEnvironment { } public void runTests() { - Set abstractTestInstances = new HashSet<>(); - Set abstractSimulatorInstances = new HashSet<>(); + Set abstractTestInstances = new HashSet<>(); + Set abstractSimulatorInstances = new HashSet<>(); Map totalNumberOfTasks = new HashMap<>(); - Map> testResults = new HashMap<>(); - Map> simulatorResults = - new HashMap<>(); + Table testResults = HashBasedTable.create(); + Table simulatorResults = HashBasedTable.create(); - for (Constructor c : abstractTests) { - try { - abstractTestInstances.add(c.newInstance(this.systemSetup)); - } catch (Exception e) { - System.out.println("Could not instantiate object of AbstractTest!"); + for (PriorityManager pm : schedulingAlgorithms) { + for (Constructor c : abstractTests) { + try { + abstractTestInstances.add(new TestPair(pm, c.newInstance(this.systemSetup))); + } catch (Exception e) { + System.out.println("Could not instantiate object of AbstractTest!"); + } } - } - for (Constructor c : abstractSimulators) { - try { - abstractSimulatorInstances.add(c.newInstance(this.systemSetup)); - } catch (Exception e) { - System.out.println("Could not instantiate object of AbstractSimulator!"); + for (Constructor c : abstractSimulators) { + try { + abstractSimulatorInstances + .add(new SimulatorPair(pm, c.newInstance(this.systemSetup))); + } catch (Exception e) { + System.out.println("Could not instantiate object of AbstractSimulator!"); + } } } @@ -123,36 +125,34 @@ public class TestEnvironment { totalNumberOfTasks.compute(roundedUtilization, (k, v) -> v == null ? 1 : v + 1); - for (PriorityManager priorityManager : schedulingAlgorithms) { - for (AbstractTest test : abstractTestInstances) { - Table table = testResults - .computeIfAbsent(roundedUtilization, k -> HashBasedTable.create()); - if (priorityManager.hasTest(test) - && test.runSchedulabilityCheck(priorityManager)) { - Long val = table.get(priorityManager, test); - if (val == null) { - table.put(priorityManager, test, (long) 1); - } else { - table.put(priorityManager, test, val + 1); - } - } + for (TestPair testInstance : abstractTestInstances) { + PriorityManager priorityManager = testInstance.getPriorityManager(); + AbstractTest abstractTest = testInstance.getAbstractTest(); + if (!testResults.contains(roundedUtilization, testInstance)) { + testResults.put(roundedUtilization, testInstance, (long) 0); } - for (AbstractSimulator simulator : abstractSimulatorInstances) { - Table table = simulatorResults - .computeIfAbsent(roundedUtilization, k -> HashBasedTable.create()); - if (priorityManager.hasSimulator(simulator) - && simulator.runSimulation(priorityManager)) { - Long val = table.get(priorityManager, simulator); - if (val == null) { - table.put(priorityManager, simulator, (long) 1); - } else { - table.put(priorityManager, simulator, val + 1); - } - } + if (priorityManager.hasTest(abstractTest) + && abstractTest.runSchedulabilityCheck(priorityManager)) { + Long val = testResults.get(roundedUtilization, testInstance); + testResults.put(roundedUtilization, testInstance, val + 1); + } + } + + for (SimulatorPair simulatorInstance : abstractSimulatorInstances) { + PriorityManager priorityManager = simulatorInstance.getPriorityManager(); + AbstractSimulator abstractSimulator = simulatorInstance.getAbstractSimulator(); + if (!simulatorResults.contains(roundedUtilization, simulatorInstance)) { + simulatorResults.put(roundedUtilization, simulatorInstance, (long) 0); } + if (priorityManager.hasSimulator(abstractSimulator) + && abstractSimulator.runSimulation(priorityManager)) { + Long val = simulatorResults.get(roundedUtilization, simulatorInstance); + simulatorResults.put(roundedUtilization, simulatorInstance, val + 1); + } } + builder.addTask(systemSetup); utilization = this.systemSetup.getUtilization(); @@ -163,7 +163,7 @@ public class TestEnvironment { logSimulationResults(simulatorResults, totalNumberOfTasks); } - private void logTestResults(Map> results, + private void logTestResults(Table results, Map totalNumberOfTasks) { LocalTime date = LocalTime.now(); Logger log = new Logger("./results/results_" + systemSetup.getNumberOfProcessors() + "_" @@ -171,28 +171,17 @@ public class TestEnvironment { String firstLine = new String("Utilization"); if (!results.isEmpty()) { - Long first = results.keySet().iterator().next(); - - for (PriorityManager p : results.get(first).rowKeySet()) { - for (AbstractTest t : results.get(first).columnKeySet()) { - firstLine = firstLine + "\t" + t.getName() + "_" + p.getName(); - } + for (TestPair testPair : results.columnKeySet()) { + firstLine = firstLine + "\t" + testPair.getName(); } log.log(firstLine); - for (Long util : results.keySet()) { - String line = String.valueOf((double) util / 10); - Table tests = results.get(util); - - for (PriorityManager p : tests.rowKeySet()) { - for (AbstractTest t : tests.columnKeySet()) { - if (tests.contains(p, t)) { - line += "\t" + tests.get(p, t); - } else { - line += "\t" + "0"; - } - } + for (Long util : totalNumberOfTasks.keySet()) { + String line = + String.valueOf((double) util / 10) + "\t" + totalNumberOfTasks.get(util); + for (TestPair simulatorPair : results.columnKeySet()) { + line += "\t" + results.get(util, simulatorPair); } log.log(line); } @@ -201,8 +190,7 @@ public class TestEnvironment { log.finalize(); } - private void logSimulationResults( - Map> results, + private void logSimulationResults(Table results, Map totalNumberOfTasks) { LocalTime date = LocalTime.now(); Logger log = new Logger("./results/results_sim_" + systemSetup.getNumberOfProcessors() + "_" @@ -210,29 +198,16 @@ public class TestEnvironment { String firstLine = new String("Utilization\tTotal"); if (!results.isEmpty()) { - Long first = results.keySet().iterator().next(); - - for (PriorityManager p : results.get(first).rowKeySet()) { - for (AbstractSimulator s : results.get(first).columnKeySet()) { - firstLine = firstLine + "\t" + s.getName() + "_" + p.getName(); - } + for (SimulatorPair simulatorPair : results.columnKeySet()) { + firstLine = firstLine + "\t" + simulatorPair.getName(); } log.log(firstLine); - for (Long util : totalNumberOfTasks.keySet()) { - String line = String.valueOf((double) util / 10); - line += "\t" + totalNumberOfTasks.get(util); - Table tests = results.get(util); - - for (PriorityManager p : tests.rowKeySet()) { - for (AbstractSimulator s : tests.columnKeySet()) { - if (tests.contains(p, s)) { - line += "\t" + tests.get(p, s); - } else { - line += "\t" + "0"; - } - } + for (Long kv : totalNumberOfTasks.keySet()) { + String line = String.valueOf((double) kv / 10) + "\t" + totalNumberOfTasks.get(kv); + for (SimulatorPair simulatorPair : results.columnKeySet()) { + line += "\t" + results.get(kv, simulatorPair); } log.log(line); } @@ -240,4 +215,65 @@ public class TestEnvironment { log.finalize(); } + + + private class SimulatorPair { + private final PriorityManager priorityManager; + private final AbstractSimulator abstractSimulator; + + + public SimulatorPair(PriorityManager priorityManager, AbstractSimulator abstractSimulator) { + this.abstractSimulator = abstractSimulator; + this.priorityManager = priorityManager; + } + + /** + * @return the priorityManager + */ + public PriorityManager getPriorityManager() { + return priorityManager; + } + + /** + * @return the abstractSimulator + */ + public AbstractSimulator getAbstractSimulator() { + return abstractSimulator; + } + + public String getName() { + return abstractSimulator.getName() + "_" + priorityManager.getName(); + } + } + + private class TestPair { + private final PriorityManager priorityManager; + private final AbstractTest abstractTest; + + + public TestPair(PriorityManager priorityManager, AbstractTest abstractTest) { + this.abstractTest = abstractTest; + this.priorityManager = priorityManager; + } + + /** + * @return the priorityManager + */ + public PriorityManager getPriorityManager() { + return priorityManager; + } + + /** + * @return the abstractTest + */ + public AbstractTest getAbstractTest() { + return abstractTest; + } + + public String getName() { + return abstractTest.getName() + "_" + priorityManager.getName(); + } + } } + + diff --git a/src/main/java/mvd/jester/simulator/internals/maiabertogna/JobContext.java b/src/main/java/mvd/jester/simulator/internals/maiabertogna/JobContext.java index 115a17e..05c7d54 100644 --- a/src/main/java/mvd/jester/simulator/internals/maiabertogna/JobContext.java +++ b/src/main/java/mvd/jester/simulator/internals/maiabertogna/JobContext.java @@ -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(); diff --git a/src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskletContext.java b/src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskletContext.java index 1c11e3f..d3c43e9 100644 --- a/src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskletContext.java +++ b/src/main/java/mvd/jester/simulator/internals/schmidmottok/TaskletContext.java @@ -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; } diff --git a/src/test/java/mvd/jester/simulator/TestProcessorContext.java b/src/test/java/mvd/jester/simulator/TestProcessorContext.java new file mode 100644 index 0000000..0a5a00d --- /dev/null +++ b/src/test/java/mvd/jester/simulator/TestProcessorContext.java @@ -0,0 +1,107 @@ +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 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(); + } + } + +} diff --git a/src/test/java/mvd/jester/simulator/maiabertogna/TestJobContext.java b/src/test/java/mvd/jester/simulator/maiabertogna/TestJobContext.java index 446864f..e65fc4c 100644 --- a/src/test/java/mvd/jester/simulator/maiabertogna/TestJobContext.java +++ b/src/test/java/mvd/jester/simulator/maiabertogna/TestJobContext.java @@ -1,9 +1,12 @@ 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)); } } } diff --git a/src/test/java/mvd/jester/simulator/schmidmottok/TestJobContext.java b/src/test/java/mvd/jester/simulator/schmidmottok/TestJobContext.java index 1c832bb..d6a105c 100644 --- a/src/test/java/mvd/jester/simulator/schmidmottok/TestJobContext.java +++ b/src/test/java/mvd/jester/simulator/schmidmottok/TestJobContext.java @@ -1,8 +1,24 @@ 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(); + } } diff --git a/src/test/java/mvd/jester/simulator/schmidmottok/TestSegmentContext.java b/src/test/java/mvd/jester/simulator/schmidmottok/TestSegmentContext.java index 8e4a7a3..3170520 100644 --- a/src/test/java/mvd/jester/simulator/schmidmottok/TestSegmentContext.java +++ b/src/test/java/mvd/jester/simulator/schmidmottok/TestSegmentContext.java @@ -80,10 +80,9 @@ public class TestSegmentContext { SegmentContext sc = new SegmentContext(tc, s, 4); for (int i = 0; i < jobWcet; ++i) { - Optional 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 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); } diff --git a/src/test/java/mvd/jester/simulator/schmidmottok/TestTaskContext.java b/src/test/java/mvd/jester/simulator/schmidmottok/TestTaskContext.java index 88210a6..badc60c 100644 --- a/src/test/java/mvd/jester/simulator/schmidmottok/TestTaskContext.java +++ b/src/test/java/mvd/jester/simulator/schmidmottok/TestTaskContext.java @@ -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 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 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)); - assertFalse(tc.getNextJob().isPresent()); + job.get().setCurrentProcessor(new ProcessorContext(10)); - tc.acceptNotification(0); + 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()); } } diff --git a/src/test/java/mvd/jester/simulator/schmidmottok/TestTaskletContext.java b/src/test/java/mvd/jester/simulator/schmidmottok/TestTaskletContext.java new file mode 100644 index 0000000..41b5205 --- /dev/null +++ b/src/test/java/mvd/jester/simulator/schmidmottok/TestTaskletContext.java @@ -0,0 +1,53 @@ +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)); + } + } +}