diff --git a/redis.ts b/redis.ts index d47fbcdb..c9eba83e 100644 --- a/redis.ts +++ b/redis.ts @@ -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); }, diff --git a/tests/commands/connection.ts b/tests/commands/connection.ts index 91536832..32b6840f 100644 --- a/tests/commands/connection.ts +++ b/tests/commands/connection.ts @@ -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, @@ -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()); @@ -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(); + }); + }); } diff --git a/tests/commands/general.ts b/tests/commands/general.ts index 92265f2b..68980632 100644 --- a/tests/commands/general.ts +++ b/tests/commands/general.ts @@ -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 { @@ -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(); - } - }); - }); }