|
| 1 | +const calculateSlot = require('cluster-key-slot') |
| 2 | + |
| 3 | +describe('NAT', () => { |
| 4 | + it('works for normal case', (done) => { |
| 5 | + const slotTable = [ |
| 6 | + [0, 1, ['192.168.1.1', 30001]], |
| 7 | + [2, 16383, ['192.168.1.2', 30001]] |
| 8 | + ] |
| 9 | + |
| 10 | + let cluster |
| 11 | + new MockServer(30001, null, slotTable) |
| 12 | + new MockServer(30002, ([command, arg]) => { |
| 13 | + if (command === 'get' && arg === 'foo') { |
| 14 | + cluster.disconnect() |
| 15 | + done() |
| 16 | + } |
| 17 | + }, slotTable) |
| 18 | + |
| 19 | + cluster = new Redis.Cluster([{ |
| 20 | + host: '127.0.0.1', |
| 21 | + port: 30001 |
| 22 | + }], { |
| 23 | + natMap: { |
| 24 | + '192.168.1.1:30001': {host: '127.0.0.1', port: 30001}, |
| 25 | + '192.168.1.2:30001': {host: '127.0.0.1', port: 30002} |
| 26 | + } |
| 27 | + }) |
| 28 | + |
| 29 | + cluster.get('foo') |
| 30 | + }) |
| 31 | + |
| 32 | + it('works if natMap does not match all the cases', (done) => { |
| 33 | + const slotTable = [ |
| 34 | + [0, 1, ['192.168.1.1', 30001]], |
| 35 | + [2, 16383, ['127.0.0.1', 30002]] |
| 36 | + ] |
| 37 | + |
| 38 | + let cluster |
| 39 | + new MockServer(30001, null, slotTable) |
| 40 | + new MockServer(30002, ([command, arg]) => { |
| 41 | + if (command === 'get' && arg === 'foo') { |
| 42 | + cluster.disconnect() |
| 43 | + done() |
| 44 | + } |
| 45 | + }, slotTable) |
| 46 | + |
| 47 | + cluster = new Redis.Cluster([{ |
| 48 | + host: '127.0.0.1', |
| 49 | + port: 30001 |
| 50 | + }], { |
| 51 | + natMap: { |
| 52 | + '192.168.1.1:30001': {host: '127.0.0.1', port: 30001} |
| 53 | + } |
| 54 | + }) |
| 55 | + |
| 56 | + cluster.get('foo') |
| 57 | + }) |
| 58 | + |
| 59 | + it('works for moved', (done) => { |
| 60 | + const slotTable = [ |
| 61 | + [0, 16383, ['192.168.1.1', 30001]] |
| 62 | + ] |
| 63 | + |
| 64 | + let cluster |
| 65 | + new MockServer(30001, ([command, arg]) => { |
| 66 | + if (command === 'get' && arg === 'foo') { |
| 67 | + return new Error('MOVED ' + calculateSlot('foo') + ' 192.168.1.2:30001'); |
| 68 | + } |
| 69 | + }, slotTable) |
| 70 | + new MockServer(30002, ([command, arg]) => { |
| 71 | + if (command === 'get' && arg === 'foo') { |
| 72 | + cluster.disconnect() |
| 73 | + done() |
| 74 | + } |
| 75 | + }, slotTable) |
| 76 | + |
| 77 | + cluster = new Redis.Cluster([{ |
| 78 | + host: '127.0.0.1', |
| 79 | + port: 30001 |
| 80 | + }], { |
| 81 | + natMap: { |
| 82 | + '192.168.1.1:30001': {host: '127.0.0.1', port: 30001}, |
| 83 | + '192.168.1.2:30001': {host: '127.0.0.1', port: 30002} |
| 84 | + } |
| 85 | + }) |
| 86 | + |
| 87 | + cluster.get('foo') |
| 88 | + }) |
| 89 | + |
| 90 | + it('works for ask', (done) => { |
| 91 | + const slotTable = [ |
| 92 | + [0, 16383, ['192.168.1.1', 30001]] |
| 93 | + ] |
| 94 | + |
| 95 | + let cluster |
| 96 | + let asked = false |
| 97 | + new MockServer(30001, ([command, arg]) => { |
| 98 | + if (command === 'get' && arg === 'foo') { |
| 99 | + return new Error('ASK ' + calculateSlot('foo') + ' 192.168.1.2:30001'); |
| 100 | + } |
| 101 | + }, slotTable) |
| 102 | + new MockServer(30002, ([command, arg]) => { |
| 103 | + if (command === 'asking') { |
| 104 | + asked = true |
| 105 | + } |
| 106 | + if (command === 'get' && arg === 'foo') { |
| 107 | + if (!asked) { |
| 108 | + throw new Error('expected asked to be true') |
| 109 | + } |
| 110 | + cluster.disconnect() |
| 111 | + done() |
| 112 | + } |
| 113 | + }, slotTable) |
| 114 | + |
| 115 | + cluster = new Redis.Cluster([{ |
| 116 | + host: '127.0.0.1', |
| 117 | + port: 30001 |
| 118 | + }], { |
| 119 | + natMap: { |
| 120 | + '192.168.1.1:30001': {host: '127.0.0.1', port: 30001}, |
| 121 | + '192.168.1.2:30001': {host: '127.0.0.1', port: 30002} |
| 122 | + } |
| 123 | + }) |
| 124 | + |
| 125 | + cluster.get('foo') |
| 126 | + }) |
| 127 | +}) |
0 commit comments