|
| 1 | +import Redis from "../../lib/redis"; |
| 2 | +import { expect } from "chai"; |
| 3 | +import MockServer from "../helpers/mock_server"; |
| 4 | + |
| 5 | +// AWS Elasticache closes the connection immediately when it encounters a READONLY error |
| 6 | +function simulateElasticache(options: { |
| 7 | + reconnectOnErrorValue: boolean | number; |
| 8 | +}) { |
| 9 | + let inTransaction = false; |
| 10 | + const mockServer = new MockServer(30000, (argv, socket, flags) => { |
| 11 | + switch (argv[0]) { |
| 12 | + case "multi": |
| 13 | + inTransaction = true; |
| 14 | + return MockServer.raw("+OK\r\n"); |
| 15 | + case "del": |
| 16 | + flags.disconnect = true; |
| 17 | + return new Error( |
| 18 | + "READONLY You can't write against a read only replica." |
| 19 | + ); |
| 20 | + case "get": |
| 21 | + return inTransaction ? MockServer.raw("+QUEUED\r\n") : argv[1]; |
| 22 | + case "exec": |
| 23 | + inTransaction = false; |
| 24 | + return []; |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + return new Redis({ |
| 29 | + port: 30000, |
| 30 | + reconnectOnError(err: Error): boolean | number { |
| 31 | + // bring the mock server back up |
| 32 | + mockServer.connect(); |
| 33 | + return options.reconnectOnErrorValue; |
| 34 | + } |
| 35 | + }); |
| 36 | +} |
| 37 | + |
| 38 | +function expectReplyError(err) { |
| 39 | + expect(err).to.exist; |
| 40 | + expect(err.name).to.eql("ReplyError"); |
| 41 | +} |
| 42 | + |
| 43 | +function expectAbortError(err) { |
| 44 | + expect(err).to.exist; |
| 45 | + expect(err.name).to.eql("AbortError"); |
| 46 | + expect(err.message).to.eql("Command aborted due to connection close"); |
| 47 | +} |
| 48 | + |
| 49 | +describe("elasticache", function() { |
| 50 | + it("should abort a failed transaction when connection is lost", function(done) { |
| 51 | + const redis = simulateElasticache({ reconnectOnErrorValue: true }); |
| 52 | + |
| 53 | + redis |
| 54 | + .multi() |
| 55 | + .del("foo") |
| 56 | + .del("bar") |
| 57 | + .exec(err => { |
| 58 | + expectAbortError(err); |
| 59 | + expect(err.command).to.eql({ |
| 60 | + name: "exec", |
| 61 | + args: [] |
| 62 | + }); |
| 63 | + expect(err.previousErrors).to.have.lengthOf(2); |
| 64 | + expectReplyError(err.previousErrors[0]); |
| 65 | + expect(err.previousErrors[0].command).to.eql({ |
| 66 | + name: "del", |
| 67 | + args: ["foo"] |
| 68 | + }); |
| 69 | + expectAbortError(err.previousErrors[1]); |
| 70 | + expect(err.previousErrors[1].command).to.eql({ |
| 71 | + name: "del", |
| 72 | + args: ["bar"] |
| 73 | + }); |
| 74 | + |
| 75 | + // ensure we've recovered into a healthy state |
| 76 | + redis.get("foo", (err, res) => { |
| 77 | + expect(res).to.eql("foo"); |
| 78 | + done(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + }); |
| 82 | + |
| 83 | + it("should not resend failed transaction commands", function(done) { |
| 84 | + const redis = simulateElasticache({ reconnectOnErrorValue: 2 }); |
| 85 | + redis |
| 86 | + .multi() |
| 87 | + .del("foo") |
| 88 | + .get("bar") |
| 89 | + .exec(err => { |
| 90 | + expectAbortError(err); |
| 91 | + expect(err.command).to.eql({ |
| 92 | + name: "exec", |
| 93 | + args: [] |
| 94 | + }); |
| 95 | + expect(err.previousErrors).to.have.lengthOf(2); |
| 96 | + expectAbortError(err.previousErrors[0]); |
| 97 | + expect(err.previousErrors[0].command).to.eql({ |
| 98 | + name: "del", |
| 99 | + args: ["foo"] |
| 100 | + }); |
| 101 | + expectAbortError(err.previousErrors[1]); |
| 102 | + expect(err.previousErrors[1].command).to.eql({ |
| 103 | + name: "get", |
| 104 | + args: ["bar"] |
| 105 | + }); |
| 106 | + |
| 107 | + // ensure we've recovered into a healthy state |
| 108 | + redis.get("foo", (err, res) => { |
| 109 | + expect(res).to.eql("foo"); |
| 110 | + done(); |
| 111 | + }); |
| 112 | + }); |
| 113 | + }); |
| 114 | + |
| 115 | + it("should resend intact pipelines", function(done) { |
| 116 | + const redis = simulateElasticache({ reconnectOnErrorValue: true }); |
| 117 | + |
| 118 | + let p1Result; |
| 119 | + redis |
| 120 | + .pipeline() |
| 121 | + .del("foo") |
| 122 | + .get("bar") |
| 123 | + .exec((err, result) => (p1Result = result)); |
| 124 | + |
| 125 | + redis |
| 126 | + .pipeline() |
| 127 | + .get("baz") |
| 128 | + .get("qux") |
| 129 | + .exec((err, p2Result) => { |
| 130 | + // First pipeline should have been aborted |
| 131 | + expect(p1Result).to.have.lengthOf(2); |
| 132 | + expect(p1Result[0]).to.have.lengthOf(1); |
| 133 | + expect(p1Result[1]).to.have.lengthOf(1); |
| 134 | + expectReplyError(p1Result[0][0]); |
| 135 | + expect(p1Result[0][0].command).to.eql({ |
| 136 | + name: "del", |
| 137 | + args: ["foo"] |
| 138 | + }); |
| 139 | + expectAbortError(p1Result[1][0]); |
| 140 | + expect(p1Result[1][0].command).to.eql({ |
| 141 | + name: "get", |
| 142 | + args: ["bar"] |
| 143 | + }); |
| 144 | + |
| 145 | + // Second pipeline was intact and should have been retried successfully |
| 146 | + expect(p2Result).to.have.lengthOf(2); |
| 147 | + expect(p2Result[0]).to.eql([null, "baz"]); |
| 148 | + expect(p2Result[1]).to.eql([null, "qux"]); |
| 149 | + |
| 150 | + done(); |
| 151 | + }); |
| 152 | + }); |
| 153 | +}); |
0 commit comments