<commit_before>from __future__ import absolute_import

import time
from types import TracebackType
from decimal import Decimal

import pytest
from tornado import gen, ioloop
from concurrent.futures import Future, as_completed, TimeoutError

from threadloop import ThreadLoop
from threadloop.exceptions import ThreadNotStartedError


@pytest.fixture(autouse=True)
def clear_io_loop():
    # clear the current IOLoop before all tests
    ioloop.IOLoop.clear_current()


@pytest.yield_fixture
def threadloop():
    with ThreadLoop() as threadloop:
        yield threadloop


class TestException(Exception):
    pass


def test_coroutine_returns_future(threadloop):

    @gen.coroutine
    def coroutine():
        raise gen.Return("Hello World")

    future = threadloop.submit(coroutine)

    assert (
        isinstance(future, Future),
        "expected a concurrent.futures.Future"
    )

    assert future.result() == "Hello World"


def test_propogates_arguments(threadloop):

    @gen.coroutine
    def coroutine(message, adjective="Shady"):
        raise gen.Return("Hello %s %s" % (adjective, message))

    future = threadloop.submit(coroutine, "World")
    assert future.result() == "Hello Shady World"

    future = threadloop.submit(coroutine, "World", adjective="Cloudy")
    assert future.result() == "Hello Cloudy World"


def test_coroutine_exception_propagates(threadloop):

    @gen.coroutine
    def coroutine():
        raise TestException()

    with pytest.raises(TestException):
        future = threadloop.submit(coroutine)
        future.result()


def test_coroutine_exception_contains_exc_info(threadloop):

    @gen.coroutine
    def coroutine():
        raise TestException('something went wrong')

    with pytest.raises(Exception) as exc_info:
        threadloop.submit(coroutine).result()

    assert 'something went wrong' in str(exc_info.value)
    assert isinstance(exc_info.value, TestException)
    assert isinstance(exc_info.tb, TracebackType)
    assert (
        "raise TestException('something went wrong')"
        in str(exc_info.traceback[-1])
    )


def test_plain_function(threadloop):

    def not_a_coroutine():
        return "Hello World"

    future = threadloop.submit(not_a_coroutine)

    assert (
        isinstance(future, Future),
        "expected a concurrent.futures.Future"
    )

    assert future.result() == "Hello World"


def test_plain_function_exception_propagates(threadloop):<commit_msg>fix spurious assertions that were always true<commit_after>from __future__ import absolute_import

import time
from types import TracebackType
from decimal import Decimal

import pytest
from tornado import gen, ioloop
from concurrent.futures import Future, as_completed, TimeoutError

from threadloop import ThreadLoop
from threadloop.exceptions import ThreadNotStartedError


@pytest.fixture(autouse=True)
def clear_io_loop():
    # clear the current IOLoop before all tests
    ioloop.IOLoop.clear_current()


@pytest.yield_fixture
def threadloop():
    with ThreadLoop() as threadloop:
        yield threadloop


class TestException(Exception):
    pass


def test_coroutine_returns_future(threadloop):

    @gen.coroutine
    def coroutine():
        raise gen.Return("Hello World")

    future = threadloop.submit(coroutine)

    assert isinstance(future, Future), "expected a concurrent.futures.Future"

    assert future.result() == "Hello World"


def test_propogates_arguments(threadloop):

    @gen.coroutine
    def coroutine(message, adjective="Shady"):
        raise gen.Return("Hello %s %s" % (adjective, message))

    future = threadloop.submit(coroutine, "World")
    assert future.result() == "Hello Shady World"

    future = threadloop.submit(coroutine, "World", adjective="Cloudy")
    assert future.result() == "Hello Cloudy World"


def test_coroutine_exception_propagates(threadloop):

    @gen.coroutine
    def coroutine():
        raise TestException()

    with pytest.raises(TestException):
        future = threadloop.submit(coroutine)
        future.result()


def test_coroutine_exception_contains_exc_info(threadloop):

    @gen.coroutine
    def coroutine():
        raise TestException('something went wrong')

    with pytest.raises(Exception) as exc_info:
        threadloop.submit(coroutine).result()

    assert 'something went wrong' in str(exc_info.value)
    assert isinstance(exc_info.value, TestException)
    assert isinstance(exc_info.tb, TracebackType)
    assert (
        "raise TestException('something went wrong')"
        in str(exc_info.traceback[-1])
    )


def test_plain_function(threadloop):

    def not_a_coroutine():
        return "Hello World"

    future = threadloop.submit(not_a_coroutine)

    assert isinstance(future, Future), "expected a concurrent.futures.Future"

    assert future.result() == "Hello World"


def test_plain_function_exception_propagates(threadloop):
{'fraction_non_alphanumeric': 0.09027777777777778, 'fraction_numerical': 0.0004084967320261438, 'mean_word_length': 3.9918450560652396, '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': '803491', 'n_tokens_mistral': 1463, 'n_tokens_neox': 1436, 'n_words': 351}