diff --git a/README.md b/README.md index 1ab32f92..3573fa7b 100644 --- a/README.md +++ b/README.md @@ -90,8 +90,8 @@ Argument | Description | Required? | Type | Default Value| `project_name` | Name of the project to retrieve features from. If not set, all feature flags will be retrieved. | N | String | nil | `refresh_interval` | How often the unleash client should check with the server for configuration changes. | N | Integer | 15 | `metrics_interval` | How often the unleash client should send metrics to server. | N | Integer | 60 | -`disable_client` | Disables all communication with the Unleash server, effectively taking it *offline*. If set, `is_enabled?` will always answer with the `default_value` and configuration validation is skipped. Defeats the entire purpose of using unleash, but can be useful in when running tests. | N | Boolean | `false` | -`disable_metrics` | Disables sending metrics to Unleash server. | N | Boolean | `false` | +`disable_client` | Disables all communication with the Unleash server, effectively taking it *offline*. If set, `is_enabled?` will always answer with the `default_value` and configuration validation is skipped. Will also forcefully set `disable_metrics` to `true`. Defeats the entire purpose of using unleash, except when running tests. | N | Boolean | `false` | +`disable_metrics` | Disables sending metrics to Unleash server. If the `disable_client` option is set to `true`, then this option will also be set to `true`, regardless of the value provided. | N | Boolean | `false` | `custom_http_headers` | Custom headers to send to Unleash. As of Unleash v4.0.0, the `Authorization` header is required. For example: `{'Authorization': ''}` | N | Hash/Proc | {} | `timeout` | How long to wait for the connection to be established or wait in reading state (open_timeout/read_timeout) | N | Integer | 30 | `retry_limit` | How many consecutive failures in connecting to the Unleash server are allowed before giving up. The default is to retry indefinitely. | N | Float::INFINITY | 5 | diff --git a/lib/unleash/client.rb b/lib/unleash/client.rb index 62b75f35..5c43fdc7 100644 --- a/lib/unleash/client.rb +++ b/lib/unleash/client.rb @@ -21,6 +21,8 @@ def initialize(*opts) Unleash.toggle_fetcher = Unleash::ToggleFetcher.new if Unleash.configuration.disable_client Unleash.logger.warn "Unleash::Client is disabled! Will only return default (or bootstrapped if available) results!" + Unleash.logger.warn "Unleash::Client is disabled! Metrics and MetricsReporter are also disabled!" + Unleash.configuration.disable_metrics = true return end diff --git a/spec/unleash/client_spec.rb b/spec/unleash/client_spec.rb index 9fb8108d..5c5739cf 100644 --- a/spec/unleash/client_spec.rb +++ b/spec/unleash/client_spec.rb @@ -1,4 +1,9 @@ RSpec.describe Unleash::Client do + after do + WebMock.reset! + File.delete(Unleash.configuration.backup_file) if File.exist?(Unleash.configuration.backup_file) + end + it "Uses custom http headers when initializing client" do WebMock.stub_request(:post, "http://test-url/client/register") .with( @@ -212,6 +217,10 @@ expect(WebMock).not_to have_requested(:get, 'http://test-url/') expect(WebMock).not_to have_requested(:post, 'http://test-url/client/register') expect(WebMock).not_to have_requested(:get, 'http://test-url/client/features') + + # No requests at all: + expect(WebMock).not_to have_requested(:get, /.*/) + expect(WebMock).not_to have_requested(:post, /.*/) end it "should not fail if we are provided no toggles from the unleash server" do @@ -262,6 +271,30 @@ expect(WebMock).not_to have_requested(:post, 'http://test-url/client/metrics') end + it "should forcefully disable metrics if the client is disabled" do + Unleash.configure do |config| + config.url = 'http://test-url/' + config.app_name = 'my-test-app' + config.instance_id = 'rspec/test' + config.disable_client = true + config.disable_metrics = false + config.custom_http_headers = { 'X-API-KEY' => '123' } + end + + unleash_client = Unleash::Client.new + + expect( + unleash_client.is_enabled?('any_feature', {}, true) + ).to eq(true) + + expect(Unleash.configuration.disable_client).to be true + expect(Unleash.configuration.disable_metrics).to be true + + # No requests at all: + expect(WebMock).not_to have_requested(:get, /.*/) + expect(WebMock).not_to have_requested(:post, /.*/) + end + it "should return default results via block or param if running with disable_client" do Unleash.configure do |config| config.disable_client = true