Search in sources :

Example 1 with Pointer

use of jnr.ffi.Pointer in project alluxio by Alluxio.

the class AlluxioFuseFileSystemTest method allocateNativeFileInfo.

// Allocate native memory for a FuseFileInfo data struct and return its pointer
private FuseFileInfo allocateNativeFileInfo() {
    final Runtime runtime = Runtime.getSystemRuntime();
    final Pointer pt = runtime.getMemoryManager().allocateTemporary(36, true);
    return FuseFileInfo.of(pt);
}
Also used : Runtime(jnr.ffi.Runtime) Pointer(jnr.ffi.Pointer)

Example 2 with Pointer

use of jnr.ffi.Pointer in project alluxio by Alluxio.

the class AlluxioFuseFileSystemTest method read.

@Test
public void read() throws Exception {
    // mocks set-up
    AlluxioURI expectedPath = BASE_EXPECTED_URI.join("/foo/bar");
    FileInfo fi = new FileInfo();
    fi.setFolder(false);
    URIStatus status = new URIStatus(fi);
    when(mFileSystem.exists(expectedPath)).thenReturn(true);
    when(mFileSystem.getStatus(expectedPath)).thenReturn(status);
    FileInStream fakeInStream = mock(FileInStream.class);
    when(fakeInStream.read(any(byte[].class), anyInt(), anyInt())).then(new Answer<Integer>() {

        @Override
        public Integer answer(InvocationOnMock invocationOnMock) throws Throwable {
            byte[] myDest = (byte[]) invocationOnMock.getArguments()[0];
            for (byte i = 0; i < 4; i++) {
                myDest[i] = i;
            }
            return 4;
        }
    });
    when(mFileSystem.openFile(expectedPath)).thenReturn(fakeInStream);
    mFileInfo.flags.set(O_RDONLY.intValue());
    // prepare something to read to it
    Runtime r = Runtime.getSystemRuntime();
    Pointer ptr = r.getMemoryManager().allocateTemporary(4, true);
    // actual test
    mFuseFs.open("/foo/bar", mFileInfo);
    mFuseFs.read("/foo/bar", ptr, 4, 0, mFileInfo);
    final byte[] dst = new byte[4];
    ptr.get(0, dst, 0, 4);
    final byte[] expected = new byte[] { 0, 1, 2, 3 };
    assertArrayEquals("Source and dst data should be equal", expected, dst);
}
Also used : Runtime(jnr.ffi.Runtime) FuseFileInfo(ru.serce.jnrfuse.struct.FuseFileInfo) FileInfo(alluxio.wire.FileInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) FileInStream(alluxio.client.file.FileInStream) Pointer(jnr.ffi.Pointer) URIStatus(alluxio.client.file.URIStatus) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 3 with Pointer

use of jnr.ffi.Pointer in project alluxio by Alluxio.

the class AlluxioFuseFileSystemTest method write.

@Test
public void write() throws Exception {
    FileOutStream fos = mock(FileOutStream.class);
    AlluxioURI anyURI = any();
    when(mFileSystem.createFile(anyURI)).thenReturn(fos);
    // open a file
    mFileInfo.flags.set(O_WRONLY.intValue());
    mFuseFs.create("/foo/bar", 0, mFileInfo);
    // prepare something to write into it
    Runtime r = Runtime.getSystemRuntime();
    Pointer ptr = r.getMemoryManager().allocateTemporary(4, true);
    byte[] expected = { 42, -128, 1, 3 };
    ptr.put(0, expected, 0, 4);
    mFuseFs.write("/foo/bar", ptr, 4, 0, mFileInfo);
    verify(fos).write(expected);
}
Also used : Runtime(jnr.ffi.Runtime) FileOutStream(alluxio.client.file.FileOutStream) Pointer(jnr.ffi.Pointer) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Aggregations

Pointer (jnr.ffi.Pointer)3 Runtime (jnr.ffi.Runtime)3 AlluxioURI (alluxio.AlluxioURI)2 Test (org.junit.Test)2 FileInStream (alluxio.client.file.FileInStream)1 FileOutStream (alluxio.client.file.FileOutStream)1 URIStatus (alluxio.client.file.URIStatus)1 FileInfo (alluxio.wire.FileInfo)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 FuseFileInfo (ru.serce.jnrfuse.struct.FuseFileInfo)1