|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +# Released under the MIT License. |
| 4 | +# Copyright, 2024-2025, by Samuel Williams. |
| 5 | + |
| 6 | +require "async/redis/sentinel_client" |
| 7 | +require "sus/fixtures/async" |
| 8 | +require "openssl" |
| 9 | + |
| 10 | +describe Async::Redis::SentinelClient do |
| 11 | + include Sus::Fixtures::Async::ReactorContext |
| 12 | + |
| 13 | + let(:sentinels) {[ |
| 14 | + Async::Redis::Endpoint.parse("redis://localhost:26379") |
| 15 | + ]} |
| 16 | + |
| 17 | + with "initialization" do |
| 18 | + it "can be created with basic parameters" do |
| 19 | + client = subject.new(sentinels) |
| 20 | + expect(client.master_name).to be == "mymaster" |
| 21 | + expect(client.role).to be == :master |
| 22 | + end |
| 23 | + |
| 24 | + it "accepts master_options parameter" do |
| 25 | + master_options = {database: 1, timeout: 5} |
| 26 | + client = subject.new(sentinels, master_options: master_options) |
| 27 | + |
| 28 | + # Test that the options are used by checking the instance variables |
| 29 | + expect(client.instance_variable_get(:@master_options)).to be == master_options |
| 30 | + end |
| 31 | + |
| 32 | + it "accepts slave_options parameter" do |
| 33 | + master_options = {database: 1} |
| 34 | + slave_options = {database: 2, timeout: 10} |
| 35 | + client = subject.new(sentinels, master_options: master_options, slave_options: slave_options) |
| 36 | + |
| 37 | + expect(client.instance_variable_get(:@slave_options)).to be == slave_options |
| 38 | + end |
| 39 | + |
| 40 | + it "uses master_options as fallback for slaves when slave_options not provided" do |
| 41 | + master_options = {database: 1, timeout: 5} |
| 42 | + client = subject.new(sentinels, master_options: master_options) |
| 43 | + |
| 44 | + expect(client.instance_variable_get(:@slave_options)).to be == master_options |
| 45 | + end |
| 46 | + |
| 47 | + it "handles empty options gracefully" do |
| 48 | + client = subject.new(sentinels) |
| 49 | + |
| 50 | + expect(client.instance_variable_get(:@master_options)).to be == {} |
| 51 | + expect(client.instance_variable_get(:@slave_options)).to be == {} |
| 52 | + end |
| 53 | + end |
| 54 | + |
| 55 | + with "endpoint options by role" do |
| 56 | + let(:master_options) {{database: 1, timeout: 5}} |
| 57 | + let(:slave_options) {{database: 2, timeout: 10}} |
| 58 | + let(:client) {subject.new(sentinels, master_options: master_options, slave_options: slave_options)} |
| 59 | + |
| 60 | + it "stores master options correctly" do |
| 61 | + expect(client.instance_variable_get(:@master_options)).to be == master_options |
| 62 | + end |
| 63 | + |
| 64 | + it "stores slave options correctly" do |
| 65 | + expect(client.instance_variable_get(:@slave_options)).to be == slave_options |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + with "role-specific scheme handling" do |
| 70 | + it "creates correct endpoint scheme for master with SSL" do |
| 71 | + ssl_context = OpenSSL::SSL::SSLContext.new |
| 72 | + master_options = {ssl_context: ssl_context} |
| 73 | + |
| 74 | + endpoint = Async::Redis::Endpoint.for(nil, "localhost", **master_options) |
| 75 | + expect(endpoint.scheme).to be == "rediss" |
| 76 | + end |
| 77 | + |
| 78 | + it "creates different schemes for master vs slave when options differ" do |
| 79 | + ssl_context = OpenSSL::SSL::SSLContext.new |
| 80 | + master_options = {ssl_context: ssl_context} # SSL enabled |
| 81 | + slave_options = {database: 1} # No SSL |
| 82 | + |
| 83 | + master_endpoint = Async::Redis::Endpoint.for(nil, "localhost", **master_options) |
| 84 | + slave_endpoint = Async::Redis::Endpoint.for(nil, "localhost", **slave_options) |
| 85 | + |
| 86 | + expect(master_endpoint.scheme).to be == "rediss" |
| 87 | + expect(slave_endpoint.scheme).to be == "redis" |
| 88 | + end |
| 89 | + end |
| 90 | +end |
0 commit comments