Skip to content

Commit db2b5e5

Browse files
authored
Improve examples in testing (#63)
1 parent d2af6c0 commit db2b5e5

File tree

5 files changed

+64
-29
lines changed

5 files changed

+64
-29
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
key: check-types-${{ hashFiles('pyproject.toml') }}
2929
- run: curl -LsSf https://astral.sh/uv/install.sh | sh
3030
- uses: extractions/setup-just@v2
31-
- run: just install check-types
31+
- run: just check-types
3232

3333
lint:
3434
runs-on: ubuntu-latest
@@ -43,7 +43,7 @@ jobs:
4343
key: lint-${{ hashFiles('pyproject.toml') }}
4444
- run: curl -LsSf https://astral.sh/uv/install.sh | sh
4545
- uses: extractions/setup-just@v2
46-
- run: just install lint
46+
- run: just lint
4747

4848
test:
4949
runs-on: ubuntu-latest
@@ -64,7 +64,7 @@ jobs:
6464
key: ${{ github.job }}-${{ matrix.python-version }}-${{ hashFiles('pyproject.toml') }}
6565
- run: curl -LsSf https://astral.sh/uv/install.sh | sh
6666
- uses: extractions/setup-just@v2
67-
- run: just install test -vv
67+
- run: just test -vv
6868

6969
test-integration:
7070
runs-on: ubuntu-latest

Justfile

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,33 @@
1-
default: install lint check-types test test-integration
2-
3-
install:
4-
uv lock
5-
uv sync
1+
default: lint check-types test test-integration
62

73
lint:
8-
uv run -q --frozen ruff check .
9-
uv run -q --frozen ruff format .
4+
uv run ruff check .
5+
uv run ruff format .
106

117
check-types:
12-
uv run -q --frozen mypy .
8+
uv run mypy .
139

1410
test *args:
15-
uv run -q --frozen pytest {{args}}
11+
uv run pytest {{args}}
1612

1713
test-integration *args:
1814
#!/bin/bash
1915
trap 'echo; docker compose down --remove-orphans' EXIT
2016
docker compose run --build --rm app .venv/bin/pytest tests/integration.py --no-cov {{args}}
2117

2218
run-artemis:
19+
#!/bin/bash
20+
trap 'echo; docker compose down --remove-orphans' EXIT
2321
docker compose run --service-ports artemis
2422

2523
run-consumer:
26-
ARTEMIS_HOST=0.0.0.0 uv run -q --frozen python testing/consumer.py
24+
uv run testing/consumer.py
2725

2826
run-producer:
29-
ARTEMIS_HOST=0.0.0.0 uv run -q --frozen python testing/producer.py
27+
uv run testing/producer.py
3028

3129
publish:
3230
rm -rf dist/*
33-
uv tool run --from build python -m build --installer uv
34-
uv tool run twine check dist/*
35-
uv tool run twine upload dist/* --username __token__ --password $PYPI_TOKEN
31+
uvx --from build python -m build --installer uv
32+
uvx twine check dist/*
33+
uvx twine upload dist/* --username __token__ --password $PYPI_TOKEN

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,7 @@ Also, I want to pointed out that:
121121
- Protocol parsing is inspired by [aiostomp](https://github.com/pedrokiefer/aiostomp/blob/3449dcb53f43e5956ccc7662bb5b7d76bc6ef36b/aiostomp/protocol.py) (meaning: consumed by me and refactored from).
122122
- stompman is tested and used with [Artemis ActiveMQ](https://activemq.apache.org/components/artemis/).
123123
- Specification says that headers in CONNECT and CONNECTED frames shouldn't be escaped for backwards compatibility. stompman escapes headers in CONNECT frame (outcoming), but does not unescape headers in CONNECTED (outcoming).
124+
125+
### Examples
126+
127+
See producer and consumer examples in [testing/](./testing/).

testing/consumer.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,34 @@
11
import asyncio
2+
import os
23

34
import stompman
4-
from tests.integration import CONNECTION_PARAMETERS
55

6+
server = stompman.ConnectionParameters(
7+
host=os.environ.get("ARTEMIS_HOST", "0.0.0.0"), # noqa: S104
8+
port=61616,
9+
login="admin",
10+
passcode=":=123",
11+
)
612

7-
async def main() -> None:
8-
async def handle_message(frame: stompman.MessageFrame) -> None: # noqa: RUF029
9-
print(frame) # noqa: T201
1013

11-
async with stompman.Client(servers=[CONNECTION_PARAMETERS]) as client:
12-
await client.subscribe("DLQ", handler=handle_message, on_suppressed_exception=print)
14+
async def handle_message(message_frame: stompman.MessageFrame) -> None:
15+
message_content = message_frame.body.decode()
16+
17+
if "Hi" not in message_content:
18+
error_message = "Producer is not friendly :("
19+
raise ValueError(error_message)
20+
21+
await asyncio.sleep(0.1)
22+
print(f"received and processed friendly message: {message_content}") # noqa: T201
23+
24+
25+
def handle_suppressed_exception(exception: Exception, message_frame: stompman.MessageFrame) -> None:
26+
print(f"caught an exception, perhaps, producer is not friendly: {message_frame.body=!r} {exception=}") # noqa: T201
27+
28+
29+
async def main() -> None:
30+
async with stompman.Client(servers=[server]) as client:
31+
await client.subscribe("DLQ", handler=handle_message, on_suppressed_exception=handle_suppressed_exception)
1332

1433

1534
if __name__ == "__main__":

testing/producer.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,29 @@
11
import asyncio
2+
import os
23

34
import stompman
4-
from tests.integration import CONNECTION_PARAMETERS
5+
6+
server = stompman.ConnectionParameters(
7+
host=os.environ.get("ARTEMIS_HOST", "0.0.0.0"), # noqa: S104
8+
port=61616,
9+
login="admin",
10+
passcode=":=123",
11+
)
512

613

714
async def main() -> None:
8-
async with stompman.Client(servers=[CONNECTION_PARAMETERS]) as client, client.begin() as transaction:
9-
for _ in range(10):
10-
await transaction.send(body=b"hi there!", destination="DLQ")
11-
await asyncio.sleep(3)
12-
await transaction.send(body=b"hi there!", destination="DLQ")
15+
async with stompman.Client([server]) as client:
16+
await client.send(b"Hi!", "DLQ")
17+
print("Said hi") # noqa: T201
18+
19+
async with client.begin() as transaction:
20+
for index in range(5):
21+
await transaction.send(b"Hi from transaction! " + str(index).encode(), "DLQ")
22+
print(f"Said hi in transaction ({index})") # noqa: T201
23+
await asyncio.sleep(0.3)
24+
25+
await client.send(b"Mu-ha-ha!", "DLQ")
26+
print("Laughed evilly") # noqa: T201
1327

1428

1529
if __name__ == "__main__":

0 commit comments

Comments
 (0)