|
| 1 | +import { expect } from "chai"; |
| 2 | +import Redis, { Cluster } from "../lib"; |
| 3 | + |
| 4 | +const masters = [30000, 30001, 30002]; |
| 5 | +const replicas = [30003, 30004, 30005]; |
| 6 | + |
| 7 | +describe("cluster", () => { |
| 8 | + afterEach(async () => { |
| 9 | + for (const port of masters) { |
| 10 | + const redis = new Redis(port); |
| 11 | + await redis.flushall(); |
| 12 | + await redis.script("FLUSH"); |
| 13 | + } |
| 14 | + // Wait for replication |
| 15 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 16 | + }); |
| 17 | + |
| 18 | + it("discovers nodes from master", async () => { |
| 19 | + const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }]); |
| 20 | + await cluster.set("foo", "bar"); |
| 21 | + expect(await cluster.get("foo")).to.eql("bar"); |
| 22 | + }); |
| 23 | + |
| 24 | + it("discovers nodes from replica", async () => { |
| 25 | + const cluster = new Cluster([{ host: "127.0.0.1", port: replicas[0] }]); |
| 26 | + await cluster.set("foo", "bar"); |
| 27 | + expect(await cluster.get("foo")).to.eql("bar"); |
| 28 | + }); |
| 29 | + |
| 30 | + describe("#nodes()", () => { |
| 31 | + it("returns master nodes", async () => { |
| 32 | + const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }]); |
| 33 | + await cluster.info(); |
| 34 | + const nodes = cluster.nodes("master"); |
| 35 | + expect(nodes.map((node) => node.options.port).sort()).to.eql(masters); |
| 36 | + }); |
| 37 | + |
| 38 | + it("returns replica nodes", async () => { |
| 39 | + const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }]); |
| 40 | + await cluster.info(); |
| 41 | + const nodes = cluster.nodes("slave"); |
| 42 | + expect(nodes.map((node) => node.options.port).sort()).to.eql(replicas); |
| 43 | + }); |
| 44 | + |
| 45 | + it("returns all nodes", async () => { |
| 46 | + const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }]); |
| 47 | + await cluster.info(); |
| 48 | + const nodes = cluster.nodes(); |
| 49 | + expect(nodes.map((node) => node.options.port).sort()).to.eql( |
| 50 | + masters.concat(replicas) |
| 51 | + ); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe("scaleReads", () => { |
| 56 | + it("ensures non-readonly commands still working", async () => { |
| 57 | + const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }], { |
| 58 | + scaleReads: "slave", |
| 59 | + }); |
| 60 | + await cluster.set("foo", "bar"); |
| 61 | + expect(await cluster.get("foo")).to.eql("bar"); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + describe("pipeline", () => { |
| 66 | + it("ensures script ordering when not loaded", async () => { |
| 67 | + const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }]); |
| 68 | + cluster.defineCommand("myget", { |
| 69 | + numberOfKeys: 1, |
| 70 | + lua: "return redis.call('GET', KEYS[1])", |
| 71 | + }); |
| 72 | + |
| 73 | + expect( |
| 74 | + await cluster |
| 75 | + .pipeline() |
| 76 | + // @ts-expect-error |
| 77 | + .myget("foo") |
| 78 | + .set("foo", "setAfterMyGET") |
| 79 | + .myget("foo") |
| 80 | + .exec() |
| 81 | + ).to.eql([ |
| 82 | + [null, null], |
| 83 | + [null, "OK"], |
| 84 | + [null, "setAfterMyGET"], |
| 85 | + ]); |
| 86 | + }); |
| 87 | + |
| 88 | + it("falls back to eval when the cache is flushed", async () => { |
| 89 | + const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }]); |
| 90 | + cluster.defineCommand("myget", { |
| 91 | + numberOfKeys: 1, |
| 92 | + lua: "return redis.call('GET', KEYS[1])", |
| 93 | + }); |
| 94 | + |
| 95 | + // @ts-expect-error |
| 96 | + await cluster.myget("foo"); |
| 97 | + |
| 98 | + for (const node of cluster.nodes("master")) { |
| 99 | + await node.script("FLUSH"); |
| 100 | + } |
| 101 | + |
| 102 | + expect( |
| 103 | + await cluster |
| 104 | + .pipeline() |
| 105 | + // @ts-expect-error |
| 106 | + .myget("foo") |
| 107 | + .set("foo", "setAfterMyGET") |
| 108 | + .myget("foo") |
| 109 | + .exec() |
| 110 | + ).to.eql([ |
| 111 | + [null, "setAfterMyGET"], |
| 112 | + [null, "OK"], |
| 113 | + [null, "setAfterMyGET"], |
| 114 | + ]); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe("auto pipelining", () => { |
| 119 | + it("works", async () => { |
| 120 | + const cluster = new Cluster([{ host: "127.0.0.1", port: masters[0] }], { |
| 121 | + enableAutoPipelining: true, |
| 122 | + }); |
| 123 | + |
| 124 | + cluster.set("foo", "auto pipelining"); |
| 125 | + expect(await cluster.get("foo")).to.eql("auto pipelining"); |
| 126 | + }); |
| 127 | + }); |
| 128 | +}); |
0 commit comments