Skip to content

Commit 425bcaf

Browse files
committed
Add spec for testing the polling manager
1 parent 7d1a5db commit 425bcaf

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

lib/flagsmith/sdk/pooling_manager.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ module Flagsmith
77
class EnvironmentDataPollingManager
88
include Flagsmith::SDK::Intervals
99

10+
attr_reader :failures_since_last_update
11+
1012
def initialize(main, refresh_interval_seconds, polling_manager_failure_limit)
1113
@main = main
1214
@refresh_interval_seconds = refresh_interval_seconds

spec/sdk/datapooling_manager_spec.rb

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
# frozen_string_literal: true
2-
32
require 'spec_helper'
43

54
require_relative 'shared_mocks.rb'
@@ -8,14 +7,14 @@
87
include_context "shared mocks"
98

109
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)) }
1211
let(:refresh_interval_seconds) { 0.01 }
1312
let(:delay_time) { 0.045 }
1413

1514
before(:each) do
1615
allow(Thread).to receive(:new).and_call_original
1716
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)
1918
allow(double(Flagsmith::Config)).to receive(:environment_url).and_return("environment-document/")
2019
end
2120

@@ -29,3 +28,54 @@
2928
subject.stop
3029
end
3130
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

Comments
 (0)