<commit_before>from .._defer import GatherDeferredsAPI, gather_deferreds

from twisted.internet.defer import fail, FirstError, succeed
from twisted.python import log
from twisted.python.failure import Failure
from twisted.trial.unittest import TestCase


class GatherDeferredsTests(TestCase):
    """
    Tests for ``gather_deferreds``.
    """
    def test_gather_deferreds_api(self):
        """
        ``gather_deferreds`` is bound to an instance of ``GatherDeferredsAPI``.
        """
        self.assertIs(GatherDeferredsAPI, gather_deferreds.im_class)

    def test_success(self):
        """
        The successful results of the supplied ``Deferred``s are returned.
        """
        expected_result1 = object()
        expected_result2 = object()

        d = GatherDeferredsAPI(log_errors=False).gather_deferreds(
            [succeed(expected_result1), succeed(expected_result2)])

        results = self.successResultOf(d)
        self.assertEqual([expected_result1, expected_result2], results)

    def test_consume_errors_true(self):
        """
        The unhandled errbacks in the supplied ``Deferred``s are handled.
        """
        d = GatherDeferredsAPI(log_errors=False).gather_deferreds(
            [fail(ZeroDivisionError('test_consume_errors'))])

        self.failureResultOf(d, FirstError)
        self.assertEqual([], self.flushLoggedErrors(ZeroDivisionError))

    def test_fire_on_first_failure(self):
        """
        The first errback in the supplied list of deferreds causes the returned
        deferred to errback with that failure.
        """
        expected_error = ZeroDivisionError('test_fire_on_first_failure')
        d = GatherDeferredsAPI(log_errors=False).gather_deferreds(
            [fail(expected_error)])

        failure = self.failureResultOf(d, FirstError)
        self.assertEqual(expected_error, failure.value.subFailure.value)

    def test_logging(self):
        """
        Failures in the supplied deferreds are all logged.
        """
        messages = []
        log.addObserver(messages.append)
        self.addCleanup(log.removeObserver, messages.append)
        expected_failure1 = Failure(ZeroDivisionError('test_logging1'))
        expected_failure2 = Failure(ZeroDivisionError('test_logging2'))

        d = GatherDeferredsAPI(log_errors=True).gather_deferreds(
            [fail(expected_failure1), fail(expected_failure2)])

        self.failureResultOf(d, FirstError)
        self.assertEqual(
            [expected_failure1, expected_failure2],
            self.flushLoggedErrors(ZeroDivisionError)
        )
<commit_msg>Add multiple failures to tests that assert about FirstError<commit_after>from .._defer import GatherDeferredsAPI, gather_deferreds

from twisted.internet.defer import fail, FirstError, succeed
from twisted.python import log
from twisted.python.failure import Failure
from twisted.trial.unittest import TestCase


class GatherDeferredsTests(TestCase):
    """
    Tests for ``gather_deferreds``.
    """
    def test_gather_deferreds_api(self):
        """
        ``gather_deferreds`` is bound to an instance of ``GatherDeferredsAPI``.
        """
        self.assertIs(GatherDeferredsAPI, gather_deferreds.im_class)

    def test_success(self):
        """
        The successful results of the supplied ``Deferred``s are returned.
        """
        expected_result1 = object()
        expected_result2 = object()

        d = GatherDeferredsAPI(log_errors=False).gather_deferreds(
            [succeed(expected_result1), succeed(expected_result2)])

        results = self.successResultOf(d)
        self.assertEqual([expected_result1, expected_result2], results)

    def test_consume_errors_true(self):
        """
        The unhandled errbacks in the supplied ``Deferred``s are handled.
        """
        d = GatherDeferredsAPI(log_errors=False).gather_deferreds(
            [fail(ZeroDivisionError('test_consume_errors1')),
             fail(ZeroDivisionError('test_consume_errors2'))])

        self.failureResultOf(d, FirstError)
        self.assertEqual([], self.flushLoggedErrors(ZeroDivisionError))

    def test_fire_on_first_failure(self):
        """
        The first errback in the supplied list of deferreds causes the returned
        deferred to errback with that failure.
        """
        expected_error = ZeroDivisionError('test_fire_on_first_failure1')
        d = GatherDeferredsAPI(log_errors=False).gather_deferreds(
            [fail(expected_error),
             fail(ZeroDivisionError('test_fire_on_first_failure2'))])

        failure = self.failureResultOf(d, FirstError)
        self.assertEqual(expected_error, failure.value.subFailure.value)

    def test_logging(self):
        """
        Failures in the supplied deferreds are all logged.
        """
        messages = []
        log.addObserver(messages.append)
        self.addCleanup(log.removeObserver, messages.append)
        expected_failure1 = Failure(ZeroDivisionError('test_logging1'))
        expected_failure2 = Failure(ZeroDivisionError('test_logging2'))

        d = GatherDeferredsAPI(log_errors=True).gather_deferreds(
            [fail(expected_failure1), fail(expected_failure2)])

        self.failureResultOf(d, FirstError)
        self.assertEqual(
            [expected_failure1, expected_failure2],
            self.flushLoggedErrors(ZeroDivisionError)
        )

{'fraction_non_alphanumeric': 0.09247910863509749, 'fraction_numerical': 0.005942432683379758, 'mean_word_length': 3.178432893716059, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5879404', 'n_tokens_mistral': 1538, 'n_tokens_neox': 1515, 'n_words': 290}