|
47 | 47 | providers[0, 2].each { |provider| expect(provider).to receive(:shutdown) } |
48 | 48 | configuration.set_provider(providers[0]) |
49 | 49 |
|
50 | | - allow(providers[0]).to receive(:shutdown).once { sleep 0.5 } |
| 50 | + allow(providers[0]).to(receive(:shutdown).once { sleep 0.5 }) |
51 | 51 | background { configuration.set_provider(providers[1]) } |
52 | 52 | background { configuration.set_provider(providers[2]) } |
53 | 53 | yield_to_background |
54 | 54 | expect(configuration.provider).to be(providers[2]) |
55 | 55 | end |
56 | 56 | end |
57 | 57 | end |
| 58 | + |
| 59 | + describe "#set_provider_and_wait" do |
| 60 | + context "when provider has a successful init method" do |
| 61 | + let(:provider) { OpenFeature::SDK::Provider::InMemoryProvider.new } |
| 62 | + |
| 63 | + it "waits for init to complete and sets the provider" do |
| 64 | + expect(provider).to receive(:init).once |
| 65 | + |
| 66 | + configuration.set_provider_and_wait(provider) |
| 67 | + |
| 68 | + expect(configuration.provider).to be(provider) |
| 69 | + end |
| 70 | + |
| 71 | + it "supports custom timeout" do |
| 72 | + expect(provider).to receive(:init).once |
| 73 | + |
| 74 | + configuration.set_provider_and_wait(provider, timeout: 60) |
| 75 | + |
| 76 | + expect(configuration.provider).to be(provider) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + context "when provider does not have an init method" do |
| 81 | + it "sets the provider without waiting" do |
| 82 | + provider = OpenFeature::SDK::Provider::NoOpProvider.new |
| 83 | + |
| 84 | + configuration.set_provider_and_wait(provider) |
| 85 | + |
| 86 | + expect(configuration.provider).to be(provider) |
| 87 | + end |
| 88 | + end |
| 89 | + |
| 90 | + context "when domain is given" do |
| 91 | + it "binds the provider to that domain" do |
| 92 | + provider = OpenFeature::SDK::Provider::InMemoryProvider.new |
| 93 | + expect(provider).to receive(:init).once |
| 94 | + |
| 95 | + configuration.set_provider_and_wait(provider, domain: "testing") |
| 96 | + |
| 97 | + expect(configuration.provider(domain: "testing")).to be(provider) |
| 98 | + end |
| 99 | + end |
| 100 | + |
| 101 | + context "when provider init raises an exception" do |
| 102 | + let(:provider) { OpenFeature::SDK::Provider::InMemoryProvider.new } |
| 103 | + let(:error_message) { "Database connection failed" } |
| 104 | + |
| 105 | + before do |
| 106 | + allow(provider).to receive(:init).and_raise(StandardError.new(error_message)) |
| 107 | + end |
| 108 | + |
| 109 | + it "raises ProviderInitializationError" do |
| 110 | + expect do |
| 111 | + configuration.set_provider_and_wait(provider) |
| 112 | + end.to raise_error(OpenFeature::SDK::ProviderInitializationError) do |error| |
| 113 | + expect(error.message).to include("Provider initialization failed") |
| 114 | + expect(error.message).to include(error_message) |
| 115 | + expect(error.provider).to be(provider) |
| 116 | + expect(error.original_error).to be_a(StandardError) |
| 117 | + expect(error.original_error.message).to eq(error_message) |
| 118 | + expect(error.error_code).to eq(OpenFeature::SDK::Provider::ErrorCode::PROVIDER_FATAL) |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | + it "does not set the provider when init fails" do |
| 123 | + old_provider = configuration.provider |
| 124 | + |
| 125 | + expect do |
| 126 | + configuration.set_provider_and_wait(provider) |
| 127 | + end.to raise_error(OpenFeature::SDK::ProviderInitializationError) |
| 128 | + |
| 129 | + expect(configuration.provider).to be(old_provider) |
| 130 | + end |
| 131 | + end |
| 132 | + |
| 133 | + context "when provider init times out" do |
| 134 | + let(:provider) { OpenFeature::SDK::Provider::InMemoryProvider.new } |
| 135 | + |
| 136 | + before do |
| 137 | + allow(provider).to receive(:init) do |
| 138 | + sleep 2 # Simulate slow initialization |
| 139 | + end |
| 140 | + end |
| 141 | + |
| 142 | + it "raises ProviderInitializationError after timeout" do |
| 143 | + expect do |
| 144 | + configuration.set_provider_and_wait(provider, timeout: 0.1) |
| 145 | + end.to raise_error(OpenFeature::SDK::ProviderInitializationError) do |error| |
| 146 | + expect(error.message).to include("Provider initialization timed out after 0.1 seconds") |
| 147 | + expect(error.provider).to be(provider) |
| 148 | + expect(error.original_error).to be_a(Timeout::Error) |
| 149 | + expect(error.error_code).to eq(OpenFeature::SDK::Provider::ErrorCode::PROVIDER_FATAL) |
| 150 | + end |
| 151 | + end |
| 152 | + |
| 153 | + it "does not set the provider when init times out" do |
| 154 | + old_provider = configuration.provider |
| 155 | + |
| 156 | + expect do |
| 157 | + configuration.set_provider_and_wait(provider, timeout: 0.1) |
| 158 | + end.to raise_error(OpenFeature::SDK::ProviderInitializationError) |
| 159 | + |
| 160 | + expect(configuration.provider).to be(old_provider) |
| 161 | + end |
| 162 | + end |
| 163 | + |
| 164 | + context "when shutting down the old provider fails" do |
| 165 | + let(:old_provider) { OpenFeature::SDK::Provider::InMemoryProvider.new } |
| 166 | + let(:new_provider) { OpenFeature::SDK::Provider::InMemoryProvider.new } |
| 167 | + |
| 168 | + before do |
| 169 | + # Set up initial provider |
| 170 | + configuration.set_provider(old_provider) |
| 171 | + allow(old_provider).to receive(:shutdown).and_raise(StandardError.new("Shutdown failed")) |
| 172 | + allow(new_provider).to receive(:init) |
| 173 | + end |
| 174 | + |
| 175 | + it "continues with setting the new provider" do |
| 176 | + # Should not raise an exception even if shutdown fails |
| 177 | + configuration.set_provider_and_wait(new_provider) |
| 178 | + |
| 179 | + expect(configuration.provider).to be(new_provider) |
| 180 | + end |
| 181 | + end |
| 182 | + |
| 183 | + context "when the provider is set concurrently" do |
| 184 | + let(:providers) { (0..2).map { OpenFeature::SDK::Provider::InMemoryProvider.new } } |
| 185 | + |
| 186 | + it "handles concurrent calls safely" do |
| 187 | + providers.each { |provider| expect(provider).to receive(:init).once } |
| 188 | + # First two providers should be shut down |
| 189 | + expect(providers[0]).to receive(:shutdown).once |
| 190 | + expect(providers[1]).to receive(:shutdown).once |
| 191 | + |
| 192 | + configuration.set_provider_and_wait(providers[0]) |
| 193 | + |
| 194 | + # Simulate slow initialization for concurrent testing |
| 195 | + allow(providers[0]).to receive(:shutdown) { sleep 0.1 } |
| 196 | + |
| 197 | + background { configuration.set_provider_and_wait(providers[1]) } |
| 198 | + background { configuration.set_provider_and_wait(providers[2]) } |
| 199 | + yield_to_background |
| 200 | + |
| 201 | + # The last provider should be set |
| 202 | + expect(configuration.provider).to be(providers[2]) |
| 203 | + end |
| 204 | + end |
| 205 | + |
| 206 | + context "when handling complex initialization scenarios" do |
| 207 | + let(:provider) { OpenFeature::SDK::Provider::InMemoryProvider.new } |
| 208 | + |
| 209 | + it "handles provider that responds_to init but init is nil" do |
| 210 | + allow(provider).to receive(:respond_to?).with(:init).and_return(true) |
| 211 | + allow(provider).to receive(:init).and_return(nil) |
| 212 | + |
| 213 | + configuration.set_provider_and_wait(provider) |
| 214 | + |
| 215 | + expect(configuration.provider).to be(provider) |
| 216 | + end |
| 217 | + end |
| 218 | + end |
58 | 219 | end |
0 commit comments