/*
 * 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.master.block;

import static org.junit.Assert.assertEquals;

import alluxio.AlluxioURI;
import alluxio.BaseIntegrationTest;
import alluxio.Constants;
import alluxio.IntegrationTestUtils;
import alluxio.LocalAlluxioClusterResource;
import alluxio.PropertyKey;
import alluxio.client.WriteType;
import alluxio.client.file.FileInStream;
import alluxio.client.file.FileSystem;
import alluxio.client.file.FileSystemTestUtils;
import alluxio.client.file.options.CreateFileOptions;
import alluxio.client.file.policy.RoundRobinPolicy;

import org.apache.commons.io.IOUtils;
import org.junit.Rule;
import org.junit.Test;

/**
 * Tests a cluster containing multiple workers.
 */
public final class MultiWorkerIntegrationTest extends BaseIntegrationTest {
  private static final int NUM_WORKERS = 4;
  private static final int WORKER_MEMORY_SIZE_BYTES = Constants.MB;
  private static final int BLOCK_SIZE_BYTES = WORKER_MEMORY_SIZE_BYTES / 2;

  @Rule
  public LocalAlluxioClusterResource mResource =
      new LocalAlluxioClusterResource.Builder()
          .setProperty(PropertyKey.WORKER_MEMORY_SIZE, WORKER_MEMORY_SIZE_BYTES)
          .setProperty(PropertyKey.USER_BLOCK_SIZE_BYTES_DEFAULT, BLOCK_SIZE_BYTES)
          .setProperty(PropertyKey.USER_FILE_BUFFER_BYTES, BLOCK_SIZE_BYTES)
          .setNumWorkers(NUM_WORKERS)
          .build();

  @Test
  public void writeLargeFile() throws Exception {
    int fileSize = NUM_WORKERS * WORKER_MEMORY_SIZE_BYTES;
    AlluxioURI file = new AlluxioURI("/test");
    FileSystem fs = mResource.get().getClient();
    // Write a file large enough to fill all the memory of all the workers.
    FileSystemTestUtils.createByteFile(fs, file.getPath(), fileSize,
        CreateFileOptions.defaults().setWriteType(WriteType.MUST_CACHE)
            .setLocationPolicy(new RoundRobinPolicy()));
    IntegrationTestUtils.waitForFileCached(fs, file, 1000);
    try (FileInStream inStream = fs.openFile(file)) {
      assertEquals(fileSize, IOUtils.toByteArray(inStream).length);
    }
  }
}

@@ -677,45 +677,8 @@
 ts;%0A
-import alluxio.IntegrationTestUtils;%0A
 impo
@@ -745,16 +745,48 @@
 rtyKey;%0A
+import alluxio.client.ReadType;%0A
 import a
@@ -969,22 +969,112 @@
 ile.
-options.Create
+URIStatus;%0Aimport alluxio.client.file.options.CreateFileOptions;%0Aimport alluxio.client.file.options.Open
 File
@@ -2442,63 +2442,162 @@
     
-IntegrationTestUtils.waitForFileCached(fs, file, 1000);
+URIStatus status = fs.getStatus(file);%0A    assertEquals(100, status.getInAlluxioPercentage());%0A    // Use NO_CACHE to prevent evicting %22none-local%22 blocks
 %0A   
@@ -2628,16 +2628,25 @@
 eam = fs
+%0A        
 .openFil
@@ -2651,16 +2651,75 @@
 ile(file
+, OpenFileOptions.defaults().setReadType(ReadType.NO_CACHE)
 )) %7B%0A   

Fix failing test alluxio.master.block.MultiWorkerIntegrationTest#writeLargeFile