Skip to content

Commit

Permalink
Merge d225ae6 into 3165da3
Browse files Browse the repository at this point in the history
  • Loading branch information
rarruda committed May 2, 2023
2 parents 3165da3 + d225ae6 commit c7c1747
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -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': '<API token>'}` | 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 |
Expand Down
2 changes: 2 additions & 0 deletions lib/unleash/client.rb
Expand Up @@ -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

Expand Down
33 changes: 33 additions & 0 deletions 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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c7c1747

Please sign in to comment.