@Override public void close( boolean populationCompletedSuccessfully ) { latch.finish(); } }
@Override public void stop() { latch.finish(); }
public void finishAndWaitForAllToFinish() { finish(); waitForAllToFinish(); }
@Override public void run() { count.incrementAndGet(); latch.waitForAllToStart(); latch.finish(); }
@Override public void handle( String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response ) throws IOException { response.setContentType( "text/plain; charset=utf-8" ); response.setStatus( HttpServletResponse.SC_OK ); PrintWriter out = response.getWriter(); writeBatch( out, firstBatchSize ); out.flush(); latch.start(); innerBarrier.reached(); latch.finish(); writeBatch( out, otherBatchSize ); baseRequest.setHandled(true); }
private void dropIndex( IndexDefinition index, DoubleLatch populationCompletionLatch ) { try ( Transaction tx = db.beginTx() ) { index.drop(); populationCompletionLatch.finish(); tx.success(); } }
latch.finish();
latch.finish(); } );
@Test public void shouldShutDownOnTaskFailureEvenIfOtherTasksArePending() throws Exception { // GIVEN TaskExecutor<Void> executor = new DynamicTaskExecutor<>( 2, 0, 10, PARK, getClass().getSimpleName() ); IOException exception = new IOException( "Test message" ); ControlledTask firstBlockingTask = new ControlledTask(); ControlledTask secondBlockingTask = new ControlledTask(); executor.submit( firstBlockingTask ); executor.submit( secondBlockingTask ); firstBlockingTask.latch.waitForAllToStart(); secondBlockingTask.latch.waitForAllToStart(); FailingTask failingTask = new FailingTask( exception ); executor.submit( failingTask ); ControlledTask thirdBlockingTask = new ControlledTask(); executor.submit( thirdBlockingTask ); // WHEN firstBlockingTask.latch.finish(); failingTask.latch.await(); failingTask.latch.release(); // THEN assertExceptionOnSubmit( executor, exception ); executor.close(); // call would block if the shutdown as part of failure doesn't complete properly secondBlockingTask.latch.finish(); }
@Test public void shouldExecuteTasksInParallel() { // GIVEN TaskExecutor<Void> executor = new DynamicTaskExecutor<>( 2, 0, 5, PARK, getClass().getSimpleName() ); ControlledTask task1 = new ControlledTask(); TestTask task2 = new TestTask(); // WHEN executor.submit( task1 ); task1.latch.waitForAllToStart(); executor.submit( task2 ); //noinspection StatementWithEmptyBody while ( task2.executed == 0 ) { // Busy loop } task1.latch.finish(); //noinspection StatementWithEmptyBody while ( task1.executed == 0 ) { // Busy loop } executor.close(); // THEN assertEquals( 1, task1.executed ); assertEquals( 1, task2.executed ); }
@Test( expected = /* THEN */ IllegalStateException.class ) public void shouldNotDropWhileCreating() throws IOException { // GIVEN final DoubleLatch latch = new DoubleLatch(); final IndexProxy inner = new IndexProxyAdapter() { @Override public void start() { latch.startAndWaitForAllToStartAndFinish(); } }; final IndexProxy outer = newContractCheckingIndexProxy( inner ); // WHEN runInSeparateThread( outer::start ); try { latch.waitForAllToStart(); outer.drop(); } finally { latch.finish(); } }
@Test public void shouldIncrementNumberOfProcessorsWhenRunning() { // GIVEN TaskExecutor<Void> executor = new DynamicTaskExecutor<>( 1, 0, 5, PARK, getClass().getSimpleName() ); ControlledTask task1 = new ControlledTask(); TestTask task2 = new TestTask(); // WHEN executor.submit( task1 ); task1.latch.waitForAllToStart(); executor.submit( task2 ); executor.processors( 1 ); // now at 2 //noinspection StatementWithEmptyBody while ( task2.executed == 0 ) { // With one additional worker, the second task can execute even if task one is still executing } task1.latch.finish(); //noinspection StatementWithEmptyBody while ( task1.executed == 0 ) { // Busy loop } executor.close(); // THEN assertEquals( 1, task1.executed ); assertEquals( 1, task2.executed ); }
@Test( expected = /* THEN */ IllegalStateException.class ) public void shouldNotCloseWhileCreating() throws IOException { // GIVEN final DoubleLatch latch = new DoubleLatch(); final IndexProxy inner = new IndexProxyAdapter() { @Override public void start() { latch.startAndWaitForAllToStartAndFinish(); } }; final IndexProxy outer = newContractCheckingIndexProxy( inner ); // WHEN runInSeparateThread( outer::start ); try { latch.waitForAllToStart(); outer.close(); } finally { latch.finish(); } }
@Test public void shouldProvideUserByUsernameEvenIfMidSetUsers() throws Throwable { // Given FileUserRepository users = new FileUserRepository( fs, authFile, logProvider ); users.create( new User.Builder( "oskar", LegacyCredential.forPassword( "hidden" ) ).build() ); DoubleLatch latch = new DoubleLatch( 2 ); // When Future<Object> setUsers = threading.execute( o -> { users.setUsers( new HangingListSnapshot( latch, 10L, Collections.emptyList() ) ); return null; }, null ); latch.startAndWaitForAllToStart(); // Then assertNotNull( users.getUserByName( "oskar" ) ); latch.finish(); setUsers.get(); }
latch.finish();
@Test public void shouldTimeoutWaitingForIndexToComeOnline() { // given GraphDatabaseService db = rule.getGraphDatabaseAPI(); DoubleLatch latch = provider.installPopulationJobCompletionLatch(); IndexDefinition index; try ( Transaction tx = db.beginTx() ) { index = db.schema().indexFor( Label.label( "Person" ) ).on( "name" ).create(); tx.success(); } latch.waitForAllToStart(); // when try ( Transaction tx = db.beginTx() ) { // then db.schema().awaitIndexOnline( index, 1, TimeUnit.MILLISECONDS ); fail( "Expected IllegalStateException to be thrown" ); } catch ( IllegalStateException e ) { // good assertThat( e.getMessage(), containsString( "come online" ) ); } finally { latch.finish(); } }