|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require 'connection_pool' |
4 | | -require 'redis' |
5 | 4 |
|
6 | 5 | RSpec.describe Faulty::Storage::Redis do |
7 | 6 | subject(:storage) { described_class.new(**options.merge(client: client)) } |
8 | 7 |
|
9 | 8 | let(:options) { {} } |
10 | | - let(:client) { Redis.new(timeout: 1) } |
| 9 | + let(:client_options) { { timeout: 1 } } |
| 10 | + let(:client_class) do |
| 11 | + if ENV['REDIS_CLUSTER'] == 'true' |
| 12 | + require 'redis-clustering' |
| 13 | + Redis::Cluster |
| 14 | + else |
| 15 | + require 'redis' |
| 16 | + Redis |
| 17 | + end |
| 18 | + end |
| 19 | + let(:client) { client_class.new(**client_options) } |
11 | 20 | let(:circuit) { Faulty::Circuit.new('test', storage: storage) } |
12 | 21 |
|
13 | 22 | after { circuit&.reset! } |
14 | 23 |
|
15 | | - context 'with default options' do |
| 24 | + context 'with default options', unless: ENV['REDIS_CLUSTER'] == 'true' do |
16 | 25 | subject(:storage) { described_class.new } |
17 | 26 |
|
18 | 27 | it 'can add an entry' do |
|
32 | 41 | let(:pool_size) { 100 } |
33 | 42 |
|
34 | 43 | let(:client) do |
35 | | - ConnectionPool.new(size: pool_size, timeout: 1) { Redis.new(timeout: 1) } |
| 44 | + ConnectionPool.new(size: pool_size, timeout: 1) { client_class.new(**client_options) } |
36 | 45 | end |
37 | 46 |
|
38 | 47 | it 'adds an entry' do |
|
54 | 63 | end |
55 | 64 |
|
56 | 65 | context 'when Redis has high timeout' do |
57 | | - let(:client) { Redis.new(timeout: 5.0) } |
| 66 | + let(:client) { client_class.new(**client_options, timeout: 5.0) } |
58 | 67 |
|
59 | 68 | it 'prints timeout warning' do |
60 | 69 | timeouts = { connect_timeout: 5.0, read_timeout: 5.0, write_timeout: 5.0 } |
|
63 | 72 | end |
64 | 73 |
|
65 | 74 | context 'when Redis has high reconnect_attempts' do |
66 | | - let(:client) { Redis.new(timeout: 1, reconnect_attempts: 2) } |
| 75 | + let(:client) { client_class.new(**client_options, reconnect_attempts: 2) } |
67 | 76 |
|
68 | 77 | it 'prints reconnect_attempts warning' do |
69 | 78 | expect { storage }.to output(/Your setting is larger/).to_stderr |
|
72 | 81 |
|
73 | 82 | context 'when ConnectionPool has high timeout' do |
74 | 83 | let(:client) do |
75 | | - ConnectionPool.new(timeout: 6) { Redis.new(timeout: 1) } |
| 84 | + ConnectionPool.new(timeout: 6) { client_class.new(**client_options) } |
76 | 85 | end |
77 | 86 |
|
78 | 87 | it 'prints timeout warning' do |
|
82 | 91 |
|
83 | 92 | context 'when ConnectionPool Redis client has high timeout' do |
84 | 93 | let(:client) do |
85 | | - ConnectionPool.new(timeout: 1) { Redis.new(timeout: 7.0) } |
| 94 | + ConnectionPool.new(timeout: 1) { client_class.new(**client_options, timeout: 7.0) } |
86 | 95 | end |
87 | 96 |
|
88 | 97 | it 'prints Redis timeout warning' do |
|
106 | 115 | it 'sets opened_at to the maximum' do |
107 | 116 | Timecop.freeze |
108 | 117 | storage.open(circuit, Faulty.current_time) |
109 | | - client.del('faulty:circuit:test:opened_at') |
| 118 | + client.del('faulty:circuit:{test}:opened_at') |
110 | 119 | status = storage.status(circuit) |
111 | 120 | expect(status.opened_at).to eq(Faulty.current_time - storage.options.circuit_ttl) |
112 | 121 | end |
113 | 122 | end |
114 | 123 |
|
115 | 124 | context 'when history entries are integers and floats' do |
116 | 125 | it 'gets floats' do |
117 | | - client.lpush('faulty:circuit:test:entries', '1660865630:1') |
118 | | - client.lpush('faulty:circuit:test:entries', '1660865646.897674:1') |
| 126 | + client.lpush('faulty:circuit:{test}:entries', '1660865630:1') |
| 127 | + client.lpush('faulty:circuit:{test}:entries', '1660865646.897674:1') |
119 | 128 | expect(storage.history(circuit)).to eq([[1_660_865_630.0, true], [1_660_865_646.897674, true]]) |
120 | 129 | end |
121 | 130 | end |
|
0 commit comments