package com.atlassian.bitbucket.mesh.throttle;

import org.junit.Test;

import java.util.OptionalLong;

import static com.atlassian.bitbucket.mesh.throttle.ThrottledResource.COMMAND;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;

public class FixedResourceThrottleStrategyTest {

    @Test
    public void testCreate() {
        SemaphoreTicketBucket
                bucket = new FixedResourceThrottleStrategy(COMMAND, 100, 10).create();

        assertNotNull(bucket);
        assertEquals(100, MILLISECONDS.toSeconds(bucket.getDefaultAcquireTimeoutMillis()));
        assertEquals(10, bucket.getTotalTickets());
    }

    @Test
    public void testGetUpdateInterval() {
        assertEquals(OptionalLong.empty(),
                new FixedResourceThrottleStrategy(COMMAND, 100, 10).getUpdateInterval(SECONDS));
    }

    @Test
    public void testUpdate() {
        SemaphoreTicketBucket bucket = mock(SemaphoreTicketBucket.class);
        new FixedResourceThrottleStrategy(COMMAND, 100, 10).update(bucket);
        verify(bucket).setAcquireTimeoutMillis(100_000L);
        verify(bucket).setPermits(10);
    }
}
