ManifestGeneratorImpl implements ManifestGenerator { @Override public InputStream getManifest(String account, String storeId, String spaceId, ManifestFormat format) throws ManifestArgumentException, ManifestNotFoundException { log.info("retrieving manifest for account:{}, storeId:{}, spaceId:{}, format:{}", account, storeId, spaceId, format); try { storeId = validateStoreId(storeId); validateSpaceId(storeId, spaceId); PipedInputStream is = new PipedInputStream(10 * 1024); final PipedOutputStream os = new PipedOutputStream(is); final Iterator<ManifestItem> it = this.manifestStore.getItems(account, storeId, spaceId); final ManifestFormatter formatter = getFormatter(format); if (!it.hasNext()) { formatter.writeManifestItemToOutput(null, os); os.close(); return is; } else { new Thread(new Runnable() { @Override public void run() { try { while (it.hasNext()) { formatter.writeManifestItemToOutput(it.next(), os); } try { os.close(); } catch (IOException e) { log.error("failed to close piped output stream : " + e.getMessage(), e); } } catch (Exception e) { log.error("error writing to piped output stream : " + e.getMessage(), e); } } }).start(); } return is; } catch (IOException | RuntimeException ex) { log.error("failed to retrieve manifest: " + ex.getMessage(), ex); throw new ManifestGeneratorException(ex.getMessage()); } } ManifestGeneratorImpl(ManifestStore manifestStore,
                                 StorageProviderFactory storageProviderFactory); @Override InputStream getManifest(String account,
                                   String storeId,
                                   String spaceId,
                                   ManifestFormat format);  }
@Test public void testIteratorFailure() throws Exception { Iterator<ManifestItem> it = createMock(Iterator.class); expect(it.hasNext()).andReturn(true).times(3); expect(it.next()).andReturn(createMockManifestItem()); expect(it.next()).andAnswer(new IAnswer<ManifestItem>() { @Override public ManifestItem answer() throws Throwable { throw new IOException("exception"); } }); expect(store.getItems(eq(account), eq(storeId), eq(spaceId))).andReturn(it); mockStorageProviderFactory(); replayAll(); generator = new ManifestGeneratorImpl(store, storageProviderFactory); InputStream is = generator.getManifest(account, storeId, spaceId, ManifestFormat.TSV); BufferedReader reader = new BufferedReader(new InputStreamReader(is)); int read = 0; try { assertNotNull(reader.readLine()); read++; assertNotNull(reader.readLine()); read++; Thread.sleep(100); reader.readLine(); fail("expected exception to be thrown before getting null from reader.readLine()."); } catch (IOException e) { assertTrue(true); } assertEquals(2, read); }