|
| 1 | +from testcontainers.nats import NatsContainer |
| 2 | +from uuid import uuid4 |
| 3 | +import pytest |
| 4 | + |
| 5 | + |
| 6 | +""" |
| 7 | +If you are developing this and you want to test more advanced scenarios using a client |
| 8 | +Activate your poetry shell. |
| 9 | +pip install nats-py |
| 10 | +This will get nats-py into your environment but keep it out of the project |
| 11 | +
|
| 12 | +
|
| 13 | +""" |
| 14 | + |
| 15 | + |
| 16 | +NO_NATS_CLIENT = True |
| 17 | +try: |
| 18 | + from nats import connect as nats_connect |
| 19 | + from nats.aio.client import Client as NATSClient |
| 20 | + |
| 21 | + NO_NATS_CLIENT = False |
| 22 | +except ImportError: |
| 23 | + pass |
| 24 | + |
| 25 | + |
| 26 | +async def get_client(container: NatsContainer) -> "NATSClient": |
| 27 | + """ |
| 28 | + Get a nats client. |
| 29 | +
|
| 30 | + Returns: |
| 31 | + client: Nats client to connect to the container. |
| 32 | + """ |
| 33 | + conn_string = container.nats_uri() |
| 34 | + client = await nats_connect(conn_string) |
| 35 | + return client |
| 36 | + |
| 37 | + |
| 38 | +def test_basic_container_ops(): |
| 39 | + with NatsContainer() as container: |
| 40 | + # Not sure how to get type information without doing this |
| 41 | + container: NatsContainer = container |
| 42 | + h, p = container.nats_host_and_port() |
| 43 | + assert h == "localhost" |
| 44 | + uri = container.nats_uri() |
| 45 | + management_uri = container.nats_management_uri() |
| 46 | + |
| 47 | + assert uri != management_uri |
| 48 | + |
| 49 | + |
| 50 | +pytest.mark.usefixtures("anyio_backend") |
| 51 | + |
| 52 | + |
| 53 | +@pytest.mark.skipif(NO_NATS_CLIENT, reason="No NATS Client Available") |
| 54 | +@pytest.mark.parametrize("anyio_backend", ["asyncio"]) |
| 55 | +async def test_pubsub(anyio_backend): |
| 56 | + with NatsContainer() as container: |
| 57 | + nc: NATSClient = await get_client(container) |
| 58 | + |
| 59 | + topic = str(uuid4()) |
| 60 | + |
| 61 | + sub = await nc.subscribe(topic) |
| 62 | + sent_message = b"Test-Containers" |
| 63 | + await nc.publish(topic, b"Test-Containers") |
| 64 | + received_msg = await sub.next_msg() |
| 65 | + print("Received:", received_msg) |
| 66 | + assert sent_message == received_msg.data |
| 67 | + await nc.flush() |
| 68 | + await nc.close() |
| 69 | + |
| 70 | + |
| 71 | +pytest.mark.usefixtures("anyio_backend") |
| 72 | + |
| 73 | + |
| 74 | +@pytest.mark.parametrize("anyio_backend", ["asyncio"]) |
| 75 | +@pytest.mark.skipif(NO_NATS_CLIENT, reason="No NATS Client Available") |
| 76 | +async def test_more_complex_example(anyio_backend): |
| 77 | + with NatsContainer() as container: |
| 78 | + nc: NATSClient = await get_client(container) |
| 79 | + |
| 80 | + await nc.publish("greet.joe", b"hello") |
| 81 | + |
| 82 | + sub = await nc.subscribe("greet.*") |
| 83 | + |
| 84 | + try: |
| 85 | + await sub.next_msg(timeout=0.1) |
| 86 | + except TimeoutError: |
| 87 | + pass |
| 88 | + |
| 89 | + await nc.publish("greet.joe", b"hello.joe") |
| 90 | + await nc.publish("greet.pam", b"hello.pam") |
| 91 | + |
| 92 | + first = await sub.next_msg(timeout=0.1) |
| 93 | + assert b"hello.joe" == first.data |
| 94 | + |
| 95 | + second = await sub.next_msg(timeout=0.1) |
| 96 | + assert b"hello.pam" == second.data |
| 97 | + |
| 98 | + await nc.publish("greet.bob", b"hello") |
| 99 | + |
| 100 | + await sub.unsubscribe() |
| 101 | + await nc.drain() |
0 commit comments