# frozen_string_literal: true require "spec_helpers" describe Wayfarer::GC, redis: true do include Wayfarer::Redis::Connection let(:task) { build(:task) } let(:klass) { Class.new(Wayfarer::Base) } let(:job) { klass.new } subject(:gc) { Wayfarer::GC.new(job) } before do job.arguments = [task] task.barrier.seen?(task.url) 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 { |spy| klass.after_batch(&spy) gc.run }.to yield_control 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