Sha256: 95ac7895a04682dfb1e64da533dc63376ebcd80e8c4c24fcef4a7cb2116ec0cb

Contents?: true

Size: 1.94 KB

Versions: 5

Compression:

Stored size: 1.94 KB

Contents

require 'spec_helper'
require 'infopark_component_cache/delayed_guard'

describe InfoparkComponentCache::DelayedGuard do
  let(:base_class_with_options) do
    Class.new do
      def options
        {}
      end
    end
  end

  let(:undelayed_class) do
    Class.new(base_class_with_options) do
      def simple_counter
        @simple_counter ||= 0
        @simple_counter += 1
      end
    end
  end

  let(:delayed_class) do
    Class.new(undelayed_class) do
      include InfoparkComponentCache::DelayedGuard
      delay :simple_counter, for: 1.second
    end
  end

  let(:undelayed_subject) { undelayed_class.new }
  let(:delayed_subject) { delayed_class.new }

  context "with caching disabled" do
    disable_cache

    specify "the calls are not delayed" do
      10.times do
        expect(undelayed_subject.simple_counter).to eq(delayed_subject.simple_counter)
      end
    end
  end

  context "with caching enabled" do
    enable_cache
    before { InfoparkComponentCache::VolatileCacheStorage.instance.send(:backing_storage).send(:clear) }

    specify "first call returns the same value" do
      expect(undelayed_subject.simple_counter).to eq(delayed_subject.simple_counter)
    end

    specify "following call does return the same value" do
      first_value = delayed_subject.simple_counter
      expect(first_value).to eq(undelayed_subject.simple_counter)
      10.times do
        expect(delayed_subject.simple_counter).to eq(first_value)
        expect(undelayed_subject.simple_counter).not_to eq(first_value)
      end
    end

    context "after the delay has passed" do
      specify "the calls returns new value" do
        first_value = delayed_subject.simple_counter
        expect(first_value).to eq(undelayed_subject.simple_counter)

        sleep 1

        second_value = delayed_subject.simple_counter
        expect(second_value).not_to eq(first_value)
        expect(second_value).to eq(undelayed_subject.simple_counter)
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
infopark_component_cache-3.2.0 spec/lib/delayed_guard_spec.rb
infopark_component_cache-3.1.1 spec/lib/delayed_guard_spec.rb
infopark_component_cache-3.1.0 spec/lib/delayed_guard_spec.rb
infopark_component_cache-3.0.0 spec/lib/delayed_guard_spec.rb
infopark_component_cache-2.0.0 spec/lib/delayed_guard_spec.rb