Skip to content

Commit

Permalink
tests: use asyncio pytest plugin for asyncio tests (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
P403n1x87 authored Sep 10, 2023
1 parent 8ab1361 commit fe90184
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 39 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ austinp-resolve = "austin.tools.resolve:main"
template = "tests"
dependencies = [
"pytest>=5.4.2",
"pytest-asyncio",
"pytest-cov>=2.8.1",
]
[tool.hatch.envs.tests.scripts]
Expand Down
75 changes: 36 additions & 39 deletions test/test_aio.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import asyncio
import sys

import pytest
from pytest import raises

from austin import AustinError
Expand Down Expand Up @@ -70,21 +71,20 @@ class InvalidBinaryAsyncAustin(AsyncAustin):
BINARY = "_austin"


def test_async_time():
@pytest.mark.asyncio
async def test_async_time():
austin = TestAsyncAustin()

asyncio.get_event_loop().run_until_complete(
austin.start(
[
"-t",
"10",
"-Ci",
"100",
"python",
"-c",
"from time import sleep; sleep(2)",
]
)
await austin.start(
[
"-t",
"10",
"-Ci",
"100",
"python",
"-c",
"from time import sleep; sleep(2)",
]
)

austin.assert_callbacks_called()
Expand All @@ -93,30 +93,31 @@ def test_async_time():
assert austin.python_version is not None


def test_async_memory():
@pytest.mark.asyncio
async def test_async_memory():
austin = TestAsyncAustin()

def sample_callback(data):
austin._sample_received = True

austin._sample_callback = sample_callback
asyncio.get_event_loop().run_until_complete(
austin.start(
[
"-t",
"10",
"-mCi",
"100",
"python",
"-c",
"[i for i in range(10000000)]",
]
)
await austin.start(
[
"-t",
"10",
"-mCi",
"100",
"python",
"-c",
"[i for i in range(10000000)]",
]
)

austin.assert_callbacks_called()


def test_async_terminate():
@pytest.mark.asyncio
async def test_async_terminate():
austin = TestAsyncAustin()

def sample_callback(*args):
Expand All @@ -130,30 +131,26 @@ def terminate_callback(*args):
austin._terminate_callback = terminate_callback

try:
asyncio.get_event_loop().run_until_complete(
asyncio.wait_for(austin.start(["-t", "10", "-Ci", "10ms", "python"]), 30)
)
await asyncio.wait_for(austin.start(["-t", "10", "-Ci", "10ms", "python"]), 30)
except AustinError:
austin.assert_callbacks_called()


def test_async_invalid_binary():
@pytest.mark.asyncio
async def test_async_invalid_binary():
with raises(AustinError):
asyncio.get_event_loop().run_until_complete(
InvalidBinaryAsyncAustin(sample_callback=lambda x: None).start(["python"])
)
await InvalidBinaryAsyncAustin(sample_callback=lambda x: None).start(["python"])


def test_async_no_sample_callback():
with raises(AustinError):
InvalidBinaryAsyncAustin()


def test_async_bad_options():
@pytest.mark.asyncio
async def test_async_bad_options():
austin = TestAsyncAustin(terminate_callback=lambda *args: None)
with raises(AustinError):
asyncio.get_event_loop().run_until_complete(
austin.start(
["-I", "1000", "python", "-c", "for i in range(1000000): print(i)"]
)
await austin.start(
["-I", "1000", "python", "-c", "for i in range(1000000): print(i)"]
)

0 comments on commit fe90184

Please sign in to comment.