/*
 * The Alluxio Open Foundation licenses this work under the Apache License, version 2.0
 * (the "License"). You may not use this work except in compliance with the License, which is
 * available at www.apache.org/licenses/LICENSE-2.0
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied, as more fully set forth in the License.
 *
 * See the NOTICE file distributed with this work for information regarding copyright ownership.
 */

package alluxio.client.file.policy;

import alluxio.CommonTestUtils;
import alluxio.Constants;
import alluxio.client.block.BlockWorkerInfo;
import alluxio.util.network.NetworkAddressUtils;
import alluxio.wire.WorkerNetAddress;

import org.junit.Assert;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * Tests {@link LocalFirstPolicy}.
 */
public final class LocalFirstPolicyTest {
  private static final int PORT = 1;

  /**
   * Tests that the local host is returned first.
   */
  @Test
  public void getLocalFirst() {
    String localhostName = NetworkAddressUtils.getLocalHostName();
    LocalFirstPolicy policy = new LocalFirstPolicy();
    List<BlockWorkerInfo> workerInfoList = new ArrayList<>();
    workerInfoList.add(new BlockWorkerInfo(new WorkerNetAddress().setHost("worker1")
        .setRpcPort(PORT).setDataPort(PORT).setWebPort(PORT), Constants.GB, 0));
    workerInfoList.add(new BlockWorkerInfo(new WorkerNetAddress().setHost(localhostName)
        .setRpcPort(PORT).setDataPort(PORT).setWebPort(PORT), Constants.GB, 0));
    Assert.assertEquals(localhostName,
        policy.getWorkerForNextBlock(workerInfoList, Constants.MB).getHost());
  }

  /**
   * Tests that another worker is picked in case the local host does not have enough space.
   */
  @Test
  public void getOthersWhenNotEnoughSpaceOnLocal() {
    String localhostName = NetworkAddressUtils.getLocalHostName();
    LocalFirstPolicy policy = new LocalFirstPolicy();
    List<BlockWorkerInfo> workerInfoList = new ArrayList<>();
    workerInfoList.add(new BlockWorkerInfo(new WorkerNetAddress().setHost("worker1")
        .setRpcPort(PORT).setDataPort(PORT).setWebPort(PORT), Constants.GB, 0));
    workerInfoList.add(new BlockWorkerInfo(new WorkerNetAddress().setHost(localhostName)
        .setRpcPort(PORT).setDataPort(PORT).setWebPort(PORT), Constants.MB, Constants.MB));
    Assert.assertEquals("worker1",
        policy.getWorkerForNextBlock(workerInfoList, Constants.GB).getHost());
  }

  @Test
  public void equalsTest() throws Exception {
    CommonTestUtils.testEquals(LocalFirstPolicy.class);
  }
}

@@ -2517,16 +2517,929 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Tests that non-local workers are randomly selected.%0A   */%0A  @Test%0A  public void getOthersRandomly() %7B%0A    LocalFirstPolicy policy = new LocalFirstPolicy();%0A    List%3CBlockWorkerInfo%3E workerInfoList = new ArrayList%3C%3E();%0A    workerInfoList.add(new BlockWorkerInfo(new WorkerNetAddress().setHost(%22worker1%22)%0A        .setRpcPort(PORT).setDataPort(PORT).setWebPort(PORT), Constants.GB, 0));%0A    workerInfoList.add(new BlockWorkerInfo(new WorkerNetAddress().setHost(%22worker2%22)%0A        .setRpcPort(PORT).setDataPort(PORT).setWebPort(PORT), Constants.GB, Constants.MB));%0A    boolean success = false;%0A    for (int i = 0; i %3C 100; i++) %7B%0A      String host = policy.getWorkerForNextBlock(workerInfoList, Constants.GB).getHost();%0A      if (!host.equals(policy.getWorkerForNextBlock(workerInfoList, Constants.GB).getHost())) %7B%0A        success = true;%0A        break;%0A      %7D%0A    %7D%0A    Assert.assertTrue(success);%0A  %7D%0A%0A
   @Test%0A

Add a unit test.