Source:
public void testFileTruncation() throws ExecutionException, InterruptedException { final int NUM_WRITER_THREADS = 2; final int NUM_READER_THREADS = 2; final int NUM_KEYS = 5; final int TEST_DURATION_SECONDS = 2; Cache<String, String> cache = cacheManager.getCache(CACHE_NAME); final SingleFileStore store = TestingUtil.getFirstWriter(cache); final StreamingMarshaller marshaller = TestingUtil.extractComponentRegistry(cache).getCacheMarshaller(); assertEquals(0, store.size()); final List<String> keys = populateStore(NUM_KEYS, 2, store, marshaller); final CountDownLatch stopLatch = new CountDownLatch(1); Future[] writeFutures = new Future[NUM_WRITER_THREADS]; for (int i = 0; i < NUM_WRITER_THREADS; i++) writeFutures[i] = fork(stopOnException(new WriteTask(store, marshaller, keys, stopLatch), stopLatch)); Future[] readFutures = new Future[NUM_READER_THREADS]; for (int i = 0; i < NUM_READER_THREADS; i++) readFutures[i] = fork(stopOnException(new ReadTask(store, keys, false, stopLatch), stopLatch)); stopLatch.await(TEST_DURATION_SECONDS, SECONDS); stopLatch.countDown(); for (int i = 0; i < NUM_WRITER_THREADS; i++) writeFutures[i].get(); for (int i = 0; i < NUM_READER_THREADS; i++) readFutures[i].get(); File file = new File(location, CACHE_NAME + ".dat"); long length1 = file.length(); store.purge(null, null); long length2 = file.length(); populateStore(NUM_KEYS, 0, store, marshaller); store.purge(null, null); long length3 = file.length(); assertTrue(length2 <= length1); assertTrue(String.format("Length2=%d, Length3=%d", length2, length3), length3 <= length2); }

Target:
public void testFileTruncation() throws ExecutionException, InterruptedException { final int writerThreads = 2; final int readerThreads = 2; final int numberOfKeys = 5; Cache<String, String> cache = cacheManager.getCache(CACHE_NAME); final SingleFileStore store = TestingUtil.getFirstWriter(cache); final StreamingMarshaller marshaller = TestingUtil.extractComponentRegistry(cache).getCacheMarshaller(); assertEquals(0, store.size()); final List<String> keys = populateStore(5, 0, store, marshaller); final CountDownLatch stopLatch = new CountDownLatch(1); Future[] writeFutures = new Future[writerThreads]; for (int i = 0; i < writerThreads; i++) { writeFutures[i] = fork(stopOnException(new WriteTask(store, marshaller, keys, stopLatch), stopLatch)); } Future[] readFutures = new Future[readerThreads]; for (int i = 0; i < readerThreads; i++) { readFutures[i] = fork(stopOnException(new ReadTask(store, keys, false, stopLatch), stopLatch)); } stopLatch.await(2, SECONDS); stopLatch.countDown(); for (int i = 0; i < writerThreads; i++) { writeFutures[i].get(); } for (int i = 0; i < readerThreads; i++) { readFutures[i].get(); } File file = new File(location, CACHE_NAME + ".dat"); long length1 = file.length(); store.purge(null, null); long length2 = file.length(); assertTrue(String.format("Length1=%d, Length2=%d", length1, length2), length2 <= length1); String key = "key" + numberOfKeys; byte[] bytes = new byte[(int) store.getFileSize()]; ByteBuffer buf = new ByteBufferImpl(bytes, 0, bytes.length); store.write(new MarshalledEntryImpl<>(key, buf, (ByteBuffer) null, marshaller)); length1 = file.length(); store.delete(key); store.purge(null, null); length2 = file.length(); assertTrue(String.format("Length1=%d, Length2=%d", length1, length2), length2 < length1); }