Skip to content

Commit

Permalink
fix: connect() to work with the lazy client (#360)
Browse files Browse the repository at this point in the history
  • Loading branch information
uki00a authored Jan 5, 2023
1 parent 44bf9f3 commit da3afb9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 25 deletions.
4 changes: 3 additions & 1 deletion redis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2440,7 +2440,9 @@ function createLazyExecutor(connection: Connection): CommandExecutor {
async exec(command, ...args) {
if (!executor) {
executor = new MuxExecutor(connection);
await connection.connect();
if (!connection.isConnected) {
await connection.connect();
}
}
return executor.exec(command, ...args);
},
Expand Down
61 changes: 57 additions & 4 deletions tests/commands/connection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { connect } from "../../mod.ts";
import { assertEquals } from "../../vendor/https/deno.land/std/testing/asserts.ts";
import { connect, createLazyClient } from "../../mod.ts";
import {
assert,
assertEquals,
assertNotEquals,
} from "../../vendor/https/deno.land/std/testing/asserts.ts";
import {
afterAll,
beforeAll,
Expand All @@ -14,9 +18,12 @@ export function connectionTests(
getServer: () => TestServer,
): void {
let client!: Redis;
const getOpts = () => ({
hostname: "127.0.0.1",
port: getServer().port,
});
beforeAll(async () => {
const { port } = getServer();
client = await newClient({ hostname: "127.0.0.1", port });
client = await newClient(getOpts());
});

afterAll(() => client.close());
Expand Down Expand Up @@ -60,4 +67,50 @@ export function connectionTests(
assertEquals(await client.swapdb(0, 1), "OK");
});
});

describe("createLazyClient", () => {
it("returns the lazily connected client", async () => {
const opts = getOpts();
const resources = Deno.resources();
const client = createLazyClient(opts);
assert(!client.isConnected);
assertEquals(resources, Deno.resources());
try {
await client.get("foo");
assert(client.isConnected);
assertNotEquals(resources, Deno.resources());
} finally {
client.close();
}
});
});

describe("connect()", () => {
it("connects to the server", async () => {
const client = await newClient(getOpts());
assert(client.isConnected);

client.close();
assert(!client.isConnected);

await client.connect();
assert(client.isConnected);

assertEquals(await client.ping(), "PONG");

client.close();
});

it("works with a lazy client", async () => {
const client = createLazyClient(getOpts());
assert(!client.isConnected);

await client.connect();
assert(client.isConnected);

assertEquals(await client.ping(), "PONG");

client.close();
});
});
}
21 changes: 1 addition & 20 deletions tests/commands/general.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { createLazyClient, ErrorReplyError } from "../../mod.ts";
import { ErrorReplyError } from "../../mod.ts";
import type { Redis } from "../../mod.ts";
import {
assert,
assertEquals,
assertNotEquals,
assertRejects,
} from "../../vendor/https/deno.land/std/testing/asserts.ts";
import {
Expand Down Expand Up @@ -227,21 +225,4 @@ export function generalTests(
await assertRejects(() => tempClient.ping());
});
});

describe("createLazyClient", () => {
it("returns the lazily connected client", async () => {
const opts = getOpts();
const resources = Deno.resources();
const client = createLazyClient(opts);
assert(!client.isConnected);
assertEquals(resources, Deno.resources());
try {
await client.get("foo");
assert(client.isConnected);
assertNotEquals(resources, Deno.resources());
} finally {
client.close();
}
});
});
}

0 comments on commit da3afb9

Please sign in to comment.