-
-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #847 from flippercloud/actor-limit
Add ActorLimit adapter
- Loading branch information
Showing
6 changed files
with
105 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Flipper | ||
module Adapters | ||
class ActorLimit < Wrapper | ||
LimitExceeded = Class.new(Flipper::Error) | ||
|
||
attr_reader :limit | ||
|
||
def initialize(adapter, limit = 100) | ||
super(adapter) | ||
@limit = limit | ||
end | ||
|
||
def enable(feature, gate, resource) | ||
if gate.is_a?(Flipper::Gates::Actor) && over_limit?(feature) | ||
raise LimitExceeded, "Actor limit of #{@limit} exceeded for feature #{feature.key}. See https://www.flippercloud.io/docs/features/actors#limitations" | ||
else | ||
super | ||
end | ||
end | ||
|
||
private | ||
|
||
def over_limit?(feature) | ||
feature.actors_value.size >= @limit | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require "flipper/adapters/actor_limit" | ||
|
||
RSpec.describe Flipper::Adapters::ActorLimit do | ||
it_should_behave_like 'a flipper adapter' do | ||
let(:limit) { 5 } | ||
let(:adapter) { Flipper::Adapters::ActorLimit.new(Flipper::Adapters::Memory.new, limit) } | ||
|
||
subject { adapter } | ||
|
||
describe '#enable' do | ||
it "fails when limit exceeded" do | ||
5.times { |i| feature.enable Flipper::Actor.new("User;#{i}") } | ||
|
||
expect { | ||
feature.enable Flipper::Actor.new("User;6") | ||
}.to raise_error(Flipper::Adapters::ActorLimit::LimitExceeded) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require "test_helper" | ||
require "flipper/test/shared_adapter_test" | ||
require "flipper/adapters/actor_limit" | ||
|
||
class Flipper::Adapters::ActorLimitTest < MiniTest::Test | ||
prepend Flipper::Test::SharedAdapterTests | ||
|
||
def setup | ||
@memory = Flipper::Adapters::Memory.new | ||
@adapter = Flipper::Adapters::ActorLimit.new(@memory, 5) | ||
end | ||
|
||
def test_enable_fails_when_limit_exceeded | ||
5.times { |i| @feature.enable Flipper::Actor.new("User;#{i}") } | ||
|
||
assert_raises Flipper::Adapters::ActorLimit::LimitExceeded do | ||
@feature.enable Flipper::Actor.new("User;6") | ||
end | ||
end | ||
end |