forked from dpep/berater_rb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
135 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--require spec_helper |
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 |
---|---|---|
@@ -1,39 +1,41 @@ | ||
PATH | ||
remote: . | ||
specs: | ||
berater (0.0.1) | ||
berater (0.0.2) | ||
redis | ||
|
||
GEM | ||
remote: https://rubygems.org/ | ||
specs: | ||
activesupport (6.1.1) | ||
concurrent-ruby (~> 1.0, >= 1.0.2) | ||
i18n (>= 1.6, < 2) | ||
minitest (>= 5.1) | ||
tzinfo (~> 2.0) | ||
zeitwerk (~> 2.3) | ||
clockwork (2.0.4) | ||
activesupport | ||
tzinfo | ||
concurrent-ruby (1.1.8) | ||
i18n (1.8.7) | ||
concurrent-ruby (~> 1.0) | ||
minitest (5.14.3) | ||
byebug (11.1.3) | ||
diff-lcs (1.4.4) | ||
rake (13.0.3) | ||
redis (4.1.3) | ||
tzinfo (2.0.4) | ||
concurrent-ruby (~> 1.0) | ||
zeitwerk (2.4.2) | ||
redis (4.2.5) | ||
rspec (3.10.0) | ||
rspec-core (~> 3.10.0) | ||
rspec-expectations (~> 3.10.0) | ||
rspec-mocks (~> 3.10.0) | ||
rspec-core (3.10.1) | ||
rspec-support (~> 3.10.0) | ||
rspec-expectations (3.10.1) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.10.0) | ||
rspec-mocks (3.10.1) | ||
diff-lcs (>= 1.2.0, < 2.0) | ||
rspec-support (~> 3.10.0) | ||
rspec-support (3.10.1) | ||
timecop (0.9.2) | ||
|
||
PLATFORMS | ||
ruby | ||
x86_64-darwin-19 | ||
|
||
DEPENDENCIES | ||
berater! | ||
clockwork | ||
minitest | ||
byebug | ||
rake | ||
rspec | ||
timecop | ||
|
||
BUNDLED WITH | ||
1.17.3 | ||
2.2.2 |
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
require 'rake/testtask' | ||
require 'rake' | ||
require 'rspec/core/rake_task' | ||
|
||
Rake::TestTask.new do |t| | ||
t.libs << 'test' | ||
end | ||
RSpec::Core::RakeTask.new | ||
|
||
desc "Run tests" | ||
task :default => :test | ||
task :default => :spec | ||
task :test => :spec |
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 |
---|---|---|
@@ -1,30 +1,30 @@ | ||
module Berater | ||
VERSION = '0.0.1' | ||
require 'berater/version' | ||
|
||
class << self | ||
|
||
def init redis | ||
@@redis = redis | ||
end | ||
module Berater | ||
extend self | ||
|
||
class LimitExceeded < RuntimeError; end | ||
|
||
def incr key, limit, seconds | ||
ts = Time.now.to_i | ||
attr_accessor :redis | ||
|
||
# bucket into time slot | ||
rkey = "%s:%s:%d" % [ self.to_s, key, ts - ts % seconds ] | ||
def configure redis | ||
self.redis = redis | ||
end | ||
|
||
count = @@redis.multi do | ||
@@redis.incr rkey | ||
@@redis.expire rkey, seconds * 2 | ||
end.first | ||
def incr key, limit, seconds | ||
ts = Time.now.to_i | ||
|
||
raise LimitExceeded if count > limit | ||
# bucket into time slot | ||
rkey = "%s:%s:%d" % [ self.to_s, key, ts - ts % seconds ] | ||
|
||
count | ||
count, _ = redis.multi do | ||
redis.incr rkey | ||
redis.expire rkey, seconds * 2 | ||
end | ||
|
||
end | ||
raise LimitExceeded if count > limit | ||
|
||
class LimitExceeded < RuntimeError; end | ||
count | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module Berater | ||
VERSION = '0.0.2' | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
describe Berater do | ||
it { is_expected.to respond_to :configure } | ||
|
||
it 'connects to Redis' do | ||
expect(Berater.redis.ping).to eq 'PONG' | ||
end | ||
|
||
def incr | ||
Berater.incr 'key', 5, 1 | ||
end | ||
|
||
it 'counts' do | ||
expect(incr).to eq 1 | ||
end | ||
|
||
it 'counts many times' do | ||
5.times do |i| | ||
expect(incr).to eq (i + 1) # i is 0-offset | ||
end | ||
end | ||
|
||
it 'limits excessive calls' do | ||
5.times do |i| | ||
expect(incr).to eq (i + 1) # i is 0-offset | ||
end | ||
|
||
expect { incr }.to raise_error(Berater::LimitExceeded) | ||
end | ||
|
||
it 'works with symbols' do | ||
count = Berater.incr :key, 5, 1 | ||
expect(count).to eq 1 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# $LOAD_PATH.unshift 'lib' | ||
|
||
require 'berater' | ||
require 'byebug' | ||
require 'redis' | ||
require 'timecop' | ||
|
||
class Numeric | ||
def seconds; self; end | ||
alias :second :seconds | ||
|
||
def minutes; self * 60; end | ||
alias :minute :minutes | ||
|
||
def hours; self * 3600; end | ||
alias :hour :hours | ||
|
||
def days; self * 86400; end | ||
alias :day :days | ||
end | ||
|
||
|
||
RSpec.configure do |config| | ||
config.before do | ||
Berater.configure Redis.new | ||
Berater.redis.flushall | ||
end | ||
|
||
# allow 'fit' examples | ||
config.filter_run_when_matching :focus | ||
|
||
config.around(:each) do |example| | ||
# only with blocks | ||
Timecop.safe_mode = true | ||
|
||
# Freeze time by default | ||
Timecop.freeze do | ||
example.run | ||
end | ||
end | ||
end | ||
|
This file was deleted.
Oops, something went wrong.