Skip to content

Commit

Permalink
Merge 94c1913 into 9ce62b5
Browse files Browse the repository at this point in the history
  • Loading branch information
rarruda committed Jan 4, 2019
2 parents 9ce62b5 + 94c1913 commit 4190d7f
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
@@ -1,12 +1,12 @@
sudo: false
language: ruby
rvm:
- 2.2
- 2.3
- 2.4
- 2.5
- 2.6
- jruby
before_install:
- gem install bundler -v 1.14.6
- gem install bundler -v 2.0.1
# install client spec from official repository:
- git clone --depth 5 https://github.com/Unleash/client-specification.git client-specification
10 changes: 10 additions & 0 deletions README.md
Expand Up @@ -52,6 +52,7 @@ Argument | Description | Required? | Type | Default Value|
`instance_id` | Identifier for the running instance of program. Important so you can trace back to where metrics are being collected from. **Highly recommended be be set.** | N | String | random UUID |
`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 | 10 |
`disable_client` | Disables all communication with the Unleash server. 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 | F |
`disable_metrics` | Disables sending metrics to Unleash server. | N | Boolean | F |
`custom_http_headers` | Custom headers to send to Unleash. | N | Hash | {} |
`timeout` | How long to wait for the connection to be established or wait in reading state (open_timeout/read_timeout) | N | Integer | 30 |
Expand Down Expand Up @@ -93,6 +94,7 @@ Unleash.configure do |config|
config.url = 'http://unleash.herokuapp.com/api'
config.app_name = Rails.application.class.parent.to_s
# config.instance_id = "#{Socket.gethostname}"
config.logger = Rails.logger
end

UNLEASH = Unleash::Client.new
Expand Down Expand Up @@ -158,6 +160,14 @@ if Rails.configuration.unleash.is_enabled? "AwesomeFeature", @unleash_context
end
```

If the feature is not found in the server, it will by default return false. However you can override that by setting the default return value to `true`:

```ruby
if UNLEASH.is_enabled? "AwesomeFeature", @unleash_context, true
puts "AwesomeFeature is enabled by default"
end
```

## Local test client

