-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
163 additions
and
13 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 |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { expect } from "chai"; | ||
import Redis, { Cluster } from "../lib"; | ||
|
||
const masters = [30000, 30001, 30002]; | ||
const replicas = [30003, 30004, 30005]; | ||
|
||
describe("cluster", () => { | ||
afterEach(async () => { | ||
for (const port of masters) { | ||
const redis = new Redis(port); | ||
await redis.flushall(); | ||
await redis.script("FLUSH"); | ||
} | ||
// Wait for replication | ||
await new Promise((resolve) => setTimeout(resolve, 500)); | ||
}); | ||
|
||
it("discovers nodes from master", async () => { | ||
const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }]); | ||
await cluster.set("foo", "bar"); | ||
expect(await cluster.get("foo")).to.eql("bar"); | ||
}); | ||
|
||
it("discovers nodes from replica", async () => { | ||
const cluster = new Cluster([{ host: "127.0.0.1", port: replicas[0] }]); | ||
await cluster.set("foo", "bar"); | ||
expect(await cluster.get("foo")).to.eql("bar"); | ||
}); | ||
|
||
describe("#nodes()", () => { | ||
it("returns master nodes", async () => { | ||
const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }]); | ||
await cluster.info(); | ||
const nodes = cluster.nodes("master"); | ||
expect(nodes.map((node) => node.options.port).sort()).to.eql(masters); | ||
}); | ||
|
||
it("returns replica nodes", async () => { | ||
const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }]); | ||
await cluster.info(); | ||
const nodes = cluster.nodes("slave"); | ||
expect(nodes.map((node) => node.options.port).sort()).to.eql(replicas); | ||
}); | ||
|
||
it("returns all nodes", async () => { | ||
const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }]); | ||
await cluster.info(); | ||
const nodes = cluster.nodes(); | ||
expect(nodes.map((node) => node.options.port).sort()).to.eql( | ||
masters.concat(replicas) | ||
); | ||
}); | ||
}); | ||
|
||
describe("scaleReads", () => { | ||
it("ensures non-readonly commands still working", async () => { | ||
const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }], { | ||
scaleReads: "slave", | ||
}); | ||
await cluster.set("foo", "bar"); | ||
expect(await cluster.get("foo")).to.eql("bar"); | ||
}); | ||
}); | ||
|
||
describe("pipeline", () => { | ||
it("ensures script ordering when not loaded", async () => { | ||
const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }]); | ||
cluster.defineCommand("myget", { | ||
numberOfKeys: 1, | ||
lua: "return redis.call('GET', KEYS[1])", | ||
}); | ||
|
||
expect( | ||
await cluster | ||
.pipeline() | ||
// @ts-expect-error | ||
.myget("foo") | ||
.set("foo", "setAfterMyGET") | ||
.myget("foo") | ||
.exec() | ||
).to.eql([ | ||
[null, null], | ||
[null, "OK"], | ||
[null, "setAfterMyGET"], | ||
]); | ||
}); | ||
|
||
it("falls back to eval when the cache is flushed", async () => { | ||
const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }]); | ||
cluster.defineCommand("myget", { | ||
numberOfKeys: 1, | ||
lua: "return redis.call('GET', KEYS[1])", | ||
}); | ||
|
||
// @ts-expect-error | ||
await cluster.myget("foo"); | ||
|
||
for (const node of cluster.nodes("master")) { | ||
await node.script("FLUSH"); | ||
} | ||
|
||
expect( | ||
await cluster | ||
.pipeline() | ||
// @ts-expect-error | ||
.myget("foo") | ||
.set("foo", "setAfterMyGET") | ||
.myget("foo") | ||
.exec() | ||
).to.eql([ | ||
[null, "setAfterMyGET"], | ||
[null, "OK"], | ||
[null, "setAfterMyGET"], | ||
]); | ||
}); | ||
}); | ||
|
||
describe("auto pipelining", () => { | ||
it("works", async () => { | ||
const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }], { | ||
enableAutoPipelining: true, | ||
}); | ||
|
||
cluster.set("foo", "auto pipelining"); | ||
expect(await cluster.get("foo")).to.eql("auto pipelining"); | ||
}); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
FROM grokzen/redis-cluster | ||
|
||
RUN apt-get --allow-releaseinfo-change update | ||
|
||
RUN apt-get update -y && apt-get install -y redis-server && apt-get install -y curl | ||
RUN touch /etc/apt/apt.conf.d/99verify-peer.conf \ | ||
&& echo >>/etc/apt/apt.conf.d/99verify-peer.conf "Acquire { https::Verify-Peer false }" | ||
RUN echo insecure >> $HOME/.curlrc | ||
|
||
RUN curl --insecure -fsSL https://deb.nodesource.com/setup_14.x | bash - | ||
RUN apt-get install -y nodejs | ||
RUN apt-get clean | ||
RUN mkdir /code | ||
WORKDIR /code | ||
ADD package.json package-lock.json ./ | ||
# Install npm dependencies without converting the lockfile version in npm 7, | ||
# and remove temporary files to save space when developing locally. | ||
RUN npm install --no-save && npm cache clean --force | ||
|
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 |
---|---|---|
@@ -0,0 +1,4 @@ | ||
docker run -e "INITIAL_PORT=30000" -e "IP=0.0.0.0" -p 30000-30005:30000-30005 grokzen/redis-cluster:latest & | ||
npm install | ||
sleep 15 | ||
npm run test:js:cluster | npm run test:js:cluster |