/*
 * Licensed to the University of California, Berkeley under one or more contributor license
 * agreements. See the NOTICE file distributed with this work for additional information regarding
 * copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the License. You may obtain a
 * copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */

package tachyon.client.block;

import java.util.List;

import org.junit.Test;

import com.google.common.collect.Lists;

import tachyon.Constants;
import tachyon.client.ClientContext;

/**
 * Tests {@link BlockStoreContext}.
 */
public final class BlockStoreContextTest {

  @Test
  public void concurrencyTest() throws Exception {
    final List<BlockMasterClient> clients = Lists.newArrayList();

    // acquire all the clients
    for (int i = 0; i < ClientContext.getConf()
        .getInt(Constants.USER_BLOCK_MASTER_CLIENT_THREADS); i ++) {
      clients.add(BlockStoreContext.INSTANCE.acquireMasterClient());
    }

    (new Thread(new AcquireClient())).start();

    // wait for thread to run
    Thread.sleep(5L);

    // release all the clients
    for (BlockMasterClient client : clients) {
      BlockStoreContext.INSTANCE.releaseMasterClient(client);
    }
  }

  class AcquireClient implements Runnable {
    @Override
    public void run() {
      BlockMasterClient client = BlockStoreContext.INSTANCE.acquireMasterClient();
      BlockStoreContext.INSTANCE.releaseMasterClient(client);
    }
  }
}

@@ -837,24 +837,49 @@
 util.List;%0A%0A
+import org.junit.Assert;%0A
 import org.j
@@ -885,24 +885,24 @@
 junit.Test;%0A
-
 %0Aimport com.
@@ -1437,17 +1437,39 @@
  %7D%0A%0A    
-(
+Thread acquireThread = 
 new Thre
@@ -1491,17 +1491,35 @@
 lient())
-)
+;%0A    acquireThread
 .start()
@@ -1648,24 +1648,24 @@
  clients) %7B%0A
-
       BlockS
@@ -1715,24 +1715,447 @@
 ient);%0A    %7D
+%0A%0A    // wait for the spawned thread to complete. If it is unable to acquire a master client before%0A    // the defined timeout, fail.%0A    final long timeoutMs = 5 * Constants.SECOND_MS;%0A    long start = System.currentTimeMillis();%0A    acquireThread.join(timeoutMs);%0A    if (System.currentTimeMillis() - start %3E timeoutMs) %7B%0A      Assert.fail(%22Failed to acquire a master client within %22 + timeoutMs + %22ms. Deadlock?%22);%0A    %7D
 %0A  %7D%0A%0A  clas

Update BlockStoreContextTest.