|
| 1 | +require 'spec_helper' |
| 2 | +require 'faraday' |
| 3 | + |
| 4 | +RSpec.describe Flagsmith::RealtimeClient do |
| 5 | + let(:mock_logger) { double('Logger', warn: nil, info: nil, error: nil) } |
| 6 | + let(:mock_config) do |
| 7 | + double('Config', |
| 8 | + realtime_api_url: 'https://example.com/', |
| 9 | + environment_key: 'test-environment', |
| 10 | + logger: mock_logger) |
| 11 | + end |
| 12 | + let(:mock_environment) { double('Environment', |
| 13 | + api_key: 'some_api_key' )} |
| 14 | + let(:mock_main) { double('Main', |
| 15 | + update_environment: nil, |
| 16 | + environment: mock_environment, |
| 17 | + ) } |
| 18 | + let(:realtime_client) { described_class.new(mock_config) } |
| 19 | + let(:sse_response) do |
| 20 | + <<~SSE |
| 21 | + data: {"updated_at": 1} |
| 22 | +
|
| 23 | + data: {"updated_at": 2} |
| 24 | + SSE |
| 25 | + end |
| 26 | + let(:retry_interval) { 0.01 } |
| 27 | + |
| 28 | + before(:each) do |
| 29 | + allow(Faraday).to receive(:new).and_return(double('Faraday::Connection', get: double('Response', body: sse_response))) |
| 30 | + allow(Thread).to receive(:new).and_yield |
| 31 | + end |
| 32 | + |
| 33 | + describe '#listen' do |
| 34 | + after { realtime_client.running = false } |
| 35 | + |
| 36 | + it 'parses SSE data and calls update_environment when updated_at increases' do |
| 37 | + expect(mock_main).to receive(:update_environment).twice |
| 38 | + realtime_client.listen(mock_main, retry_interval: retry_interval, remaining_attempts: 3) |
| 39 | + end |
| 40 | + |
| 41 | + it 'logs retries and continues on connection failure' do |
| 42 | + allow(Faraday).to receive(:new).and_raise(Faraday::ConnectionFailed.new('Connection failed')) |
| 43 | + |
| 44 | + expect(mock_logger).to receive(:warn).with(/Connection failed/).at_least(:once) |
| 45 | + realtime_client.listen(mock_main, retry_interval: retry_interval, remaining_attempts: 3) |
| 46 | + end |
| 47 | + |
| 48 | + it 'handles and logs unexpected errors gracefully' do |
| 49 | + allow(Faraday).to receive(:new).and_raise(StandardError.new('Unexpected error')) |
| 50 | + |
| 51 | + expect(mock_logger).to receive(:error).with(/Unexpected error/).at_least(:once) |
| 52 | + realtime_client.listen(mock_main, retry_interval: retry_interval, remaining_attempts: 3) |
| 53 | + end |
| 54 | + |
| 55 | + end |
| 56 | +end |
| 57 | + |
| 58 | +RSpec.describe Flagsmith::Client do |
| 59 | + describe '#initialize' do |
| 60 | + before do |
| 61 | + # Mock the methods to avoid initialization interferring. |
| 62 | + allow_any_instance_of(Flagsmith::Client).to receive(:api_client) |
| 63 | + allow_any_instance_of(Flagsmith::Client).to receive(:analytics_processor) |
| 64 | + allow_any_instance_of(Flagsmith::Client).to receive(:environment_data_polling_manager) |
| 65 | + allow_any_instance_of(Flagsmith::Client).to receive(:engine) |
| 66 | + allow_any_instance_of(Flagsmith::Client).to receive(:load_offline_handler) |
| 67 | + end |
| 68 | + |
| 69 | + context 'when realtime_mode is true and local_evaluation is false' do |
| 70 | + it 'raises a Flagsmith::ClientError' do |
| 71 | + config = double( |
| 72 | + 'Config', |
| 73 | + realtime_mode?: true, |
| 74 | + local_evaluation?: false, |
| 75 | + offline_mode?: false, |
| 76 | + offline_handler: nil, |
| 77 | + ) |
| 78 | + allow(Flagsmith::Config).to receive(:new).and_return(config) |
| 79 | + |
| 80 | + expect { |
| 81 | + Flagsmith::Client.new(config) |
| 82 | + }.to raise_error(Flagsmith::ClientError, 'The enable_realtime_updates config param requires a matching enable_local_evaluation param.') |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + context 'when realtime_mode is false or local_evaluation is true' do |
| 87 | + it 'does not raise an exception' do |
| 88 | + config = double( |
| 89 | + 'Config', |
| 90 | + realtime_mode?: false, |
| 91 | + local_evaluation?: true, |
| 92 | + offline_mode?: false, |
| 93 | + offline_handler: nil, |
| 94 | + ) |
| 95 | + allow(Flagsmith::Config).to receive(:new).and_return(config) |
| 96 | + |
| 97 | + expect { |
| 98 | + Flagsmith::Client.new(config) |
| 99 | + }.not_to raise_error |
| 100 | + end |
| 101 | + end |
| 102 | + end |
| 103 | +end |
0 commit comments