From 43b93282ae90e850568d73fe0c1302d0a91ddedb Mon Sep 17 00:00:00 2001 From: Asher Gomez Date: Fri, 20 Dec 2024 09:00:33 +1100 Subject: [PATCH] refactor: minor tests cleanup (#190) * refactor: minor tests cleanup * fix (#191) --- test.ts | 140 ++++++++++++++++++++------------------------------------ 1 file changed, 50 insertions(+), 90 deletions(-) diff --git a/test.ts b/test.ts index ebffae2..9f69e38 100644 --- a/test.ts +++ b/test.ts @@ -18,21 +18,19 @@ function readReplyRejectTest(output: string, expected: string) { ); } -Deno.test("readReply() - mixed array", async () => - await readReplyTest("*3\r\n$5\r\nstring\r\n:123\r\n$-1", [ +Deno.test("readReply() - mixed array", () => + readReplyTest("*3\r\n$5\r\nstring\r\n:123\r\n$-1", [ "string", 123, null, ])); -Deno.test("readReply() - empty array", async () => - await readReplyTest("*0\r\n", [])); +Deno.test("readReply() - empty array", () => readReplyTest("*0\r\n", [])); -Deno.test("readReply() - null array", async () => - await readReplyTest("*-1\r\n", null)); +Deno.test("readReply() - null array", () => readReplyTest("*-1\r\n", null)); -Deno.test("readReply() - nested array", async () => - await readReplyTest("*2\r\n*3\r\n:1\r\n$5\r\nhello\r\n:2\r\n#f\r\n", [[ +Deno.test("readReply() - nested array", () => + readReplyTest("*2\r\n*3\r\n:1\r\n$5\r\nhello\r\n:2\r\n#f\r\n", [[ 1, "hello", 2, @@ -50,38 +48,32 @@ Deno.test("readReply() - attribute", async () => { ]); }); -Deno.test("readReply() - positive big number", async () => - await readReplyTest( +Deno.test("readReply() - positive big number", () => + readReplyTest( "(3492890328409238509324850943850943825024385\r\n", 3492890328409238509324850943850943825024385n, )); -Deno.test("readReply() - negative big number", async () => - await readReplyTest( +Deno.test("readReply() - negative big number", () => + readReplyTest( "(-3492890328409238509324850943850943825024385\r\n", -3492890328409238509324850943850943825024385n, )); -Deno.test("readReply() - true boolean", async () => - await readReplyTest("#t\r\n", true)); +Deno.test("readReply() - true boolean", () => readReplyTest("#t\r\n", true)); -Deno.test("readReply() - false boolean", async () => - await readReplyTest("#f\r\n", false)); +Deno.test("readReply() - false boolean", () => readReplyTest("#f\r\n", false)); -Deno.test("readReply() - integer", async () => - await readReplyTest(":42\r\n", 42)); +Deno.test("readReply() - integer", () => readReplyTest(":42\r\n", 42)); -Deno.test("readReply() - bulk string", async () => - await readReplyTest("$5\r\nhello\r\n", "hello")); +Deno.test("readReply() - bulk string", () => + readReplyTest("$5\r\nhello\r\n", "hello")); -Deno.test("readReply() - emtpy bulk string", async () => - await readReplyTest("$0\r\n\r\n", "")); +Deno.test("readReply() - emtpy bulk string", () => + readReplyTest("$0\r\n\r\n", "")); -Deno.test("readReply() - null bulk string", async () => - await readReplyTest("$-1\r\n", null)); - -Deno.test("readReply() - raw bulk string", async () => { -}); +Deno.test("readReply() - null bulk string", () => + readReplyTest("$-1\r\n", null)); Deno.test("readReply() - blob error", async () => { await readReplyRejectTest( @@ -97,80 +89,70 @@ Deno.test("readReply() - error", async () => { ); }); -Deno.test("readReply() - double", async () => - await readReplyTest(",1.23\r\n", 1.23)); +Deno.test("readReply() - double", () => readReplyTest(",1.23\r\n", 1.23)); -Deno.test("readReply() - positive infinity double", async () => - await readReplyTest(",inf\r\n", Infinity)); +Deno.test("readReply() - positive infinity double", () => + readReplyTest(",inf\r\n", Infinity)); -Deno.test("readReply() - negative infinity double", async () => - await readReplyTest(",-inf\r\n", -Infinity)); +Deno.test("readReply() - negative infinity double", () => + readReplyTest(",-inf\r\n", -Infinity)); -Deno.test("readReply() - map", async () => - await readReplyTest("%2\r\n+first\r\n:1\r\n+second\r\n:2\r\n", { +Deno.test("readReply() - map", () => + readReplyTest("%2\r\n+first\r\n:1\r\n+second\r\n:2\r\n", { first: 1, second: 2, })); -Deno.test("readReply() - null", async () => await readReplyTest("_\r\n", null)); +Deno.test("readReply() - null", () => readReplyTest("_\r\n", null)); -Deno.test("readReply() - push", async () => { - await readReplyTest( +Deno.test("readReply() - push", () => + readReplyTest( ">4\r\n+pubsub\r\n+message\r\n+somechannel\r\n+this is the message\r\n", ["pubsub", "message", "somechannel", "this is the message"], - ); -}); + )); -Deno.test("readReply() - set", async () => { - await readReplyTest( +Deno.test("readReply() - set", () => + readReplyTest( "~5\r\n+orange\r\n+apple\r\n#t\r\n:100\r\n:999\r\n", new Set(["orange", "apple", true, 100, 999]), - ); -}); + )); -Deno.test("readReply() - simple string", async () => - await readReplyTest("+OK\r\n", "OK")); +Deno.test("readReply() - simple string", () => readReplyTest("+OK\r\n", "OK")); -Deno.test("readReply() - verbatim string", async () => { - await readReplyTest("=15\r\ntxt:Some string\r\n", "txt:Some string"); -}); +Deno.test("readReply() - verbatim string", () => + readReplyTest("=15\r\ntxt:Some string\r\n", "txt:Some string")); Deno.test("readReply() - large reply", async () => { const reply = "a".repeat(4096 * 2); await readReplyTest(`$${reply.length}\r\n${reply}\r\n`, reply); }); -const PORT = 6379; -const redisConn = await Deno.connect({ port: PORT }); +const redisConn = await Deno.connect({ port: 6379 }); const redisClient = new RedisClient(redisConn); await redisClient.sendCommand(["FLUSHALL"]); -async function sendCommandTest( - command: Command, - expected: Reply, -): Promise { +async function sendCommandTest(command: Command, expected: Reply) { assertEquals(await redisClient.sendCommand(command), expected); } -Deno.test("redisClient.sendCommand() - transactions", async () => { +Deno.test("RedisClient.sendCommand() - transactions", async () => { await sendCommandTest(["MULTI"], "OK"); await sendCommandTest(["INCR", "FOO"], "QUEUED"); await sendCommandTest(["INCR", "BAR"], "QUEUED"); await sendCommandTest(["EXEC"], [1, 1]); }); -Deno.test("redisClient.sendCommand() - raw data", async () => { +Deno.test("RedisClient.sendCommand() - raw data", async () => { const data = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); assertEquals(await redisClient.sendCommand(["SET", "binary", data]), "OK"); assertEquals(await redisClient.sendCommand(["GET", "binary"], true), data); }); -Deno.test("redisClient.sendCommand() - eval script", async () => { - await sendCommandTest(["EVAL", "return ARGV[1]", 0, "hello"], "hello"); -}); +Deno.test("RedisClient.sendCommand() - eval script", () => + sendCommandTest(["EVAL", "return ARGV[1]", 0, "hello"], "hello")); -Deno.test("redisClient.sendCommand() - Lua script", async () => { +Deno.test("RedisClient.sendCommand() - Lua script", async () => { await sendCommandTest([ "FUNCTION", "LOAD", @@ -179,7 +161,7 @@ Deno.test("redisClient.sendCommand() - Lua script", async () => { await sendCommandTest(["FCALL", "knockknock", 0], "Who's there?"); }); -Deno.test("redisClient.sendCommand() - RESP3", async () => { +Deno.test("RedisClient.sendCommand() - RESP3", async () => { await redisClient.sendCommand(["HELLO", 3]); await sendCommandTest(["HSET", "hash3", "foo", 1, "bar", 2], 2); await sendCommandTest(["HGETALL", "hash3"], { @@ -188,39 +170,17 @@ Deno.test("redisClient.sendCommand() - RESP3", async () => { }); }); -Deno.test("redisClient.sendCommand() - race condition", async () => { - async function fn() { +Deno.test("RedisClient.sendCommand() - race condition (#146)", async () => { + await Promise.all(Array.from({ length: 20 }, async () => { const key = crypto.randomUUID(); const value = crypto.randomUUID(); await redisClient.sendCommand(["SET", key, value]); const result = await redisClient.sendCommand(["GET", key]); assertEquals(result, value); - } - - await Promise.all([ - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - fn(), - ]); + })); }); -Deno.test("redisClient.pipelineCommands()", async () => { +Deno.test("RedisClient.pipelineCommands()", async () => { assertEquals( await redisClient.pipelineCommands([ ["INCR", "X"], @@ -232,7 +192,7 @@ Deno.test("redisClient.pipelineCommands()", async () => { ); }); -Deno.test("redisClient.writeCommand() + redisClient.readReplies()", async () => { +Deno.test("RedisClient.writeCommand() + RedisClient.readReplies()", async () => { await redisClient.writeCommand(["SUBSCRIBE", "mychannel"]); const iterator = redisClient.readReplies(); assertEquals(await iterator.next(), { @@ -246,7 +206,7 @@ Deno.test("redisClient.writeCommand() + redisClient.readReplies()", async () => }); }); -Deno.test("redisClient.sendCommand() - no reply", async () => { +Deno.test("RedisClient.sendCommand() - no reply", async () => { await assertRejects( async () => await redisClient.sendCommand(["SHUTDOWN"]), TypeError,