Skip to content

Commit 6e046d5

Browse files
committed
address comments
1 parent bf1d904 commit 6e046d5

File tree

2 files changed

+32
-62
lines changed

2 files changed

+32
-62
lines changed

modules/valkey/example_basic.py

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import socket
1+
from glide import GlideClient, NodeAddress
22

33
from testcontainers.valkey import ValkeyContainer
44

@@ -13,24 +13,22 @@ def basic_example():
1313
print(f"Valkey connection URL: {connection_url}")
1414
print(f"Host: {host}, Port: {port}")
1515

16-
# Connect using raw socket and RESP protocol
17-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
18-
s.connect((host, port))
16+
# Connect using Glide client
17+
client = GlideClient([NodeAddress(host, port)])
1918

20-
# PING command
21-
s.sendall(b"*1\r\n$4\r\nPING\r\n")
22-
response = s.recv(1024)
23-
print(f"PING response: {response.decode()}")
19+
# PING command
20+
pong = client.ping()
21+
print(f"PING response: {pong}")
2422

25-
# SET command
26-
s.sendall(b"*3\r\n$3\r\nSET\r\n$3\r\nkey\r\n$5\r\nvalue\r\n")
27-
response = s.recv(1024)
28-
print(f"SET response: {response.decode()}")
23+
# SET command
24+
client.set("key", "value")
25+
print("SET response: OK")
2926

30-
# GET command
31-
s.sendall(b"*2\r\n$3\r\nGET\r\n$3\r\nkey\r\n")
32-
response = s.recv(1024)
33-
print(f"GET response: {response.decode()}")
27+
# GET command
28+
value = client.get("key")
29+
print(f"GET response: {value}")
30+
31+
client.close()
3432

3533

3634
def password_example():
@@ -41,18 +39,14 @@ def password_example():
4139

4240
print(f"\nValkey with password connection URL: {connection_url}")
4341

44-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
45-
s.connect((host, port))
42+
# Connect using Glide client with password
43+
client = GlideClient([NodeAddress(host, port)], password="mypassword")
4644

47-
# AUTH command
48-
s.sendall(b"*2\r\n$4\r\nAUTH\r\n$10\r\nmypassword\r\n")
49-
response = s.recv(1024)
50-
print(f"AUTH response: {response.decode()}")
45+
# PING after auth
46+
pong = client.ping()
47+
print(f"PING response: {pong}")
5148

52-
# PING after auth
53-
s.sendall(b"*1\r\n$4\r\nPING\r\n")
54-
response = s.recv(1024)
55-
print(f"PING response: {response.decode()}")
49+
client.close()
5650

5751

5852
def version_example():
@@ -70,11 +64,11 @@ def bundle_example():
7064
host = valkey_container.get_host()
7165
port = valkey_container.get_exposed_port()
7266

73-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
74-
s.connect((host, port))
75-
s.sendall(b"*1\r\n$4\r\nPING\r\n")
76-
response = s.recv(1024)
77-
print(f"PING response: {response.decode()}")
67+
# Connect using Glide client
68+
client = GlideClient([NodeAddress(host, port)])
69+
pong = client.ping()
70+
print(f"PING response: {pong}")
71+
client.close()
7872

7973

8074
if __name__ == "__main__":

modules/valkey/testcontainers/valkey/__init__.py

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,16 @@
1111
# License for the specific language governing permissions and limitations
1212
# under the License.
1313

14-
import socket
1514
from typing import Optional
1615

1716
from testcontainers.core.container import DockerContainer
18-
from testcontainers.core.waiting_utils import wait_container_is_ready
19-
20-
21-
class ValkeyNotReady(Exception):
22-
pass
17+
from testcontainers.core.wait_strategies import ExecWaitStrategy
2318

2419

2520
class ValkeyContainer(DockerContainer):
2621
"""
2722
Valkey container.
2823
29-
Example:
30-
31-
.. doctest::
32-
33-
>>> from testcontainers.valkey import ValkeyContainer
34-
35-
>>> with ValkeyContainer() as valkey_container:
36-
... connection_url = valkey_container.get_connection_url()
3724
"""
3825

3926
def __init__(self, image: str = "valkey/valkey:latest", port: int = 6379, **kwargs) -> None:
@@ -53,7 +40,7 @@ def with_password(self, password: str) -> "ValkeyContainer":
5340
self: Container instance for method chaining.
5441
"""
5542
self.password = password
56-
self.with_command(f"valkey-server --requirepass {password}")
43+
self.with_command(["valkey-server", "--requirepass", password])
5744
return self
5845

5946
def with_image_tag(self, tag: str) -> "ValkeyContainer":
@@ -111,28 +98,17 @@ def get_exposed_port(self) -> int:
11198
"""
11299
return int(super().get_exposed_port(self.port))
113100

114-
@wait_container_is_ready(ValkeyNotReady)
115-
def _connect(self) -> None:
116-
"""Wait for Valkey to be ready by sending PING command."""
117-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
118-
s.connect((self.get_host(), self.get_exposed_port()))
119-
if self.password:
120-
s.sendall(f"*2\r\n$4\r\nAUTH\r\n${len(self.password)}\r\n{self.password}\r\n".encode())
121-
auth_response = s.recv(1024)
122-
if b"+OK" not in auth_response:
123-
raise ValkeyNotReady("Authentication failed")
124-
s.sendall(b"*1\r\n$4\r\nPING\r\n")
125-
response = s.recv(1024)
126-
if b"+PONG" not in response:
127-
raise ValkeyNotReady("Valkey not ready yet")
128-
129101
def start(self) -> "ValkeyContainer":
130102
"""
131103
Start the container and wait for it to be ready.
132104
133105
Returns:
134106
self: Started container instance.
135107
"""
108+
if self.password:
109+
self.waiting_for(ExecWaitStrategy(["valkey-cli", "-a", self.password, "ping"]))
110+
else:
111+
self.waiting_for(ExecWaitStrategy(["valkey-cli", "ping"]))
112+
136113
super().start()
137-
self._connect()
138114
return self

0 commit comments

Comments
 (0)