Skip to content

Commit

Permalink
Merge c09d24b into 3165da3
Browse files Browse the repository at this point in the history
  • Loading branch information
jdurkee-mdsol committed Apr 27, 2023
2 parents 3165da3 + c09d24b commit 0f4ac58
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 9 deletions.
5 changes: 0 additions & 5 deletions lib/unleash/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,6 @@ def if_disabled(feature, context = nil, default_value = true, &blk)
def get_variant(feature, context = Unleash::Context.new, fallback_variant = disabled_variant)
Unleash.logger.debug "Unleash::Client.get_variant for feature: #{feature} with context #{context}"

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

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

if toggle_as_hash.nil?
Expand Down
8 changes: 4 additions & 4 deletions lib/unleash/feature_toggle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ def sum_variant_defs_weights
end

def variant_salt(context, stickiness = "default")
return context.get_by_name(stickiness) unless stickiness == "default"
return context.user_id unless context.user_id.to_s.empty?
return context.session_id unless context.session_id.to_s.empty?
return context.remote_address unless context.remote_address.to_s.empty?
return context.get_by_name(stickiness) unless stickiness == "default" || context.nil?
return context.user_id unless context&.user_id.to_s.empty?
return context.session_id unless context&.session_id.to_s.empty?
return context.remote_address unless context&.remote_address.to_s.empty?

SecureRandom.random_number
end
Expand Down
26 changes: 26 additions & 0 deletions spec/unleash/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,23 @@
"name": "featureX",
"enabled": true,
"strategies": [{ "name": "default" }]
},
{
"enabled": true,
"name": "featureVariantX",
"strategies": [{ "name": "default" }],
"variants": [
{
"name": "default-value",
"payload": {
"type": "string",
"value": "info"
},
"stickiness": "custom_attribute",
"weight": 100,
"weightType": "variable"
}
]
}
]
}'
Expand All @@ -209,6 +226,15 @@
unleash_client.is_enabled?('featureX', {}, false)
).to be true

default_variant = Unleash::Variant.new(
name: 'featureVariantX',
enabled: false,
payload: { type: 'string', value: 'bogus' }
)
variant = unleash_client.get_variant('featureVariantX', nil, default_variant)
expect(variant.enabled).to be true
expect(variant.payload.fetch('value')).to eq('info')

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')
Expand Down

0 comments on commit 0f4ac58

Please sign in to comment.