<commit_before>import threading
try:
    import greenlet
except ImportError:
    greenlet = None
from attest import Tests, assert_hook, raises
from sider.threadlocal import get_ident


tests = Tests()


@tests.test
def get_ident_thread():
    result = [None, None]
    def test(value, result_idx):
        a = {get_ident(): value}
        result[result_idx] = a.get(get_ident())
    t1 = threading.Thread(target=test, args=(123, 0))
    t2 = threading.Thread(target=test, args=(456, 1))
    t1.start()
    t2.start()
    t1.join()
    t2.join()
    assert result == [123, 456]


if greenlet:
    @tests.test
    def get_ident_greenlet():
        result = [None, None]
        def test(value, result_idx, next_):
            a = {get_ident(): value}
            next_[0].switch(*next_[1:])
            result[result_idx] = a.get(get_ident())
            if next_:
                next_[0].switch(*next_[1:])
        g1 = greenlet.greenlet(test)
        g2 = greenlet.greenlet(test)
        g1.switch(123, 0, (g2, 456, 1, (g1, 123, 0, (g2, 456, 1, None))))
        assert result == [123, 456]
<commit_msg>Did refactoring for threadlocal test<commit_after>import threading
try:
    import greenlet
except ImportError:
    greenlet = None
from attest import Tests, assert_hook, raises
from sider.threadlocal import get_ident


tests = Tests()


def thread_test(generator, args1=(), args2=()):
    def test(g, *args):
        g.next()
        for arg in args:
            try:
                g.send(arg)
            except StopIteration:
                break
    t1 = threading.Thread(target=test, args=(generator(),) + args1)
    t2 = threading.Thread(target=test, args=(generator(),) + args2)
    t1.start()
    t2.start()
    t1.join()
    t2.join()


if greenlet:
    def coro_test(generator, args1=(), args2=()):
        def test(g, (cc, next_args), *args):
            g.next()
            for arg in args:
                try:
                    g.send(arg)
                except StopIteration:
                    break
                finally:
                    if cc is not None:
                        cc.switch(*next_args)
        c1 = greenlet.greenlet(test)
        c2 = greenlet.greenlet(test)
        c1.switch(generator(),
                  (c2, (generator(), (c1, ())) + args2),
                  *args1)
else:
    def coro_test(*a, **k):
        pass # skip


@tests.test
def test_get_ident():
    result = [None, None]
    local = {}
    def run():
        value = yield
        local[get_ident()] = value
        result_idx = yield
        result[result_idx] = local.setdefault(get_ident())
    thread_test(run, (123, 0), (456, 1))
    assert result == [123, 456]
    if greenlet:
        result = [None, None]
        coro_test(run, (123, 0), (456, 1))
        assert result == [123, 456]

{'fraction_non_alphanumeric': 0.1322431567721294, 'fraction_numerical': 0.03519374333451831, 'mean_word_length': 2.0553745928338762, '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': '631554', 'n_tokens_mistral': 980, 'n_tokens_neox': 908, 'n_words': 230}