@@ -0,0 +1,3638 @@
+/*%0A * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0%0A * (the %22License%22). You may not use this work except in compliance with the License, which is%0A * available at www.apache.org/licenses/LICENSE-2.0%0A *%0A * This software is distributed on an %22AS IS%22 basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,%0A * either express or implied, as more fully set forth in the License.%0A *%0A * See the NOTICE file distributed with this work for information regarding copyright ownership.%0A */%0A%0Apackage alluxio.client.block.stream;%0A%0Aimport static org.mockito.Matchers.any;%0Aimport static org.mockito.Matchers.eq;%0Aimport static org.mockito.Mockito.mock;%0Aimport static org.mockito.Mockito.when;%0A%0Aimport alluxio.PropertyKey;%0Aimport alluxio.SetAndRestoreConfiguration;%0Aimport alluxio.client.block.BlockWorkerClient;%0Aimport alluxio.client.block.options.LockBlockOptions;%0Aimport alluxio.client.file.FileSystemContext;%0Aimport alluxio.client.file.options.InStreamOptions;%0Aimport alluxio.client.resource.LockBlockResource;%0Aimport alluxio.wire.LockBlockResult;%0Aimport alluxio.wire.LockBlockResult.LockBlockStatus;%0Aimport alluxio.wire.WorkerNetAddress;%0A%0Aimport com.google.common.collect.ImmutableMap;%0Aimport org.junit.Assert;%0Aimport org.junit.Test;%0Aimport org.junit.runner.RunWith;%0Aimport org.powermock.core.classloader.annotations.PrepareForTest;%0Aimport org.powermock.modules.junit4.PowerMockRunner;%0A%0Aimport java.net.InetSocketAddress;%0A%0A/**%0A * Unit tests for %7B@link BlockInStream%7D.%0A */%0A@RunWith(PowerMockRunner.class)%0A@PrepareForTest(%7BFileSystemContext.class, LockBlockResource.class%7D)%0Apublic final class BlockInStreamTest %7B%0A  @Test%0A  public void readFromLocal() throws Exception %7B%0A    String clientHostname = %22clientHostname%22;%0A    try (SetAndRestoreConfiguration c = new SetAndRestoreConfiguration(%0A        ImmutableMap.%3CPropertyKey, String%3Eof(PropertyKey.USER_HOSTNAME, clientHostname))) %7B%0A      long blockId = 0;%0A      FileSystemContext context = mock(FileSystemContext.class);%0A      BlockWorkerClient blockWorkerClient = mock(BlockWorkerClient.class);%0A      when(context.createBlockWorkerClient(any(WorkerNetAddress.class)))%0A          .thenReturn(blockWorkerClient);%0A      // Mock the lock result to show that the block is locked in Alluxio storage.%0A      LockBlockResult lockResult =%0A          new LockBlockResult().setLockBlockStatus(LockBlockStatus.ALLUXIO_BLOCK_LOCKED);%0A      LockBlockResource lockResource =%0A          new LockBlockResource(blockWorkerClient, lockResult, blockId);%0A      when(blockWorkerClient.lockUfsBlock(eq(blockId), any(LockBlockOptions.class)))%0A          .thenReturn(lockResource);%0A%0A      // Set the data server hostname to match the client hostname.%0A      when(blockWorkerClient.getDataServerAddress())%0A          .thenReturn(InetSocketAddress.createUnresolved(clientHostname, 0));%0A      BlockInStream stream = BlockInStream.createUfsBlockInStream(context, %22ufsPath%22, blockId, 100,%0A          0, new WorkerNetAddress(), InStreamOptions.defaults());%0A      // The client hostname matches the worker hostname, so the stream should go to a local file.%0A      Assert.assertTrue(stream.isLocal());%0A%0A      // Set the data server hostname to not match the client hostname.%0A      when(blockWorkerClient.getDataServerAddress())%0A          .thenReturn(InetSocketAddress.createUnresolved(%22remotehost%22, 0));%0A      stream = BlockInStream.createUfsBlockInStream(context, %22ufsPath%22, blockId, 100,%0A          0, new WorkerNetAddress(), InStreamOptions.defaults());%0A      // The client hostname matches the worker hostname, so the stream should go to a local file.%0A      Assert.assertFalse(stream.isLocal());%0A    %7D%0A  %7D%0A%7D%0A

Add unit test for packet block in stream locality