Sha256: 836abea0c101aca89dbce767f8aa53e4adb046dddf6e3fe8fa0e9585ca123135

Contents?: true

Size: 1.71 KB

Versions: 2

Compression:

Stored size: 1.71 KB

Contents

require 'spec_helper'

describe Weeter::Limitator do
  let(:limitator) do
    Weeter::Limitator.new({
      max: max,
      duration: duration
    })
  end

  let(:duration) { 10.minutes }
  let(:max) { 10 }

  let(:keys) { ['key'] }

  describe '.new' do
    it { limitator.should be }
  end

  describe '#limit?' do

    subject do
      limitator.limit?(*keys)
    end

    context 'max: 0' do
      let(:max) { 0 }
      it { should be_true }

      context 'no keys' do
        let(:keys) { [] }
        it { should be_false }
      end
    end

    context 'max: 1' do
      let(:max) { 1 }

      it { should be_false }

      context 'two keys within max' do
        let(:keys) { ['key', 'key2'] }

        it { should be_false }
      end

      context 'no keys' do
        let(:keys) { [] }
        it { should be_false }
      end

      context 'one key outside max' do
        before do
          max.times do
            limitator.limit?(*keys)
          end
        end

        it { should be_true }

        context 'outside duration' do
          let(:some_time_after_duration) do
            Time.now + duration
          end

          before do
            limitator.stub(:now).and_return(some_time_after_duration)
          end

          it { should be_false }
        end
      end

      context 'two keys outside' do
        let(:keys) { ['key', 'key2'] }

        before do
          limitator.limit?(*keys)
        end

        it { should be_true }
      end

      context 'one key outside max: 1, one key within max: 1' do
        let(:max) { 1 }
        let(:keys) { ['key', 'key2'] }

        before do
          limitator.limit?(*[keys.first])
        end

        it { should be_true }
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
weeter-0.11.0 spec/weeter/limitator_spec.rb
weeter-0.10.0 spec/weeter/limitator_spec.rb