private void raceEncode( String[] INPUT, Function<Value, String> encodeFunction ) throws Throwable { Race race = new Race(); for ( String input : INPUT ) { final Value inputValue = Values.of( new String[]{input} ); race.addContestant( () -> { String first = encodeFunction.apply( inputValue ); for ( int i = 0; i < 1000; i++ ) { String encoded = encodeFunction.apply( inputValue ); assertEquals( "Each attempt at encoding should yield the same result. Turns out that first one was '" + first + "', yet another one was '" + encoded + "'", first, encoded ); } } ); } race.go(); }
@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 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 void tryOnce( final GraphDatabaseAPI db, final Node node ) throws Throwable { Race race = new Race().withRandomStartDelays(); race.addContestants( Runtime.getRuntime().availableProcessors(), () -> { try ( Transaction ignored = db.beginTx() ) { assertEquals( relCount, count( node.getRelationships() ) ); } } ); race.go(); }
/** * The test case is basically loads of concurrent CREATE/DELETE NODE or sometimes just CREATE, keeping the created node in an array * for dedicated deleter threads to pick up and delete as fast as they can see them. This concurrently with large creation transactions. */ @Test public void shouldStressIt() throws Throwable { // given Race race = new Race().withMaxDuration( 5, TimeUnit.SECONDS ); AtomicReferenceArray<Node> nodeHeads = new AtomicReferenceArray<>( NUMBER_OF_CREATORS ); for ( int i = 0; i < NUMBER_OF_CREATORS; i++ ) { race.addContestant( creator( nodeHeads, i ) ); } race.addContestants( NUMBER_OF_DELETORS, deleter( nodeHeads ) ); // when race.go(); // then DatabaseLayout dbLayout = db.databaseLayout(); db.shutdownAndKeepStore(); assertTrue( new ConsistencyCheckService().runFullConsistencyCheck( dbLayout, defaults(), NONE, toOutputStream( System.out ), false, new ConsistencyFlags( true, true, true, false ) ).isSuccessful() ); }
@Repeat( times = 10 ) @Test public void shouldCopeWithConcurrentIncrementOfProcessorsAndShutdown() throws Throwable { // GIVEN TaskExecutor<Void> executor = new DynamicTaskExecutor<>( 1, 2, 2, PARK, "test" ); Race race = new Race().withRandomStartDelays(); race.addContestant( executor::close ); race.addContestant( () -> executor.processors( 1 ) ); // WHEN race.go( 10, SECONDS ); // THEN we should be able to do so, there was a recent fix here and before that fix // shutdown() would hang, that's why we wait for 10 seconds here to cap it if there's an issue. }
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(); }
@Test public void incrementAndGetVersionMustBeAtomic() throws Throwable { try ( MetaDataStore store = newMetaDataStore() ) { long initialVersion = store.incrementAndGetVersion(); int threads = Runtime.getRuntime().availableProcessors(); int iterations = 500; Race race = new Race(); race.addContestants( threads, () -> { for ( int i = 0; i < iterations; i++ ) { store.incrementAndGetVersion(); } } ); race.go(); assertThat( store.incrementAndGetVersion(), is( initialVersion + (threads * iterations) + 1 ) ); } }
private void raceContestantsAndVerifyResults( SchemaDescriptor newDescriptor, Runnable aliceWork, Runnable changeConfig, Runnable bobWork ) throws Throwable { race.addContestants( aliceThreads, aliceWork ); race.addContestant( changeConfig ); race.addContestants( bobThreads, bobWork ); race.go(); await( IndexDescriptorFactory.forSchema( newDescriptor, Optional.of( "nodes" ), FulltextIndexProviderFactory.DESCRIPTOR ) ); try ( Transaction tx = db.beginTx() ) { KernelTransaction ktx = kernelTransaction( tx ); ScoreEntityIterator bob = fulltextAdapter.query( ktx, "nodes", "bob" ); List<ScoreEntityIterator.ScoreEntry> list = bob.stream().collect( Collectors.toList() ); try { assertEquals( bobThreads * nodesCreatedPerThread, list.size() ); } catch ( Throwable e ) { StringBuilder sb = new StringBuilder( e.getMessage() ).append( System.lineSeparator() ).append( "Nodes found in query for bob:" ); for ( ScoreEntityIterator.ScoreEntry entry : list ) { sb.append( System.lineSeparator() ).append( "\t" ).append( db.getNodeById( entry.entityId() ) ); } throw e; } ScoreEntityIterator alice = fulltextAdapter.query( ktx, "nodes", "alice" ); assertEquals( 0, alice.stream().count() ); } }
private void assertOnlyOneSucceeds( Runnable lockAction1, Runnable lockAction2 ) throws Throwable { assertUU(); Race race = new Race(); LockContestant c1 = new LockContestant( lockAction1 ); LockContestant c2 = new LockContestant( lockAction2 ); // when race.addContestant( c1 ); race.addContestant( c2 ); race.goAsync(); while ( !(c1.lockAcquired() || c2.lockAcquired()) || !(c1.started() && c2.started()) ) { LockSupport.parkNanos( TimeUnit.MILLISECONDS.toNanos( 1 ) ); } // then Pair<Boolean,Boolean> c1State = c1.state(); Pair<Boolean,Boolean> c2State = c2.state(); assertNotEquals( withState( "Expected exactly one to acquire lock.", c1State, c2State ), c1State.first(), c2State.first() ); assertTrue( withState( "Expected both to be started.", c1State, c2State ), c1State.other() && c2State.other() ); }
public void writeReport(){ Race race = new Race(); System.out.println("Enter the race times (in seconds): "); race.readIntimes(); double timeOne = race.getTimeOne(); double timeTwo = race.getTimeTwo(); double timeThree = race.getTimeThree();
/** * Starts the race and returns without waiting for contestants to complete. * Any exception thrown by contestant will be lost. */ public void goAsync() throws Throwable { asyncExecution = true; go( 0, TimeUnit.MILLISECONDS ); }
@Before public void createRace() { race = new Race(); }
public void addContestants( int count, Runnable contestant, int maxNumberOfRuns ) { for ( int i = 0; i < count; i++ ) { addContestant( contestant, maxNumberOfRuns ); } }
Map<String, Race> data = new TreeMap<String, Race>(); Race race = new Race("10001", new BigDecimal(275.70)); data.put(race.getRaceId(), race); race = new Race("10002", new BigDecimal(267.38)); data.put(race.getRaceId(), race); race = new Race("10003", new BigDecimal(272.00)); data.put(race.getRaceId(), race); race = new Race("10004", new BigDecimal(259.90)); data.put(race.getRaceId(), race);
import javax.swing.*; public class GameFrame extends JFrame { public static void main(String args[]){ new GameFrame(); } public GameFrame(){ super("Graphics with JFrame"); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setSize(800,600); Race game = new Race(); this.getContentPane().add(game); this.setVisible(true); game.run(); } }
this.playerRace = new Race(race); switch (playerRace.getraceName()) { case "Warrior": this.sex = "Male";
public void addContestants( int count, Runnable contestant ) { addContestants( count, contestant, UNLIMITED ); }