/** * Concurrently retrieve after stage but before upload. * @throws Exception */ @Test public void testConcurrentGetDelete() throws Exception { ListeningExecutorService executorService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(2)); closer.register(new ExecutorCloser(executorService)); // Add load List<ListenableFuture<Integer>> futures = put(folder); // Get a handle to the file and open stream File file = stagingCache.getIfPresent(ID_PREFIX + 0); final InputStream fStream = Files.asByteSource(file).openStream(); // task to copy the steam to a file simulating read from the stream File temp = folder.newFile(); CountDownLatch copyThreadLatch = new CountDownLatch(1); SettableFuture<File> future1 = copyStreamThread(executorService, fStream, temp, copyThreadLatch); //start taskLatch.countDown(); callbackLatch.countDown(); waitFinish(futures); // trying copying now copyThreadLatch.countDown(); future1.get(); assertTrue(Files.equal(temp, uploader.read(ID_PREFIX + 0))); }
/** * Test build on start. * @throws Exception */ @Test public void testBuild() throws Exception { // Add load List<ListenableFuture<Integer>> futures = put(folder); // Close before uploading finished closer.close(); // Start again init(1); taskLatch.countDown(); callbackLatch.countDown(); afterExecuteLatch.await(); waitFinish(futures); assertNull(stagingCache.getIfPresent(ID_PREFIX + 0)); assertTrue(Files.equal(copyToFile(randomStream(0, 4 * 1024), folder.newFile()), uploader.read(ID_PREFIX + 0))); assertCacheStats(stagingCache, 0, 0, 1, 1); }
assertNull(ret); assertTrue(Files.equal(copyToFile(randomStream(0, 4 * 1024), folder.newFile()), secondTimeUploader.read(ID_PREFIX + 0)));
/** * Test upgrade with build on start. * @throws Exception */ @Test public void testUpgrade() throws Exception { // Add load List<ListenableFuture<Integer>> futures = put(folder); // Close before uploading finished closer.close(); // Create pre-upgrade load File home = folder.newFolder(); File pendingUploadsFile = new File(home, DataStoreCacheUpgradeUtils.UPLOAD_MAP); createUpgradeLoad(home, pendingUploadsFile); // Start again init(2, new TestStagingUploader(folder.newFolder()), home); taskLatch.countDown(); callbackLatch.countDown(); afterExecuteLatch.await(); waitFinish(futures); assertNull(stagingCache.getIfPresent(ID_PREFIX + 0)); assertTrue(Files.equal(copyToFile(randomStream(0, 4 * 1024), folder.newFile()), uploader.read(ID_PREFIX + 0))); assertUpgrade(pendingUploadsFile); assertCacheStats(stagingCache, 0, 0, 2, 2); }
private void assertUpgrade(File pendingUploadFile) throws IOException { assertNull(stagingCache.getIfPresent(ID_PREFIX + 1)); assertTrue(Files.equal(copyToFile(randomStream(1, 4 * 1024), folder.newFile()), uploader.read(ID_PREFIX + 1))); assertFalse(pendingUploadFile.exists()); }
private void assertFuture(List<ListenableFuture<Integer>> futures, int... seeds) throws Exception { waitFinish(futures); for (int i = 0; i < seeds.length; i++) { File upload = uploader.read(ID_PREFIX + seeds[i]); assertFile(upload, seeds[i], folder); } }