/** * Wrapper around {@link KillSwitchHandler#checkTimeout()} to simplify instrumentation * @throws RuntimeException */ public static void killIfTimeout() throws RuntimeException { getInstance().checkTimeout(); } }
/** * Try to kill (and then join) the SUT threads. Killing the SUT threads is * important, because some spawn threads could just wait on objects/locks, * and so make the test case executions always last TIMEOUT ms. */ public void killAndJoinClientThreads() throws IllegalStateException { threadStopper.killAndJoinClientThreads(); }
/** * <p> * After the test case is executed, if any SUT thread is still running, we * will wait for their termination. To identify which thread belong to SUT, * before test case execution we should check which are the threads that are * running. * </p> * <p> * WARNING: The sandbox might prevent accessing thread informations, so best * to call this method from outside this class * </p> */ public void storeCurrentThreads() { threadStopper.storeCurrentThreads(); }
@Test public void testTimer() throws InterruptedException{ ThreadStopper stopper = new ThreadStopper(KillSwitchHandler.getInstance(), 1000); stopper.storeCurrentThreads(); stopper.startRecordingTime(); stopper.killAndJoinClientThreads(); Assert.assertTrue(isThreadRunning(TimerClass.NAME)); stopper.storeCurrentThreads(); stopper.startRecordingTime(); stopper.killAndJoinClientThreads(); Assert.assertFalse(isThreadRunning(TimerClass.NAME));
@Before public void initTestCase(){ threadStopper.storeCurrentThreads(); threadStopper.startRecordingTime(); org.evosuite.runtime.jvm.ShutdownHookHandler.getInstance().initHandler(); org.evosuite.runtime.sandbox.Sandbox.goingToExecuteSUTCode(); org.evosuite.runtime.GuiSupport.setHeadless(); org.evosuite.runtime.Runtime.getInstance().resetRuntime(); org.evosuite.runtime.agent.InstrumentingAgent.activate(); }
@After public void resetValues() { RuntimeSettings.mockJVMNonDeterminism = replaceCalls; KillSwitchHandler.getInstance().setKillSwitch(false); }
threadStopper.startRecordingTime(); result.setExecutionTime(System.currentTimeMillis() - threadStopper.getStartTime()); result.setExecutedStatements(num.get()); result.setThrownExceptions(exceptionsThrown);
private static synchronized void registerTimer(Timer timer){ if(MockFramework.isEnabled()) { try{ ThreadCounter.getInstance().checkIfCanStartNewThread(); } catch(RuntimeException e) { timer.cancel(); } } instances.add(timer); }
/** * <p> * Constructor for TestRunnable. * </p> * * @param tc * a {@link org.evosuite.testcase.TestCase} object. * @param scope * a {@link org.evosuite.testcase.execution.Scope} object. * @param observers * a {@link java.util.Set} object. */ public TestRunnable(TestCase tc, Scope scope, Set<ExecutionObserver> observers) { test = tc; this.scope = scope; this.observers = observers; runFinished = false; KillSwitch killSwitch = new KillSwitch() { @Override public void setKillSwitch(boolean kill) { ExecutionTracer.setKillSwitch(kill); } }; Set<String> threadsToIgnore = new LinkedHashSet<>(); threadsToIgnore.add(TestCaseExecutor.TEST_EXECUTION_THREAD); threadsToIgnore.addAll(Arrays.asList(Properties.IGNORE_THREADS)); threadStopper = new ThreadStopper(killSwitch, threadsToIgnore, Properties.TIMEOUT); }
System.resetRuntime(); MockThread.reset(); ThreadCounter.getInstance().resetSingleton(); MockTimeZone.reset(); MockLocale.reset();
private ThreadCounter(){ resetSingleton(); }
killSwitch.setKillSwitch(true); killSwitch.setKillSwitch(false);
@Before public void storeValues() { KillSwitchHandler.getInstance().setKillSwitch(false); RuntimeSettings.mockJVMNonDeterminism = true; MethodCallReplacementCache.resetSingleton(); Runtime.getInstance().resetRuntime(); }
@After public void doneWithTestCase(){ threadStopper.killAndJoinClientThreads(); org.evosuite.runtime.jvm.ShutdownHookHandler.getInstance().safeExecuteAddedHooks(); org.evosuite.runtime.classhandling.JDKClassResetter.reset(); org.evosuite.runtime.sandbox.Sandbox.doneWithExecutingSUTCode(); org.evosuite.runtime.agent.InstrumentingAgent.deactivate(); org.evosuite.runtime.GuiSupport.restoreHeadlessMode(); }
@Override @EvoSuiteExclude public synchronized void start() { if(!MockFramework.isEnabled()) { super.start(); return; } if(!isSutRelated()) { //no point in starting those 3rd party threads return; } ThreadCounter.getInstance().checkIfCanStartNewThread(); /* TODO: we could rather buffer them like we did for hooks, and execute them at the end. We could even have methods in the test-cluster to execute some of those threads till they yield within the test case */ super.start(); }
Assert.assertTrue(t.isAlive()); KillSwitchHandler.getInstance().setKillSwitch(true); t.interrupt(); t.join(500);