# frozen_string_literal: true require "spec_helpers" describe Wayfarer::GC, redis: true do include Wayfarer::Redis::Connection let(:task) { build(:task) } let(:klass) { Struct.new(:arguments) } let(:job) { klass.new([task]) } subject(:gc) { Wayfarer::GC.new(job) } before do allow(klass).to receive(:run_after_batch_callbacks) task.barrier.seen?("https://example.com") end describe "#run" do context "when counter reaches 0" do before { task.counter.increment } it "resets the barrier" do expect { gc.run }.to change { redis { |conn| conn.exists?(task.barrier.redis_key) } }.to(false) end it "resets the counter" do expect { gc.run }.to change { redis { |conn| conn.exists?(task.counter.redis_key) } }.to(false) end it "runs after batch callbacks" do expect(klass).to receive(:run_after_batch_callbacks).exactly(:once) gc.run end end context "when counter does not reach 0" do before { 2.times { task.counter.increment } } it "does not reset the barrier" do expect { gc.run }.not_to(change { redis { |conn| conn.exists?(task.barrier.redis_key) } }) end it "does not reset the counter" do expect { gc.run }.not_to(change { redis { |conn| conn.exists?(task.counter.redis_key) } }) end it "does not run after batch callbacks" do expect(klass).not_to receive(:run_after_batch_callbacks) gc.run end end end end