|
1 | 1 | # frozen_string_literal: true |
2 | 2 |
|
3 | 3 | require 'spec_helper' |
4 | | -require 'datadog/open_feature/evaluator' |
| 4 | +require 'datadog/open_feature/evaluation_engine' |
5 | 5 |
|
6 | | -RSpec.describe Datadog::OpenFeature::Evaluator do |
| 6 | +RSpec.describe Datadog::OpenFeature::EvaluationEngine do |
| 7 | + let(:evaluator) { described_class.new(telemetry, logger: logger) } |
7 | 8 | let(:telemetry) { instance_double(Datadog::Core::Telemetry::Component) } |
8 | | - let(:evaluator) { described_class.new(telemetry) } |
9 | 9 | let(:logger) { instance_double(Datadog::Core::Logger) } |
10 | 10 | let(:ufc) do |
11 | 11 | <<~JSON |
|
40 | 40 | JSON |
41 | 41 | end |
42 | 42 |
|
43 | | - before { allow(Datadog).to receive(:logger).and_return(logger) } |
44 | | - |
45 | 43 | describe '#fetch_value' do |
46 | 44 | let(:result) { evaluator.fetch_value(flag_key: 'test', expected_type: :string) } |
47 | 45 |
|
|
55 | 53 |
|
56 | 54 | context 'when binding evaluator returns error' do |
57 | 55 | before do |
58 | | - evaluator.ufc_json = ufc |
| 56 | + evaluator.configuration = ufc |
59 | 57 | evaluator.reconfigure! |
60 | 58 |
|
61 | | - allow_any_instance_of(described_class::Binding::Evaluator).to receive(:get_assignment) |
| 59 | + allow_any_instance_of(Datadog::OpenFeature::Binding::Evaluator).to receive(:get_assignment) |
62 | 60 | .and_return(error) |
63 | 61 | end |
64 | 62 |
|
|
73 | 71 |
|
74 | 72 | context 'when binding evaluator raises error' do |
75 | 73 | before do |
76 | | - evaluator.ufc_json = ufc |
| 74 | + evaluator.configuration = ufc |
77 | 75 | evaluator.reconfigure! |
78 | 76 |
|
79 | 77 | allow(telemetry).to receive(:report) |
80 | | - allow_any_instance_of(described_class::Binding::Evaluator).to receive(:get_assignment) |
| 78 | + allow_any_instance_of(Datadog::OpenFeature::Binding::Evaluator).to receive(:get_assignment) |
81 | 79 | .and_raise(error) |
82 | 80 | end |
83 | 81 |
|
|
92 | 90 |
|
93 | 91 | context 'when expected type not in the allowed list' do |
94 | 92 | before do |
95 | | - evaluator.ufc_json = ufc |
| 93 | + evaluator.configuration = ufc |
96 | 94 | evaluator.reconfigure! |
97 | 95 | end |
98 | 96 |
|
|
107 | 105 |
|
108 | 106 | context 'when binding evaluator returns resolution details' do |
109 | 107 | before do |
110 | | - evaluator.ufc_json = ufc |
| 108 | + evaluator.configuration = ufc |
111 | 109 | evaluator.reconfigure! |
112 | 110 | end |
113 | 111 |
|
|
118 | 116 | end |
119 | 117 |
|
120 | 118 | describe '#reconfigure!' do |
| 119 | + context 'when configuration is not yet present' do |
| 120 | + before { allow(logger).to receive(:debug) } |
| 121 | + |
| 122 | + it 'does nothing and logs the issue' do |
| 123 | + evaluator.reconfigure! |
| 124 | + |
| 125 | + expect(logger).to have_received(:debug).with(/OpenFeature: Configuration is not received, skip reconfiguration/) |
| 126 | + end |
| 127 | + end |
| 128 | + |
121 | 129 | context 'when binding initialization fails with exception' do |
122 | 130 | before do |
123 | | - evaluator.ufc_json = ufc |
| 131 | + evaluator.configuration = ufc |
124 | 132 | evaluator.reconfigure! |
125 | 133 |
|
126 | | - allow(described_class::Binding::Evaluator).to receive(:new).and_raise(error) |
| 134 | + allow(Datadog::OpenFeature::Binding::Evaluator).to receive(:new).and_raise(error) |
127 | 135 | end |
128 | 136 |
|
129 | 137 | let(:error) { StandardError.new('Ooops') } |
130 | 138 |
|
131 | 139 | it 'reports error to telemetry and logs it' do |
132 | 140 | expect(logger).to receive(:error).with(/Ooops/) |
133 | 141 | expect(telemetry).to receive(:report) |
134 | | - .with(error, description: match(/OpenFeature failed to reconfigure/)) |
| 142 | + .with(error, description: match(/OpenFeature: Failed to reconfigure/)) |
135 | 143 |
|
136 | | - evaluator.ufc_json = '{}' |
| 144 | + evaluator.configuration = '{}' |
137 | 145 | expect { evaluator.reconfigure! }.not_to raise_error |
138 | 146 | end |
139 | 147 |
|
140 | 148 | it 'persists previouly configured evaluator' do |
141 | 149 | allow(logger).to receive(:error) |
142 | 150 | allow(telemetry).to receive(:report) |
143 | 151 |
|
144 | | - evaluator.ufc_json = '{}' |
| 152 | + evaluator.configuration = '{}' |
145 | 153 | expect { evaluator.reconfigure! }.not_to change { |
146 | 154 | evaluator.fetch_value(flag_key: 'test', expected_type: :string).value |
147 | 155 | }.from('hello') |
|
150 | 158 |
|
151 | 159 | context 'when binding initialization succeeds' do |
152 | 160 | before do |
153 | | - evaluator.ufc_json = ufc |
| 161 | + evaluator.configuration = ufc |
154 | 162 | evaluator.reconfigure! |
155 | 163 | end |
156 | 164 |
|
|
188 | 196 | end |
189 | 197 |
|
190 | 198 | xit 'reconfigures binding evaluator with new flags configuration' do |
191 | | - expect { evaluator.ufc_json = new_ufc; evaluator.reconfigure!} |
| 199 | + expect { evaluator.configuration = new_ufc; evaluator.reconfigure!} |
192 | 200 | .to change { evaluator.fetch_value(flag_key: 'test', expected_type: :string).value } |
193 | 201 | .from('hello').to('goodbye') |
194 | 202 | end |
|
0 commit comments