Svoboda | Graniru | BBC Russia | Golosameriki | Facebook
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I can't use pyrogram as pytest.fixture #1434

Open
3 tasks done
Nekr0bz opened this issue Jun 18, 2024 · 0 comments
Open
3 tasks done

I can't use pyrogram as pytest.fixture #1434

Nekr0bz opened this issue Jun 18, 2024 · 0 comments

Comments

@Nekr0bz
Copy link

Nekr0bz commented Jun 18, 2024

Checklist

  • I am sure the error is coming from Pyrogram's code and not elsewhere
  • I have searched in the issue tracker for similar bug reports, including closed ones
  • I ran pip3 install -U https://github.com/pyrogram/pyrogram/archive/master.zip and reproduced the issue using the latest development version

Description

I'm trying to use pyrogram as pytest.fixture and getting the error "RuntimeError: Task ... got Future ... attached to a different loop"

But when I use pyrogram without fixture, everything works correctly.
Eg:

async def test_one():
    client = Client(
        name="test",
        api_id=settings.TG.API_ID,
        api_hash=settings.TG.API_HASH,
        phone_number=settings.TG.PHONE_NUMBER,
        workdir=settings.TG.SESSIONS_DIR,
    )

    async with client:
        await client.send_message("me", "test")

Steps to reproduce

Python 3.12.1
Pyrogram==2.0.106

Create a fixture that returns the pyrogram's client.
Use this fixture in test.

Code example

# conftest.py

@pytest.fixture(scope="session")
async def tg_client() -> AsyncGenerator[Client, None]:
    client = Client(
        name="test",
        api_id=settings.TG.API_ID,
        api_hash=settings.TG.API_HASH,
        phone_number=settings.TG.PHONE_NUMBER,
        workdir=settings.TG.SESSIONS_DIR,
    )

    async with client as tg_client:
        yield tg_client

# test_one.py

async def test_one(tg_client: Client):
    await tg_client.send_message("me", "test")

Logs

[2024-06-18 21:39:04,901 | pyrogram.connection.connection | INFO | connection.py:47 - connect]: Connecting...
[2024-06-18 21:39:04,933 | pyrogram.connection.connection | INFO | connection.py:54 - connect]: Connected! Production DC2 - IPv4
[2024-06-18 21:39:04,934 | pyrogram.session.session | INFO | session.py:294 - recv_worker]: NetworkTask started
[2024-06-18 21:39:05,121 | pyrogram.session.session | INFO | session.py:139 - start]: Session initialized: Layer 158
[2024-06-18 21:39:05,121 | pyrogram.session.session | INFO | session.py:140 - start]: Device: CPython 3.12.1 - Pyrogram 2.0.106
[2024-06-18 21:39:05,121 | pyrogram.session.session | INFO | session.py:141 - start]: System: Linux 6.5.0-35-generic (en)
[2024-06-18 21:39:05,121 | pyrogram.session.session | INFO | session.py:155 - start]: Session started
[2024-06-18 21:39:05,129 | pyrogram.session.session | INFO | session.py:272 - ping_worker]: PingTask started
[2024-06-18 21:39:05,235 | pyrogram.dispatcher | INFO | dispatcher.py:154 - start]: Started 20 HandlerTasks
FAILED[2024-06-18 21:39:05,260 | pyrogram.dispatcher | INFO | dispatcher.py:167 - stop]: Stopped 20 HandlerTasks
[2024-06-18 21:39:05,261 | pyrogram.session.session | INFO | session.py:291 - ping_worker]: PingTask stopped
[2024-06-18 21:39:05,261 | pyrogram.session.session | INFO | session.py:315 - recv_worker]: NetworkTask stopped
[2024-06-18 21:39:05,261 | pyrogram.connection.connection | INFO | connection.py:66 - close]: Disconnected
[2024-06-18 21:39:05,261 | pyrogram.session.session | INFO | session.py:180 - stop]: Session stopped


================ FAILURES ========================
________________ test_one ________________________

tg_client = <pyrogram.client.Client object at 0x7be5fccfbce0>

    async def test_one(tg_client: Client):
>       await tg_client.send_message("me", "test")

tests/e2e/test_sso.py:44: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
/usr/local/lib/python3.12/site-packages/pyrogram/methods/messages/send_message.py:126: in send_message
    r = await self.invoke(
/usr/local/lib/python3.12/site-packages/pyrogram/methods/advanced/invoke.py:79: in invoke
    r = await self.session.invoke(
/usr/local/lib/python3.12/site-packages/pyrogram/session/session.py:389: in invoke
    return await self.send(query, timeout=timeout)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <pyrogram.session.session.Session object at 0x7be5fcd4e600>
data = pyrogram.raw.functions.messages.SendMessage(peer=pyrogram.raw.types.InputPeerSelf(), message='test', random_id=7381961059881451556), wait_response = True
timeout = 15

    async def send(self, data: TLObject, wait_response: bool = True, timeout: float = WAIT_TIMEOUT):
        message = self.msg_factory(data)
        msg_id = message.msg_id
    
        if wait_response:
            self.results[msg_id] = Result()
    
        log.debug("Sent: %s", message)
    
>       payload = await self.loop.run_in_executor(
            pyrogram.crypto_executor,
            mtproto.pack,
            message,
            self.salt,
            self.session_id,
            self.auth_key,
            self.auth_key_id
        )
E       RuntimeError: Task <Task pending name='Task-30' coro=<test_one() running at /habits_bot/tests/e2e/test_sso.py:44> cb=[_run_until_complete_cb() at /usr/local/lib/python3.12/asyncio/base_events.py:180]> got Future <Future pending cb=[_chain_future.<locals>._call_check_cancel() at /usr/local/lib/python3.12/asyncio/futures.py:387]> attached to a different loop

/usr/local/lib/python3.12/site-packages/pyrogram/session/session.py:326: RuntimeError
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant