@Test public void testDisposesProperlyImmediately() throws BrokenBarrierException, InterruptedException, IOException { CacheManager manager = new CacheManager(new Configuration().name("testDisposesProperlyImmediately")); try { deleteFolder(new File(DUMPS_DIRECTORY)); final RotatingSnapshotFile rotatingSnapshotFile = new RotatingSnapshotFile(new DiskStorePathManager(DUMPS_DIRECTORY), "testingInterruptImmediate", getClass().getClassLoader()); final TerracottaStore mockedTcStore = mock(TerracottaStore.class); KeySnapshotter snapshotter = new KeySnapshotter(createFakeTcClusteredCache(manager, mockedTcStore), 1, true, rotatingSnapshotFile); final CyclicBarrier barrier = new CyclicBarrier(2); final Set mockedSet = mock(Set.class); final AtomicInteger counter = new AtomicInteger(0); final int maxElementsToReturn = 100000; when(mockedSet.iterator()).thenAnswer(new Answer<Iterator>() { public Iterator answer(final InvocationOnMock invocationOnMock) throws Throwable { return new Iterator<Object>() { public boolean hasNext() { return counter.get() < maxElementsToReturn; } public Object next() { return "Key" + counter.getAndIncrement(); } public void remove() { } }; } }); when(mockedTcStore.getLocalKeys()).thenAnswer(new Answer<Set>() { public Set answer(final InvocationOnMock invocationOnMock) throws Throwable { barrier.await(); return mockedSet; } }); barrier.await(); snapshotter.dispose(true); assertThat("Snapshot was allowed to finish - this shouldn't happen (unless the snapshot happens really fast and the scheduler is *very* unfair)", counter.get(), lessThan(maxElementsToReturn)); final int elementsRead = rotatingSnapshotFile.readAll().size(); assertThat("Should be only a couple: " + elementsRead, elementsRead, is(counter.get() - 1)); } finally { manager.shutdown(); } }
void dispose(boolean immediately) { if (thread != null) { rotatingWriter.setShutdownOnThreadInterrupted(immediately); thread.interrupt(); try { thread.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } else { scheduledFuture.cancel(immediately); } tcStore = null; }
KeySnapshotter implements Runnable { void dispose(boolean immediately) { if (thread != null) { rotatingWriter.setShutdownOnThreadInterrupted(immediately); thread.interrupt(); try { thread.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } else { scheduledFuture.cancel(immediately); } tcStore = null; } }
KeySnapshotter implements Runnable { void dispose(boolean immediately) { if (thread != null) { rotatingWriter.setShutdownOnThreadInterrupted(immediately); thread.interrupt(); try { thread.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } else { scheduledFuture.cancel(immediately); } tcStore = null; } KeySnapshotter(final Ehcache cache, final long interval,
                   final boolean doKeySnapshotOnDedicatedThread,
                   final RotatingSnapshotFile rotatingWriter); }
KeySnapshotter implements Runnable { void dispose(boolean immediately) { if (thread != null) { rotatingWriter.setShutdownOnThreadInterrupted(immediately); thread.interrupt(); try { thread.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } else { scheduledFuture.cancel(immediately); } tcStore = null; } KeySnapshotter(final Ehcache cache, final long interval,
                   final boolean doKeySnapshotOnDedicatedThread,
                   final RotatingSnapshotFile rotatingWriter); void run(); String getCacheName(); }
KeySnapshotter implements Runnable { void dispose(boolean immediately) { if (thread != null) { rotatingWriter.setShutdownOnThreadInterrupted(immediately); thread.interrupt(); try { thread.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } else { scheduledFuture.cancel(immediately); } tcStore = null; } KeySnapshotter(final Ehcache cache, final long interval,
                   final boolean doKeySnapshotOnDedicatedThread,
                   final RotatingSnapshotFile rotatingWriter); void run(); String getCacheName();  }