<commit_before>package com.palantir.docker.compose.execution;

import com.palantir.docker.compose.connection.ContainerNames;
import com.palantir.docker.compose.utils.MockitoMultiAnswer;
import org.junit.Test;
import org.mockito.invocation.InvocationOnMock;

import java.io.IOException;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

public class RetryingDockerComposeShould {
    private static final Object SUCCESS = null;

    private final DockerCompose dockerCompose = mock(DockerCompose.class);
    private final RetryingDockerCompose retryingDockerCompose = new RetryingDockerCompose(2, dockerCompose);
    private final ContainerNames someContainerNames = new ContainerNames("hey");

    @Test
    public void not_retry_if_the_up_command_was_successful() throws IOException, InterruptedException {
        retryingDockerCompose.up();
        verify(dockerCompose).up();
        verifyNoMoreInteractions(dockerCompose);
    }

    @Test
    public void retry_up_if_the_command_failed_once() throws IOException, InterruptedException {
        doAnswer(new MockitoMultiAnswer() {
            @Override
            protected Object firstCall(InvocationOnMock invocation) throws Exception {
                throw new DockerComposeExecutionException();
            }

            @Override
            protected Object secondCall(InvocationOnMock invocation) throws Exception {
                return SUCCESS;
            }
        }).when(dockerCompose).up();

        retryingDockerCompose.up();
        verify(dockerCompose, times(2)).up();
        verifyNoMoreInteractions(dockerCompose);
    }

    @Test
    public void throw_the_last_exception_when_up_fails_more_times_than_the_specified_attempts() throws IOException, InterruptedException {
        DockerComposeExecutionException finalException = new DockerComposeExecutionException();
        doAnswer(new MockitoMultiAnswer() {
            @Override
            protected Object firstCall(InvocationOnMock invocation) throws Exception {
                throw new DockerComposeExecutionException();
            }

            @Override
            protected Object secondCall(InvocationOnMock invocation) throws Exception {
                throw finalException;
            }
        }).when(dockerCompose).up();

        try {
            retryingDockerCompose.up();
            fail("Should have caught exception");
        } catch (DockerComposeExecutionException actualException) {
            assertThat(actualException, is(finalException));
        }

        verify(dockerCompose, times(2)).up();
        verifyNoMoreInteractions(dockerCompose);
    }

    @Test
    public void not_retry_if_the_ps_command_was_successful_and_return_the_correct_container_names() throws IOException, InterruptedException {
        when(dockerCompose.ps()).thenReturn(someContainerNames);

        assertThat(retryingDockerCompose.ps(), is(someContainerNames));
        verify(dockerCompose).ps();
        verifyNoMoreInteractions(dockerCompose);
    }

    @Test
    public void retry_ps_if_the_command_failed_once_and_return_the_last_container_names() throws IOException, InterruptedException {
        when(dockerCompose.ps()).thenAnswer(new MockitoMultiAnswer() {
            @Override
            protected Object firstCall(InvocationOnMock invocation) throws Exception {
                throw new DockerComposeExecutionException();
            }

            @Override
            protected Object secondCall(InvocationOnMock invocation) throws Exception {
                return someContainerNames;
            }
        });

        assertThat(retryingDockerCompose.ps(), is(someContainerNames));
        verify(dockerCompose, times(2)).ps();
        verifyNoMoreInteractions(dockerCompose);
    }

    @Test
    public void throw_the_last_exception_when_ps_fails_more_times_than_the_specified_attempts() throws IOException, InterruptedException {
        DockerComposeExecutionException finalException = new DockerComposeExecutionException();
        when(dockerCompose.ps()).thenAnswer(new MockitoMultiAnswer() {
            @Override
            protected Object firstCall(InvocationOnMock invocation) throws Exception {
                throw new DockerComposeExecutionException();
            }

            @Override
            protected Object secondCall(InvocationOnMock invocation) throws Exception {
                throw finalException;
            }
        });

        try {
            retryingDockerCompose.ps();
            fail("Should have caught exception");
        } catch (DockerComposeExecutionException actualException) {
            assertThat(actualException, is(finalException));
        }

        verify(dockerCompose, times(2)).ps();
        verifyNoMoreInteractions(dockerCompose);
    }
}<commit_msg>Cut down tests on RetryingDockerComposeShould<commit_after>package com.palantir.docker.compose.execution;

import com.palantir.docker.compose.connection.ContainerNames;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;

import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;

public class RetryingDockerComposeShould {
    private final DockerCompose dockerCompose = mock(DockerCompose.class);
    private final Retryer retryer = mock(Retryer.class);
    private final RetryingDockerCompose retryingDockerCompose = new RetryingDockerCompose(retryer, dockerCompose);
    private final ContainerNames someContainerNames = new ContainerNames("hey");

    @Before
    public void before() throws IOException, InterruptedException {
        retryerJustCallsOperation();
    }

    private void retryerJustCallsOperation() throws IOException, InterruptedException {
        when(retryer.runWithRetries(any(Retryer.RetryableDockerComposeOperation.class))).thenAnswer(invocation -> {
            Retryer.RetryableDockerComposeOperation operation = (Retryer.RetryableDockerComposeOperation) invocation.getArguments()[0];
            return operation.call();
        });
    }

    @Test
    public void calls_up_on_the_underlying_docker_compose() throws IOException, InterruptedException {
        retryingDockerCompose.up();

        verify(dockerCompose).up();
        verifyNoMoreInteractions(dockerCompose);
    }

    @Test
    public void call_ps_on_the_underlying_docker_compose_and_returns_the_same_value() throws IOException, InterruptedException {
        when(dockerCompose.ps()).thenReturn(someContainerNames);

        assertThat(retryingDockerCompose.ps(), is(someContainerNames));
        verify(dockerCompose).ps();
        verifyNoMoreInteractions(dockerCompose);
    }
}
{'fraction_non_alphanumeric': 0.07722276332266592, 'fraction_numerical': 0.0008348406845693614, 'mean_word_length': 3.5493670886075948, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1569727', 'n_tokens_mistral': 1937, 'n_tokens_neox': 1858, 'n_words': 351}