@Test void shouldConsultEndCondition() throws Throwable { // GIVEN CallCountBooleanSupplier endCondition = new CallCountBooleanSupplier( 100 ); Race race = new Race().withEndCondition( endCondition ); race.addContestants( 20, throwing( () -> sleep( 10 ) ) ); // WHEN race.go(); // THEN assertTrue( endCondition.callCount.get() >= 100 ); }
@Test void shouldBreakOnError() throws Throwable { // GIVEN String error = "Noooo"; Race race = new Race(); race.withEndCondition( () -> false ); // <-- never end race.addContestant( () -> { throw new RuntimeException( error ); } ); race.addContestants( 3, () -> { } ); // WHEN Exception exception = assertThrows( Exception.class, () -> race.go() ); assertEquals( error, exception.getMessage() ); }
@Test void shouldHaveMultipleEndConditions() throws Throwable { // GIVEN ControlledBooleanSupplier endCondition1 = spy( new ControlledBooleanSupplier( false ) ); ControlledBooleanSupplier endCondition2 = spy( new ControlledBooleanSupplier( false ) ); ControlledBooleanSupplier endCondition3 = spy( new ControlledBooleanSupplier( false ) ); Race race = new Race().withEndCondition( endCondition1, endCondition2, endCondition3 ); race.addContestant( () -> endCondition2.set( true ) ); race.addContestants( 3, Runnables.EMPTY_RUNNABLE ); // WHEN race.go(); // THEN verify( endCondition1, atLeast( 4 ) ).getAsBoolean(); verify( endCondition2, atLeast( 4 ) ).getAsBoolean(); }
race.withEndCondition( () -> latch.getCount() == 0 );