robotcodedev / robotcode

RobotFramework support for Visual Studio Code
https://robotcode.io
Apache License 2.0
184 stars 15 forks source link

[BUG] Running tests with async keywords does not work #242

Closed jaltendorfer closed 6 months ago

jaltendorfer commented 8 months ago

Describe the bug In RF 6.1 async keyword support was added, but when running tests with the robotcode extention in VSCode, it does not work. The error is: "RuntimeWarning: coroutine 'library.keyword_name' was never awaited"

With the robotframework language server plugin it works normally.

To Reproduce Steps to reproduce the behavior:

  1. Create a robotframework library in python where a keyword is async.
  2. Run a test with the keyword using robotcode in VSCode.
  3. Keyword will not be executed, bacause the coroutine is not awaited.
  4. See error: "RuntimeWarning: coroutine 'library.keyword_name' was never awaited".

Expected behavior Async keywords should be executed normally (like robotframework does it when using the robot command).

Desktop (please complete the following information):

d-biehl commented 8 months ago

Cannot reproduce this.

Can you perhaps add a short example code?

jaltendorfer commented 8 months ago

Here is an example.

Test.py:

import asyncio
from robot.api.deco import keyword, library

@library(scope="SUITE", version="1.0")
class Test:

    @keyword("Async Sleep")
    async def async_sleep(self, sleep_time: int):
        await asyncio.sleep(sleep_time)

test.robot:

*** Settings ***
Library    Test.py

*** Test Cases ***
Test
    Log    start
    Async Sleep    5
    Log    end

Message in terminal output:

RuntimeWarning: coroutine 'Test.async_sleep' was never awaited step.run(self._context, self._run, self._templated) RuntimeWarning: Enable tracemalloc to get the object allocation traceback

pkomarov commented 7 months ago

Hello, Similar problem for me. Trying to run keywords, which are using pyshark library inside, but got an error "RuntimeError: This event loop is already running"

@keyword("Acquire packet from remote interface")
def sniff_for_packet(hostname, remote_if: str, packet_count: int, bpf_filter: str = None):
    capture = RemoteCapture(hostname, remote_if, bpf_filter=bpf_filter, debug=True)
    capture.sniff(packet_count=packet_count)
    logger.info(f"Capture {capture}")
Remote Capture test
    Acquire packet from remote interface  ${rpcapd_servise}  eno1  5

Error message in a terminal output:

sys:1: RuntimeWarning: coroutine 'Capture.packets_from_tshark' was never awaited RuntimeWarning: Enable tracemalloc to get the object allocation traceback

In a robot log: image

d-biehl commented 7 months ago

ok, after some investigation I find out that the implementation of calling async keywords in RF does not work if there is already a running event loop. see here I think that should work too or at least a correct error message should appear.

on the other hand, I'm about to rework some async code, because async has some performance and syncronisation problems.

It's about time I do that for the debugger ;-) will have a closer look at it.

d-biehl commented 7 months ago

@pkomarov I don't know the pyshark library, but your keyword implementation has no async maybe this is an error in the pyshark library. Or does this happen if you add asycn to you keyword implementation?

pkomarov commented 7 months ago

@pkomarov I don't know the pyshark library, but your keyword implementation has no async maybe this is an error in the pyshark library. Or does this happen if you add asycn to you keyword implementation?

@d-biehl If I run from console, test works fine, from "Robot Framework Language Server" plugin also works fine. If I add async to a keyword - nothing is happend at all: image

Looking forward you fix this issue, would like to migrate from Robot Framework Language Server