package com.atlassian.bitbucket.mesh.config;

import org.junit.Test;
import org.springframework.mock.env.MockEnvironment;

import static com.atlassian.bitbucket.mesh.config.DefaultHooksConfig.*;
import static org.junit.Assert.assertEquals;

public class DefaultHooksConfigTest {

    @Test
    public void testThreadExpressions() {
        int cpus = Runtime.getRuntime().availableProcessors();

        DefaultHooksConfig config = forThreads("cpu", "4*cpu", 5);

        assertEquals(cpus, config.getThreadsCore());
        assertEquals(4L * cpus, config.getThreadsMax());
    }

    private static DefaultHooksConfig forThreads(String coreThreads, String maxThreads, int keepAlive) {
        MockEnvironment environment = new MockEnvironment();
        environment.setProperty(PROP_THREADS_CORE, coreThreads);
        environment.setProperty(PROP_THREADS_MAX, maxThreads);
        environment.setProperty(PROP_THREADS_KEEP_ALIVE, String.valueOf(keepAlive));

        return new DefaultHooksConfig(environment);
    }
}