<commit_before>package com.centurylink.cloud.sdk.common.management.services.domain.queue.job.future;

import com.centurylink.cloud.sdk.common.management.services.domain.queue.job.future.exceptions.JobFailedException;
import com.centurylink.cloud.sdk.common.management.services.domain.queue.job.future.waiting.SingleWaitingLoop;
import com.centurylink.cloud.sdk.common.management.services.domain.queue.job.future.waiting.WaitingLoop;
import com.centurylink.cloud.sdk.core.exceptions.ClcException;
import org.testng.annotations.Test;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletionException;

import static java.time.temporal.ChronoUnit.MINUTES;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.rangeClosed;
import static org.testng.Assert.*;

public class ParallelJobsFutureTest {

    private List<JobFuture> singleJobFutures(int count) {
        return rangeClosed(1, count)
            .mapToObj(i -> new TestJobFuture())
            .collect(toList());
    }

    class TestJobFuture extends AbstractSingleJobFuture {
        @Override
        protected String operationInfo() {
            return "Mocked Operation";
        }

        @Override
        public WaitingLoop waitingLoop() {
            return new SingleWaitingLoop(() -> {
                throw new ClcException("Testing Error");
            });
        }
    }

    @Test
    public void testWaitUntilComplete() throws Exception {
        try {
            new ParallelJobsFuture(singleJobFutures(2))
                .waitUntilComplete();

            fail("Parallel future must throw exception");
        } catch (Exception ex) {
            assert ex instanceof JobFailedException;
            assertEquals(ex.getSuppressed().length, 2);
        }
    }

    @Test
    public void testWaitUntilCompleteWithDuration() throws Exception {
        try {
            new ParallelJobsFuture(singleJobFutures(3))
                .waitUntilComplete(Duration.of(10, MINUTES));

            fail("Parallel future must throw exception");
        } catch (Exception ex) {
            assert ex instanceof JobFailedException;
            assertEquals(ex.getSuppressed().length, 3);
        }
    }

    @Test
    public void testWaitAsync() throws Exception {
        try {
            new ParallelJobsFuture(singleJobFutures(3))
                .waitAsync()
                .join();

            fail("Parallel future must throw exception");
        } catch (CompletionException ex) {
            Throwable cause = ex.getCause();

            assert cause instanceof JobFailedException;
            assertEquals(cause.getSuppressed().length, 3);
        }
    }

}<commit_msg>Development prototype of method that allow to collect error messages during batch operations #26<commit_after>package com.centurylink.cloud.sdk.common.management.services.domain.queue.job.future;

import com.centurylink.cloud.sdk.common.management.services.domain.queue.job.future.exceptions.JobFailedException;
import com.centurylink.cloud.sdk.common.management.services.domain.queue.job.future.waiting.SingleWaitingLoop;
import com.centurylink.cloud.sdk.common.management.services.domain.queue.job.future.waiting.WaitingLoop;
import com.centurylink.cloud.sdk.core.exceptions.ClcException;
import org.testng.annotations.Test;

import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletionException;

import static java.time.temporal.ChronoUnit.MINUTES;
import static java.util.stream.Collectors.toList;
import static java.util.stream.IntStream.rangeClosed;
import static org.testng.Assert.*;

public class ParallelJobsFutureTest {

    /**
     * Stub for failed job future
     */
    class FailedJobFuture extends AbstractSingleJobFuture {
        @Override
        protected String operationInfo() {
            return "Mocked Operation";
        }

        @Override
        public WaitingLoop waitingLoop() {
            return new SingleWaitingLoop(() -> {
                throw new ClcException("Testing Error");
            });
        }
    }

    private List<JobFuture> failedJobFutures(int count) {
        return rangeClosed(1, count)
            .mapToObj(i -> new FailedJobFuture())
            .collect(toList());
    }

    /**
     * Stub for succeeded job future
     */
    class SuccessJobFuture extends AbstractSingleJobFuture {

        @Override
        protected String operationInfo() {
            return "Mocked Operation";
        }

        @Override
        public WaitingLoop waitingLoop() {
            return new SingleWaitingLoop(() -> true);
        }

    }

    private List<JobFuture> successJobFutures(int count) {
        return rangeClosed(1, count)
            .mapToObj(i -> new SuccessJobFuture())
            .collect(toList());
    }

    @Test(timeOut = 1000L)
    public void testWaitUntilComplete() throws Exception {
        new ParallelJobsFuture(successJobFutures(4)).waitUntilComplete();
    }

    @Test
    public void testWaitUntilComplete_failed() throws Exception {
        try {
            new ParallelJobsFuture(failedJobFutures(2))
                .waitUntilComplete();

            fail("Parallel future must throw exception");
        } catch (Exception ex) {
            assert ex instanceof JobFailedException;
            assertEquals(ex.getSuppressed().length, 2);
        }
    }

    @Test
    public void testWaitUntilCompleteWithDuration_failed() throws Exception {
        try {
            new ParallelJobsFuture(failedJobFutures(3))
                .waitUntilComplete(Duration.of(10, MINUTES));

            fail("Parallel future must throw exception");
        } catch (Exception ex) {
            assert ex instanceof JobFailedException;
            assertEquals(ex.getSuppressed().length, 3);
        }
    }

    @Test
    public void testWaitUntilCompleteWithDuration_timedOut() throws Exception {
        try {
            new ParallelJobsFuture(failedJobFutures(3))
                .waitUntilComplete(Duration.of(10, MINUTES));

            fail("Parallel future must throw exception");
        } catch (Exception ex) {
            assert ex instanceof JobFailedException;
            assertEquals(ex.getSuppressed().length, 3);
        }
    }

    @Test(timeOut = 1000L)
    public void testWaitAsync() throws Exception {
        new ParallelJobsFuture(successJobFutures(3))
            .waitAsync()
            .join();
    }

    @Test
    public void testWaitAsync_failed() throws Exception {
        try {
            new ParallelJobsFuture(failedJobFutures(3))
                .waitAsync()
                .join();

            fail("Parallel future must throw exception");
        } catch (CompletionException ex) {
            Throwable cause = ex.getCause();

            assert cause instanceof JobFailedException;
            assertEquals(cause.getSuppressed().length, 3);
        }
    }

}
{'fraction_non_alphanumeric': 0.09301990193250649, 'fraction_numerical': 0.005047591577732911, 'mean_word_length': 2.5007571933366988, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6278772', 'n_tokens_mistral': 1850, 'n_tokens_neox': 1800, 'n_words': 384}