@Test public void testDisposesProperlyButFinishes() throws BrokenBarrierException, InterruptedException, IOException { CacheManager manager = new CacheManager(new Configuration().name("testDisposesProperlyImmediately")); try { deleteFolder(new File(DUMPS_DIRECTORY)); final RotatingSnapshotFile rotatingSnapshotFile = new RotatingSnapshotFile(DUMPS_DIRECTORY, "testingInterruptFinishes"); final TerracottaStore mockedTcStore = mock(TerracottaStore.class); final CyclicBarrier barrier = new CyclicBarrier(2); final Set mockedSet = mock(Set.class); final AtomicLong counter = new AtomicLong(0); 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() < MAX_KEY; } 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; } }); KeySnapshotter snapshotter = new KeySnapshotter(createFakeTcClusteredCache(manager, mockedTcStore), 1, true, rotatingSnapshotFile); barrier.await(); snapshotter.dispose(false); assertThat(counter.get(), is(MAX_KEY)); final Set<Object> objects = new HashSet<Object>(rotatingSnapshotFile.readAll()); assertThat(objects.size(), is((int) MAX_KEY)); for(int i = 0; i < MAX_KEY; i++) { objects.remove(KEY + i); } assertThat(objects.isEmpty(), is(true)); } finally { manager.shutdown(); } }
void dispose(boolean immediately) { if (thread != null) { rotatingWriter.setShutdownOnThreadInterrupted(immediately); thread.interrupt(); try { thread.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } }
KeySnapshotter implements Runnable { void dispose(boolean immediately) { if (thread != null) { rotatingWriter.setShutdownOnThreadInterrupted(immediately); thread.interrupt(); try { thread.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } }
KeySnapshotter implements Runnable { void dispose(boolean immediately) { if (thread != null) { rotatingWriter.setShutdownOnThreadInterrupted(immediately); thread.interrupt(); try { thread.join(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } } } 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(); } } } 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(); } } } KeySnapshotter(final Ehcache cache, final long interval,
                   final boolean doKeySnapshotOnDedicatedThread,
                   final RotatingSnapshotFile rotatingWriter); void run(); String getCacheName();  }