```
Expand Down
27 changes: 18 additions & 9 deletions lib/unleash/client.rb
Expand Up @@ -15,22 +15,31 @@ def initialize(*opts)
Unleash.logger = Unleash.configuration.logger
Unleash.logger.level = Unleash.configuration.log_level

Unleash.toggle_fetcher = Unleash::ToggleFetcher.new
register

unless Unleash.configuration.disable_metrics
Unleash.toggle_metrics = Unleash::Metrics.new
Unleash.reporter = Unleash::MetricsReporter.new
scheduledExecutor = Unleash::ScheduledExecutor.new('MetricsReporter', Unleash.configuration.metrics_interval)
scheduledExecutor.run do
Unleash.reporter.send
unless Unleash.configuration.disable_client
Unleash.toggle_fetcher = Unleash::ToggleFetcher.new
register

unless Unleash.configuration.disable_metrics
Unleash.toggle_metrics = Unleash::Metrics.new
Unleash.reporter = Unleash::MetricsReporter.new
scheduledExecutor = Unleash::ScheduledExecutor.new('MetricsReporter', Unleash.configuration.metrics_interval)
scheduledExecutor.run do
Unleash.reporter.send
end
end
else
Unleash.logger.warn "Unleash::Client is disabled! Will only return default results!"
end
end

def is_enabled?(feature, context = nil, default_value = false)
Unleash.logger.debug "Unleash::Client.is_enabled? feature: #{feature} with context #{context}"

if Unleash.configuration.disable_client
Unleash.logger.warn "unleash_client is disabled! Always returning #{default_value} for feature #{feature}!"
return default_value
end

toggle_as_hash = Unleash.toggles.select{ |toggle| toggle['name'] == feature }.first if Unleash.toggles

if toggle_as_hash.nil?
Expand Down
6 changes: 5 additions & 1 deletion lib/unleash/configuration.rb
Expand Up @@ -5,6 +5,7 @@ module Unleash
class Configuration
attr_accessor :url, :app_name, :instance_id,
:custom_http_headers,
:disable_client,
:disable_metrics, :timeout, :retry_limit,
:refresh_interval, :metrics_interval,
:backup_file, :logger, :log_level
Expand All @@ -19,6 +20,7 @@ def initialize(opts = {})
else
raise ArgumentError, "custom_http_headers must be a hash."
end
self.disable_client = opts[:disable_client] || false
self.disable_metrics = opts[:disable_metrics] || false
self.refresh_interval = opts[:refresh_interval] || 15
self.metrics_interval = opts[:metrics_interval] || 10
Expand All @@ -28,7 +30,7 @@ def initialize(opts = {})
self.backup_file = opts[:backup_file] || nil

self.logger = opts[:logger] || Logger.new(STDOUT)
self.log_level = opts[:log_level] || Logger::ERROR
self.log_level = opts[:log_level] || Logger::WARN


if opts[:logger].nil?
Expand All @@ -47,6 +49,8 @@ def metrics_interval_in_millis
end

def validate!
return if self.disable_client

if self.app_name.nil? or self.url.nil?
raise ArgumentError, "URL and app_name are required parameters."
end
Expand Down
58 changes: 40 additions & 18 deletions spec/unleash/client_spec.rb
@@ -1,10 +1,6 @@
require "spec_helper"

RSpec.describe Unleash do
it "has a version number" do
expect(Unleash::VERSION).not_to be nil
end

RSpec.describe Unleash::Client do
it "Uses custom http headers when initializing client" do
WebMock.stub_request(:post, "http://test-url//client/register")
.with(
Expand Down Expand Up @@ -80,17 +76,6 @@
})
.to_return(status: 200, body: "", headers: {})

WebMock.stub_request(:post, "http://test-url//client/features")
.with(
headers: {
'Accept'=>'*/*',
'Content-Type'=>'application/json',
'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
'User-Agent'=>'Ruby',
'X-Api-Key'=>'123'
})
.to_return(status: 200, body: '{"version": 1, "features": []}', headers: {})

Unleash.configure do |config|
config.url = 'http://test-url/'
config.app_name = 'my-test-app'
Expand All @@ -109,9 +94,46 @@
expect(
unleash_client.is_enabled?('any_feature', {}, true)
).to eq(true)

expect(WebMock).not_to have_requested(:get, 'http://test-url/')
expect(WebMock).to have_requested(:get, 'http://test-url//client/features')
expect(WebMock).to have_requested(:post, 'http://test-url//client/register')
expect(WebMock).not_to have_requested(:post, 'http://test-url//client/metrics')
end


it "should return default results if running with disable_client" do
Unleash.configure do |config|
config.disable_client = true
end
unleash_client = Unleash::Client.new

expect(
unleash_client.is_enabled?('any_feature', {}, true)
).to eq(true)

expect(
unleash_client.is_enabled?('any_feature2', {}, false)
).to eq(false)
end

it "does something useful" do
expect(false).to eq(false)
it "should not connect anywhere if running with disable_client" do
Unleash.configure do |config|
config.disable_client = true
config.url = 'http://test-url/'
config.custom_http_headers = 'invalid_string'
end

unleash_client = Unleash::Client.new

expect(
unleash_client.is_enabled?('any_feature', {}, true)
).to eq(true)

expect(WebMock).not_to have_requested(:get, 'http://test-url/')
expect(WebMock).not_to have_requested(:get, 'http://test-url//client/features')
expect(WebMock).not_to have_requested(:post, 'http://test-url//client/features')
expect(WebMock).not_to have_requested(:post, 'http://test-url//client/register')
expect(WebMock).not_to have_requested(:post, 'http://test-url//client/metrics')
end
end
5 changes: 5 additions & 0 deletions spec/unleash/configuration_spec.rb
Expand Up @@ -37,6 +37,11 @@
expect{ config.validate! }.not_to raise_error
end

it "should be lenient if disable_client is true" do
config = Unleash::Configuration.new(disable_client: true)
expect{ config.validate! }.not_to raise_error
end

it "support yield for setting the configuration" do
Unleash.configure do |config|
config.url = 'http://test-url/'
Expand Down
12 changes: 12 additions & 0 deletions spec/unleash_spec.rb
@@ -0,0 +1,12 @@
require "spec_helper"


RSpec.describe Unleash do
it "has a version number" do
expect(Unleash::VERSION).not_to be nil
end

it "does something useful" do
expect(false).to eq(false)
end
end
2 changes: 1 addition & 1 deletion unleash-client.gemspec
Expand Up @@ -26,7 +26,7 @@ Gem::Specification.new do |spec|

spec.add_dependency "murmurhash3", "~> 0.1.6"

spec.add_development_dependency "bundler", "~> 1.14"
spec.add_development_dependency "bundler", "~> 2.0"
spec.add_development_dependency "rake", "~> 10.0"
spec.add_development_dependency "rspec", "~> 3.0"
spec.add_development_dependency "webmock", "~> 3.0"
Expand Down

0 comments on commit 4190d7f

Please sign in to comment.