/**
 * 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.curator.test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Semaphore;
import java.util.concurrent.TimeUnit;

/**
 * Utility to get various testing times
 */
public class Timing
{
    private final long value;
    private final TimeUnit unit;
    private final int waitingMultiple;

    private static final int DEFAULT_SECONDS = 10;
    private static final int DEFAULT_WAITING_MULTIPLE = 5;
    private static final double SESSION_MULTIPLE = .25;

    /**
     * Use the default base time
     */
    public Timing()
    {
        this
        (
            Integer.getInteger("timing-multiple", 1),
            Integer.getInteger("timing-waiting-multiple", DEFAULT_WAITING_MULTIPLE)
        );
    }

    /**
     * Use a multiple of the default base time
     *
     * @param multiple the multiple
     */
    public Timing(double multiple)
    {
        this((long)(DEFAULT_SECONDS * multiple), TimeUnit.SECONDS, DEFAULT_WAITING_MULTIPLE);
    }

    /**
     * Use a multiple of the default base time
     *
     * @param multiple the multiple
     * @param waitingMultiple multiple of main timing to use when waiting
     */
    public Timing(double multiple, int waitingMultiple)
    {
        this((long)(DEFAULT_SECONDS * multiple), TimeUnit.SECONDS, waitingMultiple);
    }

    /**
     * @param value base time
     * @param unit  base time unit
     */
    public Timing(long value, TimeUnit unit)
    {
        this(value, unit, DEFAULT_WAITING_MULTIPLE);
    }

    /**
     * @param value base time
     * @param unit  base time unit
     * @param waitingMultiple multiple of main timing to use when waiting
     */
    public Timing(long value, TimeUnit unit, int waitingMultiple)
    {
        this.value = value;
        this.unit = unit;
        this.waitingMultiple = waitingMultiple;
    }

    /**
     * Return the base time in milliseconds
     *
     * @return time ms
     */
    public int milliseconds()
    {
        return (int)TimeUnit.MILLISECONDS.convert(value, unit);
    }

    /**
     * Return the base time in seconds
     *
     * @return time secs
     */
    public int seconds()
    {
        return (int)value;
    }

    /**
     * Wait on the given latch
     *
     * @param latch latch to wait on
     * @return result of {@link CountDownLatch#await(long, TimeUnit)}
     */
    public boolean awaitLatch(CountDownLatch latch)
    {
        Timing m = forWaiting();
        try
        {
            return latch.await(m.value, m.unit);
        }
        catch ( InterruptedException e )
        {
            Thread.currentThread().interrupt();
        }
        return false;
    }

    /**
     * Wait on the given semaphore
     *
     * @param semaphore the semaphore
     * @return result of {@link Semaphore#tryAcquire()}
     */
    public boolean acquireSemaphore(Semaphore semaphore)
    {
        Timing m = forWaiting();
        try
        {
            return semaphore.tryAcquire(m.value, m.unit);
        }
        catch ( InterruptedException e )
        {
            Thread.currentThread().interrupt();
        }
        return false;
    }

    /**
     * Wait on the given semaphore
     *
     * @param semaphore the semaphore
     * @param n         number of permits to acquire
     * @return result of {@link Semaphore#tryAcquire(int, long, TimeUnit)}
     */
    public boolean acquireSemaphore(Semaphore semaphore, int n)
    {
        Timing m = forWaiting();
        try
        {
            return semaphore.tryAcquire(n, m.value, m.unit);
        }
        catch ( InterruptedException e )
        {
            Thread.currentThread().interrupt();
        }
        return false;
    }

    /**
     * Return a new timing that is a multiple of the this timing
     *
     * @param n the multiple
     * @return this timing times the multiple
     */
    public Timing multiple(double n)
    {
        return new Timing((int)(value * n), unit);
    }

    /**
     * Return a new timing with the standard multiple for waiting on latches, etc.
     *
     * @return this timing multiplied
     */
    @SuppressWarnings("PointlessArithmeticExpression")
    public Timing forWaiting()
    {
        return multiple(waitingMultiple);
    }

    /**
     * Sleep for a small amount of time
     *
     * @throws InterruptedException if interrupted
     */
    public void sleepABit() throws InterruptedException
    {
        unit.sleep(value / 4);
    }

    /**
     * Return the value to use for ZK session timeout
     *
     * @return session timeout
     */
    public int session()
    {
        return multiple(SESSION_MULTIPLE).milliseconds();
    }

    /**
     * Return the value to use for ZK connection timeout
     *
     * @return connection timeout
     */
    public int connection()
    {
        return milliseconds();
    }
}

@@ -1390,31 +1390,9 @@
 this
-%0A        (%0A            
+(
 Inte
@@ -1432,101 +1432,29 @@
  1),
-%0A            Integer.getInteger(%22timing-w
+ getW
 aiting
--m
+M
 ultiple
-%22, DEFAULT_WAITING_MULTIPLE)%0A        
+()
 );%0A 
@@ -1677,32 +1677,28 @@
 DS, 
-DEFAULT_WAITING_MULTIPLE
+getWaitingMultiple()
 );%0A 
@@ -2199,32 +2199,28 @@
 it, 
-DEFAULT_WAITING_MULTIPLE
+getWaitingMultiple()
 );%0A 
@@ -5566,11 +5566,160 @@
 );%0A    %7D
+%0A%0A    private static Integer getWaitingMultiple()%0A    %7B%0A        return Integer.getInteger(%22timing-waiting-multiple%22, DEFAULT_WAITING_MULTIPLE);%0A    %7D
 %0A%7D%0A

Make sure system prop timing-waiting-multiple is always used