use of java.nio.channels.SeekableByteChannel in project beam by apache.
the class MinimalWordCountJava8Test method buildMockGcsUtil.
private GcsUtil buildMockGcsUtil() throws IOException {
GcsUtil mockGcsUtil = Mockito.mock(GcsUtil.class);
// Any request to open gets a new bogus channel
Mockito.when(mockGcsUtil.open(Mockito.any(GcsPath.class))).then(new Answer<SeekableByteChannel>() {
@Override
public SeekableByteChannel answer(InvocationOnMock invocation) throws Throwable {
return FileChannel.open(Files.createTempFile("channel-", ".tmp"), StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE);
}
});
// Any request for expansion returns a list containing the original GcsPath
// This is required to pass validation that occurs in TextIO during apply()
Mockito.when(mockGcsUtil.expand(Mockito.any(GcsPath.class))).then(new Answer<List<GcsPath>>() {
@Override
public List<GcsPath> answer(InvocationOnMock invocation) throws Throwable {
return ImmutableList.of((GcsPath) invocation.getArguments()[0]);
}
});
return mockGcsUtil;
}
use of java.nio.channels.SeekableByteChannel in project beam by apache.
the class DataflowRunnerTest method testRunWithFiles.
@Test
public void testRunWithFiles() throws IOException {
// Test that the function DataflowRunner.stageFiles works as expected.
final String cloudDataflowDataset = "somedataset";
// Create some temporary files.
File temp1 = File.createTempFile("DataflowRunnerTest", "txt");
temp1.deleteOnExit();
File temp2 = File.createTempFile("DataflowRunnerTest2", "txt");
temp2.deleteOnExit();
String overridePackageName = "alias.txt";
when(mockGcsUtil.getObjects(anyListOf(GcsPath.class))).thenReturn(ImmutableList.of(GcsUtil.StorageObjectOrIOException.create(new FileNotFoundException("some/path"))));
DataflowPipelineOptions options = PipelineOptionsFactory.as(DataflowPipelineOptions.class);
options.setFilesToStage(ImmutableList.of(temp1.getAbsolutePath(), overridePackageName + "=" + temp2.getAbsolutePath()));
options.setStagingLocation(VALID_STAGING_BUCKET);
options.setTempLocation(VALID_TEMP_BUCKET);
options.setTempDatasetId(cloudDataflowDataset);
options.setProject(PROJECT_ID);
options.setRegion(REGION_ID);
options.setJobName("job");
options.setDataflowClient(buildMockDataflow());
options.setGcsUtil(mockGcsUtil);
options.setGcpCredential(new TestCredential());
when(mockGcsUtil.create(any(GcsPath.class), anyString(), anyInt())).then(new Answer<SeekableByteChannel>() {
@Override
public SeekableByteChannel answer(InvocationOnMock invocation) throws Throwable {
return FileChannel.open(Files.createTempFile("channel-", ".tmp"), StandardOpenOption.CREATE, StandardOpenOption.DELETE_ON_CLOSE);
}
});
Pipeline p = buildDataflowPipeline(options);
DataflowPipelineJob job = (DataflowPipelineJob) p.run();
assertEquals("newid", job.getJobId());
ArgumentCaptor<Job> jobCaptor = ArgumentCaptor.forClass(Job.class);
Mockito.verify(mockJobs).create(eq(PROJECT_ID), eq(REGION_ID), jobCaptor.capture());
Job workflowJob = jobCaptor.getValue();
assertValidJob(workflowJob);
assertEquals(2, workflowJob.getEnvironment().getWorkerPools().get(0).getPackages().size());
DataflowPackage workflowPackage1 = workflowJob.getEnvironment().getWorkerPools().get(0).getPackages().get(0);
assertThat(workflowPackage1.getName(), startsWith(temp1.getName()));
DataflowPackage workflowPackage2 = workflowJob.getEnvironment().getWorkerPools().get(0).getPackages().get(1);
assertEquals(overridePackageName, workflowPackage2.getName());
assertEquals(GcsPath.fromUri(VALID_TEMP_BUCKET).toResourceName(), workflowJob.getEnvironment().getTempStoragePrefix());
assertEquals(cloudDataflowDataset, workflowJob.getEnvironment().getDataset());
assertEquals(ReleaseInfo.getReleaseInfo().getName(), workflowJob.getEnvironment().getUserAgent().get("name"));
assertEquals(ReleaseInfo.getReleaseInfo().getVersion(), workflowJob.getEnvironment().getUserAgent().get("version"));
}
use of java.nio.channels.SeekableByteChannel in project lucene-solr by apache.
the class TestHandleTrackingFS method testOnOpenThrowsException.
/** Test that the delegate gets closed on exception in HandleTrackingFS#onOpen */
public void testOnOpenThrowsException() throws IOException {
// we are using LeakFS under the hood if we don't get closed the test fails
Path path = wrap(createTempDir());
FileSystem fs = new HandleTrackingFS("test://", path.getFileSystem()) {
@Override
protected void onClose(Path path, Object stream) throws IOException {
}
@Override
protected void onOpen(Path path, Object stream) throws IOException {
throw new IOException("boom");
}
}.getFileSystem(URI.create("file:///"));
Path dir = new FilterPath(path, fs);
try {
OutputStream file = Files.newOutputStream(dir.resolve("somefile"));
fail("expected IOException");
} catch (IOException ex) {
// expected
}
try {
SeekableByteChannel channel = Files.newByteChannel(dir.resolve("somefile"));
fail("expected IOException");
} catch (IOException ex) {
// expected
}
try {
InputStream stream = Files.newInputStream(dir.resolve("somefile"));
fail("expected IOException");
} catch (IOException ex) {
// expected
}
fs.close();
try {
DirectoryStream<Path> dirStream = Files.newDirectoryStream(dir);
fail("expected IOException");
} catch (IOException ex) {
// expected
}
fs.close();
}
use of java.nio.channels.SeekableByteChannel in project lucene-solr by apache.
the class TestHandleTrackingFS method testOnCloseThrowsException.
/** Test that the delegate gets closed on exception in HandleTrackingFS#onClose */
public void testOnCloseThrowsException() throws IOException {
// we are using LeakFS under the hood if we don't get closed the test fails
Path path = wrap(createTempDir());
FileSystem fs = new HandleTrackingFS("test://", path.getFileSystem()) {
@Override
protected void onClose(Path path, Object stream) throws IOException {
throw new IOException("boom");
}
@Override
protected void onOpen(Path path, Object stream) throws IOException {
//
}
}.getFileSystem(URI.create("file:///"));
Path dir = new FilterPath(path, fs);
OutputStream file = Files.newOutputStream(dir.resolve("somefile"));
file.write(5);
try {
file.close();
fail("expected IOException");
} catch (IOException ex) {
// expected
}
SeekableByteChannel channel = Files.newByteChannel(dir.resolve("somefile"));
try {
channel.close();
fail("expected IOException");
} catch (IOException ex) {
// expected
}
InputStream stream = Files.newInputStream(dir.resolve("somefile"));
try {
stream.close();
fail("expected IOException");
} catch (IOException ex) {
// expected
}
fs.close();
DirectoryStream<Path> dirStream = Files.newDirectoryStream(dir);
try {
dirStream.close();
fail("expected IOException");
} catch (IOException ex) {
// expected
}
}
use of java.nio.channels.SeekableByteChannel in project lucene-solr by apache.
the class SimpleFSDirectory method openInput.
/** Creates an IndexInput for the file with the given name. */
@Override
public IndexInput openInput(String name, IOContext context) throws IOException {
ensureOpen();
ensureCanRead(name);
Path path = directory.resolve(name);
SeekableByteChannel channel = Files.newByteChannel(path, StandardOpenOption.READ);
return new SimpleFSIndexInput("SimpleFSIndexInput(path=\"" + path + "\")", channel, context);
}
Aggregations