@Step("I call a 1 second to run step method with passes throughout 1 second annotation") @PassesWithin(length=1, timeUnit = TimeUnit.SECONDS, pollMode = PollMode.PASS_THROUGHOUT_PERIOD, pollFrequencyInMilliseconds = 100) public void callATenSecondRunningMethodWithPassThroughout() { longMethodStartTime = System.currentTimeMillis(); try { Thread.sleep(1000); } catch (InterruptedException e) { } }
if ( a.annotationType() == PassesWithin.class) { PassesWithin passesWithin = (PassesWithin) a; switch(passesWithin.pollMode()) { case UNTIL_FIRST_PASS: simpleMethodInvoker = new UntilFirstPassInvoker( simpleMethodInvoker, passesWithin.length(), passesWithin.timeUnit(), passesWithin.pollFrequencyInMilliseconds() ); break; simpleMethodInvoker = new PassesThroughoutInvoker( simpleMethodInvoker, passesWithin.length(), passesWithin.timeUnit(), passesWithin.pollFrequencyInMilliseconds() ); break; default: throw new UnsupportedOperationException("Unknown mode " + passesWithin.pollMode());
@Step(".* test condition fails with Exception") @PassesWithin(length = 200, timeUnit = TimeUnit.MILLISECONDS, pollMode = PollMode.PASS_THROUGHOUT_PERIOD) public void testFailsWithException() throws Exception { throw new Exception("My Exception Message"); }
@Step(".* test condition fails with RuntimeException") @PassesWithin(length = 200, timeUnit = TimeUnit.MILLISECONDS, pollMode = PollMode.PASS_THROUGHOUT_PERIOD) public void testFailsWithRuntimeException() throws Exception { throw new RuntimeException("My Runtime Exception Message"); }
@Step("I call a 1 second to run step method with passes within 1 second annotation") @PassesWithin(length=1, timeUnit = TimeUnit.SECONDS, pollFrequencyInMilliseconds = 100) public void callATenSecondRunningMethod() { longMethodStartTime = System.currentTimeMillis(); try { Thread.sleep(1000); } catch (InterruptedException e) { } //this will cause the test to pass if the step method is polled again //we actually expect it to fail and check for this failure in the output, since after the first run we will //already be over the allotted 1 second time limit and so the step should not get called again if ( System.currentTimeMillis() - longMethodStartTime < 1100) { throw new AssertionError("Whoops"); } }
@Step(".*call a passes within step method it can be terminated immediately by FailImmediatelyException") @PassesWithin(length=360, timeUnit = TimeUnit.SECONDS) public void testFailImmediately() { passesWithinStartTime.compareAndSet(0, System.currentTimeMillis()); long zeroWhenFailingImmediately = (System.currentTimeMillis() - passesWithinStartTime.get()) / 1000; throw new FailImmediatelyException("Fail this step immediately - time elapsed " + zeroWhenFailingImmediately + " seconds"); }
@Step(".*call a passes within step method remotely it can be terminated immediately by FailImmediatelyException") @PassesWithin(length=360, timeUnit = TimeUnit.SECONDS) public void testFailImmediately() { passesWithinStartTime.compareAndSet(0, System.currentTimeMillis()); long zeroWhenFailingImmediately = (System.currentTimeMillis() - passesWithinStartTime.get()) / 1000; throw new FailImmediatelyException("Fail this step immediately - time elapsed " + zeroWhenFailingImmediately + " seconds"); }
@Step("the value is not (\\d) within 0.2 seconds so this step should fail") @PassesWithin(length = 200, timeUnit = TimeUnit.MILLISECONDS, pollFrequencyInMilliseconds = 50) public void passesWithinPointTwoSeconds(int expectCount) { assertEquals("Expect " + expectCount, expectCount, timeCount); }
@Step("the value is (\\d) for half a second") @PassesWithin(length = 500, timeUnit = TimeUnit.MILLISECONDS, pollFrequencyInMilliseconds = 50, pollMode = PollMode.PASS_THROUGHOUT_PERIOD) public void passesForHalfASecond(int expectCount) { passesForPollCount++; assertEquals("Expect " + expectCount, expectCount, timeCount); }
@Step(".* test condition fails with AssertionError") @PassesWithin(length = 200, timeUnit = TimeUnit.MILLISECONDS, pollMode = PollMode.PASS_THROUGHOUT_PERIOD) public void testFails() { ChorusAssert.fail("Failed condition"); }
@Step("the value is (\\d) within default period") @PassesWithin() public void passesWithinDefaultSecond(int expectCount) { assertEquals("Expect " + expectCount + " but was " + timeCount, expectCount, timeCount); }
@Step("test condition eventually passes") @PassesWithin(length = 1) public void testCondition() { pollCount++; assertTrue("Trigger was set", trigger); assertTrue("polled several times", pollCount > 1); }
@Step("the value is (\\d) within 2 seconds") @PassesWithin(length = 2) public void passesWithinTwoSeconds(int expectCount) { passesWithinPollCount++; assertEquals("Expect " + expectCount + " but was " + timeCount, expectCount, timeCount); //after 300ms the timer task should set the value, first poll should fail second should pass assertTrue("Expect to have been polled at least 2 times", passesWithinPollCount >= 2 && passesWithinPollCount < 5); }