|
1 | 1 | # frozen_string_literal: true |
2 | | - |
3 | 2 | require 'spec_helper' |
4 | 3 |
|
5 | 4 | require_relative 'shared_mocks.rb' |
|
8 | 7 | include_context "shared mocks" |
9 | 8 |
|
10 | 9 | let(:api_environment_response) { File.read('spec/sdk/fixtures/environment.json') } |
11 | | - let(:environemnt_response) { OpenStruct.new(body: JSON.parse(api_environment_response, symbolize_names: true)) } |
| 10 | + let(:environment_response) { OpenStruct.new(body: JSON.parse(api_environment_response, symbolize_names: true)) } |
12 | 11 | let(:refresh_interval_seconds) { 0.01 } |
13 | 12 | let(:delay_time) { 0.045 } |
14 | 13 |
|
15 | 14 | before(:each) do |
16 | 15 | allow(Thread).to receive(:new).and_call_original |
17 | 16 | allow(Thread).to receive(:kill).and_call_original |
18 | | - allow(mock_api_client).to receive(:get).with('environment-document/').and_return(environemnt_response) |
| 17 | + allow(mock_api_client).to receive(:get).with('environment-document/').and_return(environment_response) |
19 | 18 | allow(double(Flagsmith::Config)).to receive(:environment_url).and_return("environment-document/") |
20 | 19 | end |
21 | 20 |
|
22 | | - subject { Flagsmith::EnvironmentDataPollingManager.new(flagsmith, refresh_interval_seconds) } |
| 21 | + subject { Flagsmith::EnvironmentDataPollingManager.new(flagsmith, refresh_interval_seconds, 10) } |
23 | 22 |
|
24 | 23 | it 'test_polling_manager_calls_update_environment_on_start' do |
25 | 24 | times = (delay_time / refresh_interval_seconds).to_i |
|
29 | 28 | subject.stop |
30 | 29 | end |
31 | 30 | end |
| 31 | + |
| 32 | + |
| 33 | +class FakeFlagsmith |
| 34 | + attr_accessor :raise_error, :config |
| 35 | + |
| 36 | + def initialize config |
| 37 | + @config = config |
| 38 | + end |
| 39 | + |
| 40 | + def update_environment |
| 41 | + if @raise_error |
| 42 | + raise StandardError, "Some networking issue" |
| 43 | + else |
| 44 | + # Perform update logic |
| 45 | + end |
| 46 | + end |
| 47 | +end |
| 48 | + |
| 49 | +RSpec.describe Flagsmith::EnvironmentDataPollingManager do |
| 50 | + include_context "shared mocks" |
| 51 | + |
| 52 | + let(:refresh_interval_seconds) { 0.01 } |
| 53 | + let(:delay_time) { 0.045 } |
| 54 | + let(:update_failures_limit) { 5 } |
| 55 | + let(:fake_flagsmith) { FakeFlagsmith.new mock_config } |
| 56 | + |
| 57 | + before :each do |
| 58 | + allow(mock_config).to receive(:logger).and_return(Logger.new($stdout)) |
| 59 | + end |
| 60 | + |
| 61 | + subject { Flagsmith::EnvironmentDataPollingManager.new(fake_flagsmith, refresh_interval_seconds, update_failures_limit) } |
| 62 | + |
| 63 | + it "operates under an error prone environment" do |
| 64 | + fake_flagsmith.raise_error = true |
| 65 | + |
| 66 | + # Four invocations are processed without the error bubbling up. |
| 67 | + times = (delay_time / refresh_interval_seconds).to_i |
| 68 | + subject.start |
| 69 | + sleep delay_time |
| 70 | + # Show that the failures are recorded without raising. |
| 71 | + expect(subject.failures_since_last_update).to eq(4) |
| 72 | + |
| 73 | + # Now set flagsmith to respond as normal. |
| 74 | + fake_flagsmith.raise_error = false |
| 75 | + sleep delay_time |
| 76 | + |
| 77 | + # Now the exception count is back to zero. |
| 78 | + expect(subject.failures_since_last_update).to eq(0) |
| 79 | + subject.stop |
| 80 | + end |
| 81 | +end |
0 commit comments