|
| 1 | +RSpec.describe Unleash::StreamingClientExecutor do |
| 2 | + subject(:streaming_executor) { Unleash::StreamingClientExecutor.new(executor_name, engine) } |
| 3 | + |
| 4 | + unless RUBY_ENGINE == 'jruby' |
| 5 | + before do |
| 6 | + Unleash.configure do |config| |
| 7 | + config.url = 'http://streaming-test-url/' |
| 8 | + config.app_name = 'streaming-test-app' |
| 9 | + config.instance_id = 'rspec/streaming' |
| 10 | + config.disable_metrics = true |
| 11 | + config.experimental_mode = { type: 'streaming' } |
| 12 | + end |
| 13 | + |
| 14 | + WebMock.stub_request(:post, "http://streaming-test-url/client/register") |
| 15 | + .to_return(status: 200, body: "", headers: {}) |
| 16 | + |
| 17 | + Unleash.logger = Unleash.configuration.logger |
| 18 | + end |
| 19 | + |
| 20 | + after do |
| 21 | + WebMock.reset! |
| 22 | + File.delete(Unleash.configuration.backup_file) if File.exist?(Unleash.configuration.backup_file) |
| 23 | + |
| 24 | + # Reset configuration to prevent interference with other tests |
| 25 | + Unleash.configuration.bootstrap_config = nil |
| 26 | + Unleash.configuration.experimental_mode = nil |
| 27 | + Unleash.configuration.disable_metrics = false |
| 28 | + end |
| 29 | + |
| 30 | + describe '.new' do |
| 31 | + let(:engine) { YggdrasilEngine.new } |
| 32 | + let(:executor_name) { 'streaming_client_executor_spec' } |
| 33 | + |
| 34 | + context 'when there are problems connecting to streaming endpoint' do |
| 35 | + let(:backup_toggles) do |
| 36 | + { |
| 37 | + version: 1, |
| 38 | + features: [ |
| 39 | + { |
| 40 | + name: "backup-feature", |
| 41 | + description: "Feature from backup", |
| 42 | + enabled: true, |
| 43 | + strategies: [{ |
| 44 | + name: "default" |
| 45 | + }] |
| 46 | + } |
| 47 | + ] |
| 48 | + } |
| 49 | + end |
| 50 | + |
| 51 | + before do |
| 52 | + backup_file = Unleash.configuration.backup_file |
| 53 | + |
| 54 | + # manually create a stub cache on disk, so we can test that we read it correctly later. |
| 55 | + File.open(backup_file, "w") do |file| |
| 56 | + file.write(backup_toggles.to_json) |
| 57 | + end |
| 58 | + |
| 59 | + WebMock.stub_request(:get, "http://streaming-test-url/client/streaming") |
| 60 | + .to_return(status: 500, body: "Internal Server Error", headers: {}) |
| 61 | + |
| 62 | + streaming_executor |
| 63 | + end |
| 64 | + |
| 65 | + it 'reads the backup file for values' do |
| 66 | + enabled = engine.enabled?('backup-feature', {}) |
| 67 | + expect(enabled).to eq(true) |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + context 'when bootstrap is configured' do |
| 72 | + let(:bootstrap_data) do |
| 73 | + { |
| 74 | + version: 1, |
| 75 | + features: [ |
| 76 | + { |
| 77 | + name: "bootstrap-feature", |
| 78 | + enabled: true, |
| 79 | + strategies: [{ name: "default" }] |
| 80 | + } |
| 81 | + ] |
| 82 | + } |
| 83 | + end |
| 84 | + |
| 85 | + let(:bootstrap_config) do |
| 86 | + Unleash::Bootstrap::Configuration.new({ |
| 87 | + 'data' => bootstrap_data.to_json |
| 88 | + }) |
| 89 | + end |
| 90 | + |
| 91 | + before do |
| 92 | + Unleash.configuration.bootstrap_config = bootstrap_config |
| 93 | + |
| 94 | + WebMock.stub_request(:get, "http://streaming-test-url/client/streaming") |
| 95 | + .to_return(status: 200, body: "", headers: {}) |
| 96 | + |
| 97 | + streaming_executor |
| 98 | + end |
| 99 | + |
| 100 | + after do |
| 101 | + Unleash.configuration.bootstrap_config = nil |
| 102 | + end |
| 103 | + |
| 104 | + it 'uses bootstrap data on initialization' do |
| 105 | + enabled = engine.enabled?('bootstrap-feature', {}) |
| 106 | + expect(enabled).to eq(true) |
| 107 | + end |
| 108 | + |
| 109 | + it 'clears bootstrap config after use' do |
| 110 | + expect(Unleash.configuration.bootstrap_config).to be_nil |
| 111 | + end |
| 112 | + end |
| 113 | + |
| 114 | + context 'when bootstrap fails and backup file exists' do |
| 115 | + let(:invalid_bootstrap_config) do |
| 116 | + Unleash::Bootstrap::Configuration.new({ |
| 117 | + 'data' => 'invalid json' |
| 118 | + }) |
| 119 | + end |
| 120 | + |
| 121 | + let(:fallback_toggles) do |
| 122 | + { |
| 123 | + version: 1, |
| 124 | + features: [ |
| 125 | + { |
| 126 | + name: "fallback-feature", |
| 127 | + enabled: true, |
| 128 | + strategies: [{ name: "default" }] |
| 129 | + } |
| 130 | + ] |
| 131 | + } |
| 132 | + end |
| 133 | + |
| 134 | + before do |
| 135 | + backup_file = Unleash.configuration.backup_file |
| 136 | + |
| 137 | + File.open(backup_file, "w") do |file| |
| 138 | + file.write(fallback_toggles.to_json) |
| 139 | + end |
| 140 | + |
| 141 | + Unleash.configuration.bootstrap_config = invalid_bootstrap_config |
| 142 | + |
| 143 | + WebMock.stub_request(:get, "http://streaming-test-url/client/streaming") |
| 144 | + .to_return(status: 500, body: "", headers: {}) |
| 145 | + |
| 146 | + streaming_executor |
| 147 | + end |
| 148 | + |
| 149 | + after do |
| 150 | + Unleash.configuration.bootstrap_config = nil |
| 151 | + end |
| 152 | + |
| 153 | + it 'falls back to reading backup file when bootstrap fails' do |
| 154 | + enabled = engine.enabled?('fallback-feature', {}) |
| 155 | + expect(enabled).to eq(true) |
| 156 | + end |
| 157 | + end |
| 158 | + end |
| 159 | + end |
| 160 | +end |
0 commit comments