Skip to content

Commit

Permalink
#97: Parametrise background task tests to exercise aws_xray_sdk code …
Browse files Browse the repository at this point in the history
…also.
  • Loading branch information
garyd203 committed May 24, 2023
1 parent e32e48d commit 6605bea
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).


## v1.7.0 (unreleased)

Changed:
* Test against `aws_xray_sdk` release up to v2.12.0
* Test functionality in `AsyncContext` in `aws_xray_sdk`, including fix in v2.10.0


## v1.6.1 (2023-03-06)

Changed:
* Test support for python v3.11 and upgradesome dependencies. No code changes.
* Test support for python v3.11 and upgrade some dependencies. No code changes.


## v1.6.0 (2022-05-18)
Expand Down
46 changes: 40 additions & 6 deletions tests/test_asyncio_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,33 @@
from asyncio import sleep

import pytest
from aws_xray_sdk.core.async_context import AsyncContext as CoreAsyncContext
from aws_xray_sdk.version import VERSION as AWS_XRAY_SDK_VERSION_STRING

from xraysink.context import AsyncContext

pytestmark = pytest.mark.asyncio

AWS_XRAY_SDK_VERSION = list(map(int, AWS_XRAY_SDK_VERSION_STRING.split(".")))

BROKEN_SDK_VERSION = pytest.mark.xfail(
AWS_XRAY_SDK_VERSION[0] == 2 and AWS_XRAY_SDK_VERSION[1] < 10,
reason="aws_xray_sdk async context propagation prior to v2.10.0 was broken",
)


@pytest.mark.parametrize(
"context_class",
[
pytest.param(AsyncContext, id="xraysink"),
pytest.param(CoreAsyncContext, marks=[BROKEN_SDK_VERSION], id="aws_xray_sdk"),
],
)
async def test_asyncio_task_subsegments_should_use_parent_task_segment_as_parent(
recorder,
recorder, context_class
):
# Setup
recorder.configure(context=AsyncContext(use_task_factory=True))
recorder.configure(context=context_class(use_task_factory=True))

async def do_task(name: str, sleep_time: float):
# Emulate a remote call by starting a subsegment and blocking
Expand Down Expand Up @@ -65,9 +81,18 @@ async def broken_task(name: str):
assert getattr(subsegments["errored-task"], "fault", False)


async def test_asyncio_task_should_start_segment_when_none_present(recorder):
@pytest.mark.parametrize(
"context_class",
[
pytest.param(AsyncContext, id="xraysink"),
pytest.param(CoreAsyncContext, marks=[BROKEN_SDK_VERSION], id="aws_xray_sdk"),
],
)
async def test_asyncio_task_should_start_segment_when_none_present(
recorder, context_class
):
# Setup
recorder.configure(context=AsyncContext(use_task_factory=True))
recorder.configure(context=context_class(use_task_factory=True))

async def do_task(a, b):
async with recorder.in_segment_async():
Expand All @@ -86,9 +111,18 @@ async def do_task(a, b):
assert getattr(segment, "fault", False) is False


async def test_asyncio_task_should_start_segment_when_previous_segment_closed(recorder):
@pytest.mark.parametrize(
"context_class",
[
pytest.param(AsyncContext, id="xraysink"),
pytest.param(CoreAsyncContext, marks=[BROKEN_SDK_VERSION], id="aws_xray_sdk"),
],
)
async def test_asyncio_task_should_start_segment_when_previous_segment_closed(
recorder, context_class
):
# Setup
recorder.configure(context=AsyncContext(use_task_factory=True))
recorder.configure(context=context_class(use_task_factory=True))

async def do_task(a, b):
async with recorder.in_segment_async():
Expand Down

0 comments on commit 6605bea

Please sign in to comment.