Simulation implements Runnable, TaskListener, Cancellable { @Override public void run() { running.set(true); testCaseTasks.clear(); progressContributor.start(testCaseCount); finishedCount = new AtomicInteger(0); for (int i = 0; i < 5; i++) { try { SupplyChain copy = deepCopy(supplyChain); scPool.offer(copy); } catch (IOException | ClassNotFoundException ex) { Exceptions.printStackTrace(ex); } } if (scPool.isEmpty()) { return; } int i = 0; while (i < testCaseCount && running.get()) { SupplyChain sc; synchronized (scPool) { while (scPool.isEmpty()) { try { scPool.wait(); } catch (InterruptedException ex) { System.out.println("Simulation thread interrupted while waiting for supply chain. Keep waiting."); } } sc = scPool.poll(); if (sc == null) { continue; } } TestCase testCase = new TestCase(sc); Task task = SimulationExecutor.post(testCase); task.addTaskListener(this); testCaseTasks.put(task, testCase); i++; task.schedule(0); } } Simulation(SupplyChain supplyChain, int testCaseCount, String id); boolean isDone(); double getProgress(); SimulationResult getResult(); ProgressContributor getProgressContributor(); @Override void run(); @Override void taskFinished(org.openide.util.Task task); @Override boolean cancel();  }
@Test public void testRun() { simulation.run(); while (simulation.getProgress() != 1.0d) { } SimulationResult result = simulation.getResult(); assertTrue(result.getSupplyChain().equals(supplyChain)); assertEquals(NUMBER_OF_SIMULATIONS, result.getTestCaseCount(), 4); assertTrue("Two events always happen.", result.getExecutedEvents().size() >= 2); assertTrue("At least 4 events can happen.", result.getExecutedEvents().size() <= 4); assertTrue("Two events always happen.", result.getEventExecutionRate().size() >= 2); assertTrue("At least 4 events can happen.", result.getEventExecutionRate().size() <= 4); assertTrue(result.getCostsByCategory().containsKey(MANDATORY)); assertTrue("Min 5000 based on procedures.", 5000 <= result.getTotalCosts().getMin()); assertTrue("Max 7000 based on procedures.", 7000 >= result.getTotalCosts().getMax()); assertTrue(result.getExtraCostsByCategory().containsKey(MANDATORY)); assertTrue(result.getExtraCostsByCategory().containsKey("always")); assertTrue(result.getExtraCostsByCategory().containsKey("if too late")); assertTrue("Min 3000 based on events.", 3000 <= result.getTotalExtraCosts().getMin()); assertTrue("Max 4000 based on events.", 4000 >= result.getTotalExtraCosts().getMax()); assertTrue(result.getDelaysByCategory().containsKey(MANDATORY)); assertTrue(result.getDelaysByCategory().containsKey("always")); assertTrue(result.getDelaysByCategory().containsKey("if too late")); assertTrue("No delay can happen.", 0 <= result.getTotalDelays().getMin()); assertTrue("Up to 4 hours can happen.", 4 * 60 >= result.getTotalDelays().getMax()); assertTrue(result.getLeadTimesByCategory().containsKey(MANDATORY)); assertTrue("Min 5 hours lead time.", 5 * 60 <= result.getTotalLeadTimes().getMin()); assertTrue("Max 7 hours lead time.", 7 * 60 >= result.getTotalLeadTimes().getMax()); }