/*
 * Licensed to the Apache Software Foundation (ASF) 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 org.apache.hadoop.hbase.ipc;

import static org.apache.hadoop.hbase.ipc.RWQueueRpcExecutor.CALL_QUEUE_READ_SHARE_CONF_KEY;
import static org.apache.hadoop.hbase.ipc.RWQueueRpcExecutor.CALL_QUEUE_SCAN_SHARE_CONF_KEY;
import static org.apache.hadoop.hbase.ipc.RpcExecutor.CALL_QUEUE_HANDLER_FACTOR_CONF_KEY;
import static org.apache.hadoop.hbase.ipc.RpcExecutor.DEFAULT_CALL_QUEUE_SIZE_HARD_LIMIT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;

import java.util.List;
import java.util.concurrent.BlockingQueue;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.testclassification.MediumTests;
import org.apache.hadoop.hbase.testclassification.RPCTests;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInfo;

@Tag(RPCTests.TAG)
@Tag(MediumTests.TAG)
public class TestRWQueueRpcExecutor {

  private Configuration conf;
  private String testMethodName;

  @BeforeEach
  public void setUp(TestInfo testInfo) {
    testMethodName = testInfo.getTestMethod().get().getName();
    conf = HBaseConfiguration.create();
    conf.setFloat(CALL_QUEUE_HANDLER_FACTOR_CONF_KEY, 1.0f);
    conf.setFloat(CALL_QUEUE_SCAN_SHARE_CONF_KEY, 0.5f);
    conf.setFloat(CALL_QUEUE_READ_SHARE_CONF_KEY, 0.5f);
  }

  @Test
  public void itProvidesCorrectQueuesToBalancers() throws InterruptedException {
    PriorityFunction qosFunction = mock(PriorityFunction.class);
    int softQueueLimit = 100;
    RWQueueRpcExecutor executor =
      new RWQueueRpcExecutor(testMethodName, 100, softQueueLimit, qosFunction, conf, null);

    QueueBalancer readBalancer = executor.getReadBalancer();
    QueueBalancer writeBalancer = executor.getWriteBalancer();
    QueueBalancer scanBalancer = executor.getScanBalancer();

    assertTrue(readBalancer instanceof RandomQueueBalancer);
    assertTrue(writeBalancer instanceof RandomQueueBalancer);
    assertTrue(scanBalancer instanceof RandomQueueBalancer);

    List<BlockingQueue<CallRunner>> readQueues = ((RandomQueueBalancer) readBalancer).getQueues();
    List<BlockingQueue<CallRunner>> writeQueues = ((RandomQueueBalancer) writeBalancer).getQueues();
    List<BlockingQueue<CallRunner>> scanQueues = ((RandomQueueBalancer) scanBalancer).getQueues();

    assertEquals(25, readQueues.size());
    assertEquals(50, writeQueues.size());
    assertEquals(25, scanQueues.size());
    assertEquals(softQueueLimit, executor.currentQueueLimit, "Soft limit is not applied properly");
    // Hard Limit is applied as the max capacity of the queue
    int hardQueueLimit = readQueues.get(0).remainingCapacity() + readQueues.get(0).size();
    assertEquals(DEFAULT_CALL_QUEUE_SIZE_HARD_LIMIT, hardQueueLimit,
      "Default hard limit should be applied ");

    verifyDistinct(readQueues, writeQueues, scanQueues);
    verifyDistinct(writeQueues, readQueues, scanQueues);
    verifyDistinct(scanQueues, readQueues, writeQueues);

  }

  private void verifyDistinct(List<BlockingQueue<CallRunner>> queues,
    List<BlockingQueue<CallRunner>>... others) throws InterruptedException {
    CallRunner mock = mock(CallRunner.class);
    for (BlockingQueue<CallRunner> queue : queues) {
      queue.put(mock);
      assertEquals(1, queue.size());
    }

    for (List<BlockingQueue<CallRunner>> other : others) {
      for (BlockingQueue<CallRunner> queue : other) {
        assertEquals(0, queue.size());
      }
    }

    // clear them for next test
    for (BlockingQueue<CallRunner> queue : queues) {
      queue.clear();
    }
  }
}
