-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Redis integration to support valkey-cli and valkey-server; adj…
…ust package dependencies and improve error handling Signed-off-by: avifenesh <[email protected]>
- Loading branch information
Showing
11 changed files
with
220 additions
and
147 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,76 @@ | ||
/* eslint no-undef: off */ | ||
/* eslint @typescript-eslint/no-require-imports: off */ | ||
const { AsyncClient } = require("glide-rs"); | ||
const RedisServer = require("redis-server"); | ||
const { GlideClient } = require("@valkey/valkey-glide"); | ||
const FreePort = require("find-free-port"); | ||
const { execFile } = require("child_process"); | ||
const { promisify } = require("node:util"); | ||
const { exec, spawn } = require("node:child_process"); | ||
const execAsync = promisify(exec); | ||
|
||
const PORT_NUMBER = 4001; | ||
let server; | ||
let port; | ||
|
||
function checkCliAvailability(cli) { | ||
return new Promise((resolve) => { | ||
execFile(cli, ["--version"], (error) => { | ||
resolve(!error); | ||
}); | ||
}); | ||
async function checkCommandAvailability(command) { | ||
try { | ||
const { stdout } = await execAsync(`which ${command}`); | ||
console.log(stdout); | ||
return Boolean(stdout.trim()); | ||
} catch (error) { | ||
console.error(error); | ||
return false; | ||
} | ||
} | ||
|
||
async function flushallOnPort(port) { | ||
const redisCliAvailable = await checkCliAvailability("redis-cli"); | ||
const valkeyCliAvailable = await checkCliAvailability("valkey-cli"); | ||
async function main() { | ||
console.log("Starting main"); | ||
const port = await FreePort(PORT_NUMBER).then(([free_port]) => free_port); | ||
const redisServerAvailable = await checkCommandAvailability("redis-server"); | ||
const valkeyServerAvailable = | ||
await checkCommandAvailability("valkey-server"); | ||
|
||
if (!redisCliAvailable && !valkeyCliAvailable) { | ||
throw new Error("Neither redis-cli nor valkey-cli is available"); | ||
if (!redisServerAvailable && !valkeyServerAvailable) { | ||
throw new Error("Neither valkey nor redis are available"); | ||
} | ||
|
||
const cli = redisCliAvailable ? "valkey-cli" : "redis-cli"; | ||
return new Promise((resolve, reject) => { | ||
execFile(cli, ["-p", port, "FLUSHALL"], (error, _, stderr) => { | ||
if (error) { | ||
console.error(stderr); | ||
reject(error); | ||
} else { | ||
const server = valkeyServerAvailable ? "valkey-server" : "redis-server"; | ||
console.log(server); | ||
|
||
const serverProcess = spawn(server, ["--port", port.toString()], { | ||
stdio: ["ignore", "pipe", "pipe"], | ||
}); | ||
|
||
await new Promise((resolve) => { | ||
serverProcess.stdout.on("data", (data) => { | ||
console.log(`${data}`); | ||
|
||
if (data.toString().includes("Ready to accept connections")) { | ||
resolve(); | ||
} | ||
}); | ||
|
||
serverProcess.stderr.on("data", (data) => { | ||
console.error(`${data}`); | ||
}); | ||
}); | ||
} | ||
|
||
FreePort(PORT_NUMBER) | ||
.then(([free_port]) => { | ||
port = free_port; | ||
server = new RedisServer(port); | ||
server.open(async (err) => { | ||
if (err) { | ||
console.error("Error opening server:", err); | ||
throw err; | ||
} | ||
const client = await GlideClient.createClient({ | ||
addresses: [{ host: "localhost", port }], | ||
}); | ||
const setResult = await client.set("test", "test"); | ||
console.log(setResult); | ||
let getResult = await client.get("test"); | ||
console.log(getResult); | ||
|
||
const client = AsyncClient.CreateConnection( | ||
`redis://localhost:${port}`, | ||
); | ||
await client.set("test", "test"); | ||
let result = await client.get("test"); | ||
if (getResult !== "test") { | ||
throw new Error("Common Test failed"); | ||
} else { | ||
console.log("Common Test passed"); | ||
} | ||
|
||
if (result !== "test") { | ||
throw new Error("Common Test failed"); | ||
} else { | ||
console.log("Common Test passed"); | ||
} | ||
await client.flushall(); | ||
client.close(); | ||
serverProcess.kill(); | ||
} | ||
|
||
await flushallOnPort(port).then(() => { | ||
console.log("db flushed"); | ||
}); | ||
await server.close().then(() => { | ||
console.log("server closed"); | ||
}); | ||
}); | ||
}) | ||
.catch((error) => { | ||
console.error("Error occurred while finding a free port:", error); | ||
}); | ||
main().then(() => { | ||
console.log("Done"); | ||
process.exit(0); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 49 additions & 42 deletions
91
node/hybrid-node-tests/ecmascript-test/ecmascript-test.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,73 @@ | ||
/* eslint no-undef: off */ | ||
import { execFile } from "child_process"; | ||
import { spawn, execFile } from "child_process"; | ||
import findFreePorts from "find-free-ports"; | ||
import { AsyncClient } from "glide-rs"; | ||
import RedisServer from "redis-server"; | ||
import { GlideClient } from "@valkey/valkey-glide"; | ||
|
||
const PORT_NUMBER = 4001; | ||
let server; | ||
let port; | ||
|
||
function checkCliAvailability(cli) { | ||
function checkCommandAvailability(command) { | ||
return new Promise((resolve) => { | ||
execFile(cli, ["--version"], (error) => { | ||
execFile(command, ["--version"], (error) => { | ||
resolve(!error); | ||
}); | ||
}); | ||
} | ||
|
||
async function flushallOnPort(port) { | ||
const redisCliAvailable = await checkCliAvailability("redis-cli"); | ||
const valkeyCliAvailable = await checkCliAvailability("valkey-cli"); | ||
async function main() { | ||
console.log("Starting main"); | ||
const port = await findFreePorts(PORT_NUMBER).then( | ||
([free_port]) => free_port, | ||
); | ||
const redisServerAvailable = await checkCommandAvailability("redis-server"); | ||
const valkeyServerAvailable = | ||
await checkCommandAvailability("valkey-server"); | ||
|
||
if (!redisCliAvailable && !valkeyCliAvailable) { | ||
throw new Error("Neither redis-cli nor valkey-cli is available"); | ||
if (!redisServerAvailable && !valkeyServerAvailable) { | ||
throw new Error("Neither valkey nor redis are available"); | ||
} | ||
|
||
const cli = redisCliAvailable ? "valkey-cli" : "redis-cli"; | ||
return new Promise((resolve, reject) => { | ||
execFile(cli, ["-p", port, "FLUSHALL"], (error, _, stderr) => { | ||
if (error) { | ||
console.error(stderr); | ||
reject(error); | ||
} else { | ||
const server = valkeyServerAvailable ? "valkey-server" : "redis-server"; | ||
console.log(server); | ||
|
||
const serverProcess = spawn(server, ["--port", port.toString()], { | ||
stdio: ["ignore", "pipe", "pipe"], | ||
}); | ||
|
||
await new Promise((resolve) => { | ||
serverProcess.stdout.on("data", (data) => { | ||
console.log(`${data}`); | ||
|
||
if (data.toString().includes("Ready to accept connections")) { | ||
resolve(); | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
port = await findFreePorts(PORT_NUMBER).then(([free_port]) => free_port); | ||
server = await new Promise((resolve, reject) => { | ||
const server = new RedisServer(port); | ||
server.open(async (err) => { | ||
if (err) { | ||
reject(err); | ||
} | ||
serverProcess.stderr.on("data", (data) => { | ||
console.error(`${data}`); | ||
}); | ||
}); | ||
|
||
resolve(server); | ||
const client = await GlideClient.createClient({ | ||
addresses: [{ host: "localhost", port }], | ||
}); | ||
}); | ||
const client = AsyncClient.CreateConnection("redis://localhost:" + port); | ||
await client.set("test", "test"); | ||
let result = await client.get("test"); | ||
const setResult = await client.set("test", "test"); | ||
console.log(setResult); | ||
let getResult = await client.get("test"); | ||
console.log(getResult); | ||
|
||
if (result !== "test") { | ||
throw new Error("Ecma Test failed"); | ||
} else { | ||
console.log("Ecma Test passed"); | ||
if (getResult !== "test") { | ||
throw new Error("Ecma Test failed"); | ||
} else { | ||
console.log("Ecma Test passed"); | ||
} | ||
|
||
await client.flushall(); | ||
client.close(); | ||
serverProcess.kill(); | ||
console.log("Done"); | ||
} | ||
|
||
await flushallOnPort(port).then(() => { | ||
console.log("db flushed"); | ||
}); | ||
await server.close().then(() => { | ||
console.log("server closed"); | ||
main().catch((err) => { | ||
console.error(err); | ||
process.exit(1); | ||
}); |
Oops, something went wrong.