Search in sources :

Example 46 with SeekableByteChannel

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;
}
Also used : SeekableByteChannel(java.nio.channels.SeekableByteChannel) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GcsPath(org.apache.beam.sdk.util.gcsfs.GcsPath) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) GcsUtil(org.apache.beam.sdk.util.GcsUtil)

Example 47 with SeekableByteChannel

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"));
}
Also used : DataflowPipelineOptions(org.apache.beam.runners.dataflow.options.DataflowPipelineOptions) FileNotFoundException(java.io.FileNotFoundException) Matchers.containsString(org.hamcrest.Matchers.containsString) Matchers.anyString(org.mockito.Matchers.anyString) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) Pipeline(org.apache.beam.sdk.Pipeline) SeekableByteChannel(java.nio.channels.SeekableByteChannel) TestCredential(org.apache.beam.sdk.extensions.gcp.auth.TestCredential) InvocationOnMock(org.mockito.invocation.InvocationOnMock) GcsPath(org.apache.beam.sdk.util.gcsfs.GcsPath) Job(com.google.api.services.dataflow.model.Job) DataflowRunner.getContainerImageForJob(org.apache.beam.runners.dataflow.DataflowRunner.getContainerImageForJob) File(java.io.File) DataflowPackage(com.google.api.services.dataflow.model.DataflowPackage) Test(org.junit.Test)

Example 48 with SeekableByteChannel

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();
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) InputStream(java.io.InputStream) FileSystem(java.nio.file.FileSystem) OutputStream(java.io.OutputStream) Object(java.lang.Object) IOException(java.io.IOException) HandleTrackingFS(org.apache.lucene.mockfile.HandleTrackingFS) Override(java.lang.Override)

Example 49 with SeekableByteChannel

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
    }
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel) InputStream(java.io.InputStream) FileSystem(java.nio.file.FileSystem) OutputStream(java.io.OutputStream) Object(java.lang.Object) IOException(java.io.IOException) HandleTrackingFS(org.apache.lucene.mockfile.HandleTrackingFS) Override(java.lang.Override)

Example 50 with SeekableByteChannel

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);
}
Also used : Path(java.nio.file.Path) SeekableByteChannel(java.nio.channels.SeekableByteChannel)

Aggregations

SeekableByteChannel (java.nio.channels.SeekableByteChannel)58 Path (java.nio.file.Path)33 Test (org.junit.Test)20 ByteBuffer (java.nio.ByteBuffer)16 IOException (java.io.IOException)10 Test (org.testng.annotations.Test)9 File (java.io.File)6 CloudStorageFileSystem (com.google.cloud.storage.contrib.nio.CloudStorageFileSystem)5 GcsPath (org.apache.beam.sdk.util.gcsfs.GcsPath)4 InvocationOnMock (org.mockito.invocation.InvocationOnMock)4 ImmutableList (com.google.common.collect.ImmutableList)3 InputStream (java.io.InputStream)3 FileSystem (java.nio.file.FileSystem)3 java.util (java.util)3 Function (java.util.function.Function)3 CharStream (org.antlr.v4.runtime.CharStream)3 CodePointCharStream (org.antlr.v4.runtime.CodePointCharStream)3 UserException (org.broadinstitute.hellbender.exceptions.UserException)3 IOUtils (org.broadinstitute.hellbender.utils.io.IOUtils)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2