@Test void shouldWaitForAllContestantsToComplete() throws Throwable { // GIVEN Race race = new Race(); final AtomicInteger completed = new AtomicInteger(); int count = 5; race.addContestants( count, throwing( () -> { sleep( current().nextInt( 100 ) ); completed.incrementAndGet(); } ) ); // WHEN race.go(); // THEN assertEquals( count, completed.get() ); }
return throwing( () ->
@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 ); }
private Runnable updater( AtomicReferenceArray<List<? extends IndexEntryUpdate<?>>> lastBatches, CountDownLatch insertersDone, ReadWriteLock updateLock, Collection<IndexEntryUpdate<?>> updates ) return throwing( () ->
@Test public void shouldHandleMultipleConcurrentStoreCopyRequests() throws Throwable { // GIVEN Race race = new Race(); CountingAction action = new CountingAction(); int threads = Runtime.getRuntime().availableProcessors() * 10; race.addContestants( threads, throwing( () -> { parkARandomWhile(); try ( Resource lock = mutex.storeCopy( action ) ) { parkARandomWhile(); } } ) ); race.go(); // THEN // It's hard to make predictions about what should have been seen. Most importantly is that // The lock doesn't hang any requests and that number of calls to the action less than number of threads assertThat( action.count(), lessThan( threads ) ); }
private void shouldHandleIndexDropConcurrentlyWithOperation( NodeOperation operation ) throws Throwable { // given long[] nodes = createNodes(); IndexDefinition indexDefinition = createIndex(); // when Race race = new Race(); race.addContestant( () -> { try ( Transaction tx = db.beginTx() ) { indexDefinition.drop(); tx.success(); } }, 1 ); for ( int i = 0; i < NODES; i++ ) { final long nodeId = nodes[i]; race.addContestant( throwing( () -> { try ( Transaction tx = db.beginTx() ) { operation.run( nodeId ); tx.success(); } } ) ); } // then race.go(); }