Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resetting throttle count between tests #249

Closed
theirishpenguin opened this issue Nov 8, 2017 · 5 comments
Closed

Resetting throttle count between tests #249

theirishpenguin opened this issue Nov 8, 2017 · 5 comments

Comments

@theirishpenguin
Copy link

Just wondering, what is the best way to reset the rack attack counters between "it" blocks in a spec, so that throttling can be tested independently in different "it" blocks.

I presume one would use an around block like this...

        around(:each) do |each|
            clear_rack_attack_cache
            each.run
            clear_rack_attack_cache
        end

Then perhaps...

        def clear_rack_attack_cache
            while Rack::Attack.cache.store.instance_variable_get(:@data).keys.length > 0
                Rack::Attack.cache.store.clear
                sleep(1)
            end
        end

FYI, using ActiveSupport::Cache::MemoryStore as the backing store. Is a sleep required or is this synchronous?

Either way, it doesn't seem to be working quite right for me as I don't always get the expected number of 200's before I get a 429.

@markedmondson
Copy link

I'm doing this, it seems to work...

    Rack::Attack.throttles.each do |k, throttle|
      Rack::Attack.cache.reset_count("#{k}:#{key}", throttle.period)
    end

@grzuy
Copy link
Collaborator

grzuy commented Mar 22, 2018

Hi Declan,

Great question.

I think this is something we should add explicit support for, so users don't need to hack the cache store in order to achieve test case isolation in their app tests.

It appears to me that Rack::Attack.clear! is a bit confusing in that is resetting some things, not everything.

@grzuy
Copy link
Collaborator

grzuy commented Jun 29, 2018

For the record,

It appears to me that Rack::Attack.clear! is a bit confusing in that is resetting some things, not everything.

clear! has been renamed to clear_configuration to avoid further confusion in #344.

think this is something we should add explicit support for, so users don't need to hack the cache store in order to achieve test case isolation in their app tests.

Issue with this goal is the following #301

@grzuy grzuy pinned this issue Oct 7, 2019
@grzuy
Copy link
Collaborator

grzuy commented Oct 7, 2019

We should consider adding a Rack::Attack.reset! method, which should reset all Rack::Attack-specific state (mostly throttles, fail2ban and allow2ban counters) so that users can for example do something like:

config.before do
  Rack::Attack.reset!
end

when using RSpec.

@grzuy
Copy link
Collaborator

grzuy commented Oct 23, 2019

We should consider adding a Rack::Attack.reset! method, which should reset all Rack::Attack-specific state (mostly throttles, fail2ban and allow2ban counters) so that users can for example do something like:

config.before do
  Rack::Attack.reset!
end

when using RSpec.

Rack::Attack.reset! has been implemented and merged in #436.

@grzuy grzuy closed this as completed Oct 23, 2019
@grzuy grzuy unpinned this issue Oct 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants