|
| 1 | +# |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 3 | +# not use this file except in compliance with the License. You may obtain |
| 4 | +# a copy of the License at |
| 5 | +# |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +# |
| 8 | +# Unless required by applicable law or agreed to in writing, software |
| 9 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 10 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 11 | +# License for the specific language governing permissions and limitations |
| 12 | +# under the License. |
| 13 | + |
| 14 | + |
| 15 | +from testcontainers.core.container import DockerContainer |
| 16 | +from testcontainers.core.waiting_utils import wait_container_is_ready, wait_for_logs |
| 17 | + |
| 18 | + |
| 19 | +class NatsContainer(DockerContainer): |
| 20 | + """ |
| 21 | + Nats container. |
| 22 | +
|
| 23 | + Example: |
| 24 | +
|
| 25 | + .. doctest:: |
| 26 | +
|
| 27 | + >>> import asyncio |
| 28 | + >>> from nats import connect as nats_connect |
| 29 | + >>> from testcontainers.nats import NatsContainer |
| 30 | +
|
| 31 | + >>> async def test_doctest_usage(): |
| 32 | + ... with NatsContainer() as nats_container: |
| 33 | + ... client = await nats_connect(nats_container.nats_uri()) |
| 34 | + ... sub_tc = await client.subscribe("tc") |
| 35 | + ... await client.publish("tc", b"Test-Containers") |
| 36 | + ... next_message = await sub_tc.next_msg(timeout=5.0) |
| 37 | + ... await client.close() |
| 38 | + ... return next_message.data |
| 39 | + >>> asyncio.run(test_doctest_usage()) |
| 40 | + b'Test-Containers' |
| 41 | + """ |
| 42 | + |
| 43 | + def __init__( |
| 44 | + self, |
| 45 | + image: str = "nats:latest", |
| 46 | + client_port: int = 4222, |
| 47 | + management_port: int = 8222, |
| 48 | + expected_ready_log: str = "Server is ready", |
| 49 | + ready_timeout_secs: int = 120, |
| 50 | + **kwargs, |
| 51 | + ) -> None: |
| 52 | + super().__init__(image, **kwargs) |
| 53 | + self.client_port = client_port |
| 54 | + self.management_port = management_port |
| 55 | + self._expected_ready_log = expected_ready_log |
| 56 | + self._ready_timeout_secs = max(ready_timeout_secs, 0) |
| 57 | + self.with_exposed_ports(self.client_port, self.management_port) |
| 58 | + |
| 59 | + @wait_container_is_ready() |
| 60 | + def _healthcheck(self) -> None: |
| 61 | + wait_for_logs(self, self._expected_ready_log, timeout=self._ready_timeout_secs) |
| 62 | + |
| 63 | + def nats_uri(self) -> str: |
| 64 | + return f"nats://{self.get_container_host_ip()}:{self.get_exposed_port(self.client_port)}" |
| 65 | + |
| 66 | + def nats_host_and_port(self) -> tuple[str, int]: |
| 67 | + return self.get_container_host_ip(), self.get_exposed_port(self.client_port) |
| 68 | + |
| 69 | + def nats_management_uri(self) -> str: |
| 70 | + return f"nats://{self.get_container_host_ip()}:{self.get_exposed_port(self.management_port)}" |
| 71 | + |
| 72 | + def start(self) -> "NatsContainer": |
| 73 | + super().start() |
| 74 | + self._healthcheck() |
| 75 | + return self |
0 commit comments