getlogbook / logbook

A cool logging replacement for Python.
http://logbook.readthedocs.org
Other
1.48k stars 164 forks source link

test_asyncio_context_management[True] fails w/ py3.7+ if gevent is installed #310

Open mgorny opened 4 years ago

mgorny commented 4 years ago
==================================================================== FAILURES =====================================================================
______________________________________________________ test_asyncio_context_management[True] ______________________________________________________

logger = <logbook.base.Logger object at 0x7f6fd2ce90d0>

    @pytest.mark.skipif(not has_contextvars, reason="Contexvars not available")
    def test_asyncio_context_management(logger):
        h1 = logbook.TestHandler()
        h2 = logbook.TestHandler()

        async def task(handler, msg):
            for _ in range(ITERATIONS):
                with handler.contextbound():
                    logger.info(msg)

                await asyncio.sleep(0)  # allow for context switch

        asyncio.get_event_loop().run_until_complete(asyncio.gather(task(h1, 'task1'), task(h2, 'task2')))

>       assert len(h1.records) == ITERATIONS
E       assert 200 == 100
E        +  where 200 = len([<logbook.base.LogRecord object at 0x7f6fd2ce2bd0>, <logbook.base.LogRecord object at 0x7f6fd2ce2e90>, <logbook.base.L...6fd2ce2cd0>, <logbook.base.LogRecord object at 0x7f6fd2ce2f50>, <logbook.base.LogRecord object at 0x7f6fd2ce2d90>, ...])
E        +    where [<logbook.base.LogRecord object at 0x7f6fd2ce2bd0>, <logbook.base.LogRecord object at 0x7f6fd2ce2e90>, <logbook.base.L...6fd2ce2cd0>, <logbook.base.LogRecord object at 0x7f6fd2ce2f50>, <logbook.base.LogRecord object at 0x7f6fd2ce2d90>, ...] = <logbook.handlers.TestHandler object at 0x7f6fd2ce21d0>.records

tests/test_asyncio.py:23: AssertionError

To repro:

diff --git a/tox.ini b/tox.ini
index 7c21509..79be9f5 100644
--- a/tox.ini
+++ b/tox.ini
@@ -7,6 +7,7 @@ whitelist_externals =
     rm
 deps =
     py{27}: mock
+    gevent
     pytest
     speedups: Cython
 setenv =
mgorny commented 4 years ago

Oh, forgot to mention it happens with py3.7 and py3.8, not py3.6.

matan129 commented 3 years ago

[I was the original contributor of the asyncio feature in Logbook - seeing this issue by pure chance.]

This probably happens because gevent and the builtin contextvars module (Python 3.7+) aren't compatible. This explains why the tests succeed in Python 3.6, since the test is skipped in that version.

It's an easy fix IMHO, we just have to consider gevent (or even use gevent.contextvars) in concurrency.py.