Search in sources :

Example 1 with TestUnderFileSystemFileOutStream

use of alluxio.client.block.stream.TestUnderFileSystemFileOutStream in project alluxio by Alluxio.

the class FileOutStreamTest method before.

/**
 * Sets up the different contexts and clients before a test runs.
 */
@Before
public void before() throws Exception {
    GroupMappingServiceTestUtils.resetCache();
    ClientTestUtils.setSmallBufferSizes(sConf);
    mClientContext = ClientContext.create(sConf);
    // PowerMock enums and final classes
    mFileSystemContext = PowerMockito.mock(FileSystemContext.class);
    when(mFileSystemContext.getClientContext()).thenReturn(mClientContext);
    when(mFileSystemContext.getClusterConf()).thenReturn(sConf);
    when(mFileSystemContext.getPathConf(any(AlluxioURI.class))).thenReturn(sConf);
    mBlockStore = PowerMockito.mock(AlluxioBlockStore.class);
    mFileSystemMasterClient = PowerMockito.mock(FileSystemMasterClient.class);
    PowerMockito.mockStatic(AlluxioBlockStore.class);
    PowerMockito.when(AlluxioBlockStore.create(mFileSystemContext)).thenReturn(mBlockStore);
    when(mFileSystemContext.acquireMasterClientResource()).thenReturn(new DummyCloseableResource<>(mFileSystemMasterClient));
    when(mFileSystemMasterClient.getStatus(any(AlluxioURI.class), any(GetStatusPOptions.class))).thenReturn(new URIStatus(new FileInfo()));
    // Return sequentially increasing numbers for new block ids
    when(mFileSystemMasterClient.getNewBlockIdForFile(FILE_NAME)).thenAnswer(new Answer<Long>() {

        private long mCount = 0;

        @Override
        public Long answer(InvocationOnMock invocation) throws Throwable {
            return mCount++;
        }
    });
    // Set up out streams. When they are created, add them to outStreamMap
    final Map<Long, TestBlockOutStream> outStreamMap = new HashMap<>();
    when(mBlockStore.getOutStream(anyLong(), eq(BLOCK_LENGTH), any(OutStreamOptions.class))).thenAnswer(new Answer<TestBlockOutStream>() {

        @Override
        public TestBlockOutStream answer(InvocationOnMock invocation) throws Throwable {
            Long blockId = invocation.getArgument(0, Long.class);
            if (!outStreamMap.containsKey(blockId)) {
                TestBlockOutStream newStream = new TestBlockOutStream(ByteBuffer.allocate(1000), BLOCK_LENGTH);
                outStreamMap.put(blockId, newStream);
            }
            return outStreamMap.get(blockId);
        }
    });
    BlockWorkerInfo workerInfo = new BlockWorkerInfo(new WorkerNetAddress().setHost("localhost").setTieredIdentity(TieredIdentityFactory.fromString("node=localhost", sConf)).setRpcPort(1).setDataPort(2).setWebPort(3), Constants.GB, 0);
    when(mFileSystemContext.getCachedWorkers()).thenReturn(Lists.newArrayList(workerInfo));
    mAlluxioOutStreamMap = outStreamMap;
    // Create an under storage stream so that we can check whether it has been flushed
    final AtomicBoolean underStorageFlushed = new AtomicBoolean(false);
    mUnderStorageOutputStream = new TestUnderFileSystemFileOutStream(ByteBuffer.allocate(5000)) {

        @Override
        public void flush() throws IOException {
            super.flush();
            underStorageFlushed.set(true);
        }
    };
    mUnderStorageFlushed = underStorageFlushed;
    PowerMockito.mockStatic(UnderFileSystemFileOutStream.class);
    PowerMockito.when(UnderFileSystemFileOutStream.create(any(FileSystemContext.class), any(WorkerNetAddress.class), any(OutStreamOptions.class))).thenReturn(mUnderStorageOutputStream);
    OutStreamOptions options = OutStreamOptions.defaults(mClientContext).setBlockSizeBytes(BLOCK_LENGTH).setWriteType(WriteType.CACHE_THROUGH).setUfsPath(FILE_NAME.getPath());
    mTestStream = createTestStream(FILE_NAME, options);
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) GetStatusPOptions(alluxio.grpc.GetStatusPOptions) TestUnderFileSystemFileOutStream(alluxio.client.block.stream.TestUnderFileSystemFileOutStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OutStreamOptions(alluxio.client.file.options.OutStreamOptions) FileInfo(alluxio.wire.FileInfo) InvocationOnMock(org.mockito.invocation.InvocationOnMock) WorkerNetAddress(alluxio.wire.WorkerNetAddress) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) BlockWorkerInfo(alluxio.client.block.BlockWorkerInfo) TestBlockOutStream(alluxio.client.block.stream.TestBlockOutStream) AlluxioBlockStore(alluxio.client.block.AlluxioBlockStore) AlluxioURI(alluxio.AlluxioURI) Before(org.junit.Before)

Aggregations

AlluxioURI (alluxio.AlluxioURI)1 AlluxioBlockStore (alluxio.client.block.AlluxioBlockStore)1 BlockWorkerInfo (alluxio.client.block.BlockWorkerInfo)1 TestBlockOutStream (alluxio.client.block.stream.TestBlockOutStream)1 TestUnderFileSystemFileOutStream (alluxio.client.block.stream.TestUnderFileSystemFileOutStream)1 OutStreamOptions (alluxio.client.file.options.OutStreamOptions)1 GetStatusPOptions (alluxio.grpc.GetStatusPOptions)1 FileInfo (alluxio.wire.FileInfo)1 WorkerNetAddress (alluxio.wire.WorkerNetAddress)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Before (org.junit.Before)1 ArgumentMatchers.anyLong (org.mockito.ArgumentMatchers.anyLong)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1