private IndexReference createIndexReference( WritableIndexReferenceFactory indexReferenceFactory ) throws Exception { IndexReference indexReference = indexReferenceFactory.createIndexReference( indexIdentifier ); cleanupRule.add( indexReference ); return indexReference; }
@Test public void shouldCleanupAutoCloseable() throws Throwable { // GIVEN CleanupRule rule = new CleanupRule(); AutoCloseable toClose = rule.add( mock( AutoCloseable.class ) ); // WHEN simulateTestExecution( rule ); // THEN verify( toClose ).close(); }
private void simulateTestExecution( CleanupRule rule ) throws Throwable { rule.apply( new Statement() { @Override public void evaluate() { } }, null ).evaluate(); }
public <T> T add( T toClose ) { Class<?> cls = toClose.getClass(); for ( String methodName : COMMON_CLOSE_METHOD_NAMES ) { try { Method method = cls.getMethod( methodName ); method.setAccessible( true ); add( closeable( method, toClose ) ); return toClose; } catch ( NoSuchMethodException e ) { // ignore } catch ( SecurityException e ) { throw new RuntimeException( e ); } } throw new IllegalArgumentException( "No suitable close method found on " + toClose + ", which is a " + cls ); }
private GraphDatabaseService createDatabase() { return cleanup.add( builder.newGraphDatabase() ); }
@Test public void shouldCleanupMultipleObjectsInReverseAddedOrder() throws Throwable { // GIVEN CleanupRule rule = new CleanupRule(); AutoCloseable closeable = rule.add( mock( AutoCloseable.class ) ); Dirt dirt = rule.add( mock( Dirt.class ) ); // WHEN simulateTestExecution( rule ); // THEN InOrder inOrder = inOrder( dirt, closeable ); inOrder.verify( dirt, times( 1 ) ).shutdown(); inOrder.verify( closeable, times( 1 ) ).close(); }
private GraphDatabaseAPI createDatabase() { return (GraphDatabaseAPI) cleanup.add( builder.newGraphDatabase() ); }
@Test public void shouldTellUserIllegalArgumentIfSo() { // GIVEN CleanupRule rule = new CleanupRule(); try { rule.add( new Object() ); fail( "Should not accept this object" ); } catch ( IllegalArgumentException e ) { // OK, good } }
newIndexPopulationJob( populator, index, storeView, NullLogProvider.getInstance(), EntityType.NODE, indexDescriptor( FIRST, name, false ) ); OtherThreadExecutor<Void> populationJobRunner = cleanup.add( new OtherThreadExecutor<>( "Population job test runner", null ) ); Future<Void> runFuture = populationJobRunner
@Test public void shouldCleanupObjectWithAppropriateCloseMethod() throws Throwable { // GIVEN CleanupRule rule = new CleanupRule(); Dirt toClose = rule.add( mock( Dirt.class ) ); // WHEN simulateTestExecution( rule ); // THEN verify( toClose ).shutdown(); }
ExecutorService executor = cleanup.add( Executors.newCachedThreadPool() ); for ( int i = 0; i < 10; i++ )
@Test public void refreshReadOnlyIndexReference() throws Exception { ReadOnlyIndexReferenceFactory indexReferenceFactory = getReadOnlyIndexReferenceFactory(); IndexReference indexReference = indexReferenceFactory.createIndexReference( indexIdentifier ); cleanupRule.add( indexReference ); IndexReference refreshedIndex = indexReferenceFactory.refresh( indexReference ); assertSame("Refreshed instance should be the same.", indexReference, refreshedIndex); }
@Test public void createReadOnlyIndexReference() throws Exception { ReadOnlyIndexReferenceFactory indexReferenceFactory = getReadOnlyIndexReferenceFactory(); IndexReference indexReference = indexReferenceFactory.createIndexReference( indexIdentifier ); cleanupRule.add( indexReference ); expectedException.expect( UnsupportedOperationException.class ); indexReference.getWriter(); }
@Test public void shouldAbortStoreScanWaitOnDrop() throws Exception { // given the proxy structure FakePopulatingIndexProxy delegate = new FakePopulatingIndexProxy(); FlippableIndexProxy flipper = new FlippableIndexProxy( delegate ); OtherThreadExecutor<Void> waiter = cleanup.add( new OtherThreadExecutor<>( "Waiter", null ) ); // and a thread stuck in the awaitStoreScanCompletion loop Future<Object> waiting = waiter.executeDontWait( state -> flipper.awaitStoreScanCompleted() ); while ( !delegate.awaitCalled ) { Thread.sleep( 10 ); } // when flipper.drop(); // then the waiting should quickly be over waiting.get( 10, SECONDS ); }
@Test public void refreshChangedWritableIndexReference() throws Exception { WritableIndexReferenceFactory indexReferenceFactory = createFactory(); IndexReference indexReference = createIndexReference( indexReferenceFactory ); writeSomething( indexReference ); IndexReference refreshedIndexReference = indexReferenceFactory.refresh( indexReference ); cleanupRule.add( refreshedIndexReference ); assertNotSame( "Should return new refreshed index reference.", indexReference, refreshedIndexReference ); }
@Test public void concurrentCreatingAndAwaitingIndexesOnline() throws Exception { ExecutorService executor = cleanupRule.add( Executors.newSingleThreadExecutor() ); Future<?> indexCreate = executor.submit( () -> { try ( Transaction tx = db.beginTx() ) { db.schema().indexFor( label( 0 ) ).on( KEY ).create(); tx.success(); } } ); while ( !indexCreate.isDone() ) { try ( Transaction tx = db.beginTx() ) { db.schema().awaitIndexesOnline( 1, TimeUnit.MINUTES ); tx.success(); } } indexCreate.get(); }
ExecutorService executor = cleanup.add( Executors.newFixedThreadPool( 2, named( "Test-executor-thread" ) ) );
@Test public void shouldHaveOneThreadWaitForARemoval() throws Exception { // GIVEN IdOrderingQueue queue = new SynchronizedArrayIdOrderingQueue( 5 ); queue.offer( 3 ); queue.offer( 5 ); // WHEN another thread comes in and awaits 5 OtherThreadExecutor<Void> t2 = cleanup.add( new OtherThreadExecutor<Void>( "T2", null ) ); Future<Object> await5 = t2.executeDontWait( awaitHead( queue, 5 ) ); t2.waitUntilWaiting(); // ... and head (3) gets removed queue.removeChecked( 3 ); // THEN the other thread should be OK to continue await5.get(); }
ExecutorService executor = cleanup.add( Executors.newFixedThreadPool( 2 ) ); Future<?> future1 = executor.submit( createIndexes ); Future<?> future2 = executor.submit( makeAllEntitiesGreen );