BigW Consortium Gitlab

optimistic_locking_spec.rb 1.41 KB
Newer Older
1 2 3
require 'spec_helper'

describe Gitlab::OptimisticLocking, lib: true do
4 5
  let!(:pipeline) { create(:ci_pipeline) }
  let!(:pipeline2) { Ci::Pipeline.find(pipeline.id) }
6

7
  describe '#retry_lock' do
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
    it 'does not reload object if state changes' do
      expect(pipeline).not_to receive(:reload)
      expect(pipeline).to receive(:succeed).and_call_original

      described_class.retry_lock(pipeline) do |subject|
        subject.succeed
      end
    end

    it 'retries action if exception is raised' do
      pipeline.succeed

      expect(pipeline2).to receive(:reload).and_call_original
      expect(pipeline2).to receive(:drop).twice.and_call_original

      described_class.retry_lock(pipeline2) do |subject|
        subject.drop
      end
    end
27 28 29 30 31 32 33 34 35 36 37

    it 'raises exception when too many retries' do
      expect(pipeline).to receive(:drop).twice.and_call_original

      expect do
        described_class.retry_lock(pipeline, 1) do |subject|
          subject.lock_version = 100
          subject.drop
        end
      end.to raise_error(ActiveRecord::StaleObjectError)
    end
38
  end
39 40 41 42 43 44 45 46 47 48 49 50 51

  describe '#retry_optimistic_lock' do
    context 'when locking module is mixed in' do
      let(:unlockable) do
        Class.new.include(described_class).new
      end

      it 'is an alias for retry_lock' do
        expect(unlockable.method(:retry_optimistic_lock))
          .to eq unlockable.method(:retry_lock)
      end
    end
  end
52
end