Sha256: b952e384fe24f734bd3562d15dcce47523a1ef96a9ec2940c337daf8234b41d5

Contents?: true

Size: 1.95 KB

Versions: 6

Compression:

Stored size: 1.95 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 "when 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

6 entries across 6 versions & 1 rubygems

Version Path
infopark_component_cache-5.0.2 spec/lib/infopark_component_cache/delayed_guard_spec.rb
infopark_component_cache-5.0.1 spec/lib/infopark_component_cache/delayed_guard_spec.rb
infopark_component_cache-4.2.0 spec/lib/infopark_component_cache/delayed_guard_spec.rb
infopark_component_cache-4.1.0 spec/lib/infopark_component_cache/delayed_guard_spec.rb
infopark_component_cache-4.0.1 spec/lib/infopark_component_cache/delayed_guard_spec.rb
infopark_component_cache-4.0.0 spec/lib/infopark_component_cache/delayed_guard_spec.rb