Skip to content

Commit 4d119e0

Browse files
valkey
1 parent 51be24f commit 4d119e0

File tree

3 files changed

+60
-44
lines changed

3 files changed

+60
-44
lines changed

docs/modules/redis.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ These examples use the following libraries:
1616

1717
Choose an image from the [container registry](https://hub.docker.com/_/redis) and substitute `IMAGE`.
1818

19-
### Set and get a value
19+
### Set/get a value
2020

2121
<!--codeinclude-->
2222
[](../../packages/modules/redis/src/redis-container.test.ts) inside_block:redisStartContainer

docs/modules/valkey.md

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# Valkey Module
2-
3-
[Valkey](https://valkey.io/) is a distributed, in-memory, key-value store.
1+
# Valkey
42

53
## Install
64

@@ -10,32 +8,40 @@ npm install @testcontainers/valkey --save-dev
108

119
## Examples
1210

13-
<!--codeinclude-->
11+
These examples use the following libraries:
1412

15-
[Start container:](../../packages/modules/valkey/src/valkey-container.test.ts) inside_block:startContainer
13+
- [redis](https://www.npmjs.com/package/redis)
1614

17-
<!--/codeinclude-->
15+
npm install redis
1816

19-
<!--codeinclude-->
17+
Choose an image from the [container registry](https://hub.docker.com/r/valkey/valkey) and substitute `IMAGE`.
2018

21-
[Connect valkey client to container:](../../packages/modules/valkey/src/valkey-container.test.ts) inside_block:simpleConnect
22-
23-
<!--/codeinclude-->
19+
### Set/get a value
2420

2521
<!--codeinclude-->
26-
27-
[Start container with password authentication:](../../packages/modules/valkey/src/valkey-container.test.ts) inside_block:startWithCredentials
28-
22+
[](../../packages/modules/valkey/src/valkey-container.test.ts) inside_block:valkeyStartContainer
2923
<!--/codeinclude-->
3024

25+
### With password
26+
3127
<!--codeinclude-->
28+
[](../../packages/modules/valkey/src/valkey-container.test.ts) inside_block:valkeyWithCredentials
29+
<!--/codeinclude-->
3230

33-
[Define volume for persistent/predefined data:](../../packages/modules/valkey/src/valkey-container.test.ts) inside_block:persistentData
31+
### With persistent data
3432

33+
<!--codeinclude-->
34+
[](../../packages/modules/valkey/src/valkey-container.test.ts) inside_block:valkeyWithPersistentData
3535
<!--/codeinclude-->
3636

37+
### With predefined data
38+
3739
<!--codeinclude-->
40+
[](../../packages/modules/valkey/src/valkey-container.test.ts) inside_block:valkeyWithPredefinedData
41+
<!--/codeinclude-->
3842

39-
[Execute a command inside the container:](../../packages/modules/valkey/src/valkey-container.test.ts) inside_block:executeCommand
43+
### Execute a command inside the container
4044

45+
<!--codeinclude-->
46+
[](../../packages/modules/valkey/src/valkey-container.test.ts) inside_block:valkeyExecuteCommand
4147
<!--/codeinclude-->

packages/modules/valkey/src/valkey-container.test.ts

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,96 +3,106 @@ import os from "os";
33
import path from "path";
44
import { createClient } from "redis";
55
import { getImage } from "../../../testcontainers/src/utils/test-helper";
6-
import { StartedValkeyContainer, ValkeyContainer } from "./valkey-container";
6+
import { ValkeyContainer } from "./valkey-container";
77

88
const IMAGE = getImage(__dirname);
99

1010
describe("ValkeyContainer", { timeout: 240_000 }, () => {
1111
it("should connect and execute set-get", async () => {
12+
// valkeyStartContainer {
1213
await using container = await new ValkeyContainer(IMAGE).start();
1314

14-
const client = await connectTo(container);
15+
const client = createClient({ url: container.getConnectionUrl() });
16+
await client.connect();
1517

1618
await client.set("key", "val");
1719
expect(await client.get("key")).toBe("val");
1820

19-
await client.disconnect();
21+
client.destroy();
22+
// }
2023
});
2124

2225
it("should connect with password and execute set-get", async () => {
2326
await using container = await new ValkeyContainer(IMAGE).withPassword("test").start();
2427

25-
const client = await connectTo(container);
28+
const client = createClient({ url: container.getConnectionUrl() });
29+
await client.connect();
2630

2731
await client.set("key", "val");
2832
expect(await client.get("key")).toBe("val");
2933

30-
await client.disconnect();
34+
client.destroy();
3135
});
3236

3337
it("should reconnect with volume and persistence data", async () => {
38+
// valkeyWithPersistentData {
3439
const sourcePath = fs.mkdtempSync(path.join(os.tmpdir(), "valkey-"));
40+
3541
await using container = await new ValkeyContainer(IMAGE).withPassword("test").withPersistence(sourcePath).start();
36-
let client = await connectTo(container);
42+
43+
let client = createClient({ url: container.getConnectionUrl() });
44+
await client.connect();
3745

3846
await client.set("key", "val");
39-
await client.disconnect();
47+
client.destroy();
48+
4049
await container.restart();
41-
client = await connectTo(container);
50+
client = createClient({ url: container.getConnectionUrl() });
51+
await client.connect();
52+
4253
expect(await client.get("key")).toBe("val");
4354

44-
await client.disconnect();
45-
try {
46-
fs.rmSync(sourcePath, { force: true, recursive: true });
47-
} catch (e) {
48-
//Ignore clean up, when have no access on fs.
49-
console.log(e);
50-
}
55+
client.destroy();
56+
fs.rmSync(sourcePath, { force: true, recursive: true });
57+
// }
5158
});
5259

5360
it("should load initial data and can read it", async () => {
61+
// valkeyWithPredefinedData {
5462
await using container = await new ValkeyContainer(IMAGE)
5563
.withPassword("test")
5664
.withInitialData(path.join(__dirname, "initData.valkey"))
5765
.start();
58-
const client = await connectTo(container);
66+
67+
const client = createClient({ url: container.getConnectionUrl() });
68+
await client.connect();
69+
5970
const user = {
6071
first_name: "David",
6172
last_name: "Bloom",
6273
dob: "03-MAR-1981",
6374
};
6475
expect(await client.get("user:002")).toBe(JSON.stringify(user));
6576

66-
await client.disconnect();
77+
client.destroy();
78+
// }
6779
});
6880

6981
it("should start with credentials and login", async () => {
82+
// valkeyWithCredentials {
7083
const password = "testPassword";
7184

7285
await using container = await new ValkeyContainer(IMAGE).withPassword(password).start();
86+
7387
expect(container.getConnectionUrl()).toEqual(`redis://:${password}@${container.getHost()}:${container.getPort()}`);
88+
// }
7489

75-
const client = await connectTo(container);
90+
const client = createClient({ url: container.getConnectionUrl() });
91+
await client.connect();
7692

7793
await client.set("key", "val");
7894
expect(await client.get("key")).toBe("val");
7995

80-
await client.disconnect();
96+
client.destroy();
8197
});
8298

8399
it("should execute container cmd and return the result", async () => {
100+
// valkeyExecuteCommand {
84101
await using container = await new ValkeyContainer(IMAGE).start();
85102

86103
const queryResult = await container.executeCliCmd("info", ["clients"]);
104+
87105
expect(queryResult).toEqual(expect.stringContaining("connected_clients:1"));
106+
// }
88107
});
89-
90-
async function connectTo(container: StartedValkeyContainer) {
91-
const client = createClient({
92-
url: container.getConnectionUrl(),
93-
});
94-
await client.connect();
95-
expect(client.isOpen).toBeTruthy();
96-
return client;
97-
}
98108
});

0 commit comments

Comments
 (0)