Sha256: 1111ceea1592ad826f4926b13d498e5f206364ac4cdaac6e94fd4a650d144834

Contents?: true

Size: 1.12 KB

Versions: 19

Compression:

Stored size: 1.12 KB

Contents

# encoding: UTF-8

require 'test_helper'

describe Vines::TokenBucket do
  subject { Vines::TokenBucket.new(10, 1) }

  it 'raises with invalid capacity and rate values' do
    -> { Vines::TokenBucket.new(0, 1) }.must_raise ArgumentError
    -> { Vines::TokenBucket.new(1, 0) }.must_raise ArgumentError
    -> { Vines::TokenBucket.new(-1, 1) }.must_raise ArgumentError
    -> { Vines::TokenBucket.new(1, -1) }.must_raise ArgumentError
  end

  it 'does not allow taking a negative number of tokens' do
    -> { subject.take(-1) }.must_raise ArgumentError
  end

  it 'does not allow taking more tokens than its capacity' do
    refute subject.take(11)
  end

  it 'allows taking all tokens, but no more' do
    assert subject.take(10)
    refute subject.take(1)
  end

  it 'refills over time' do
    assert subject.take(10)
    refute subject.take(1)
    Time.stub(:new, Time.now + 1) do
      assert subject.take(1)
      refute subject.take(1)
    end
  end

  it 'does not refill over capacity' do
    assert subject.take(10)
    refute subject.take(1)
    Time.stub(:new, Time.now + 15) do
      refute subject.take(11)
    end
  end
end

Version data entries

19 entries across 19 versions & 3 rubygems

Version Path
diaspora-vines-0.2.0.develop.4 test/token_bucket_test.rb
diaspora-vines-0.2.0.develop.3 test/token_bucket_test.rb
diaspora-vines-0.2.0.develop.2 test/token_bucket_test.rb
diaspora-vines-0.2.0.develop.1 test/token_bucket_test.rb
diaspora-vines-0.1.28 test/token_bucket_test.rb
lygneo-vines-0.1.5 test/token_bucket_test.rb
lygneo-vines-0.1.1 test/token_bucket_test.rb
diaspora-vines-0.1.27 test/token_bucket_test.rb
diaspora-vines-0.1.26 test/token_bucket_test.rb
diaspora-vines-0.1.25 test/token_bucket_test.rb
diaspora-vines-0.1.24 test/token_bucket_test.rb
vines-0.4.10 test/token_bucket_test.rb
diaspora-vines-0.1.22 test/token_bucket_test.rb
diaspora-vines-0.1.21 test/token_bucket_test.rb
diaspora-vines-0.1.2 test/token_bucket_test.rb
vines-0.4.9 test/token_bucket_test.rb
vines-0.4.8 test/token_bucket_test.rb
vines-0.4.7 test/token_bucket_test.rb
vines-0.4.6 test/token_bucket_test.rb