summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/loop_helpers_spec.rb
blob: e17a0342d646b98000017a20c1cce0f6aeb76700 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
require 'spec_helper'

describe Gitlab::LoopHelpers do
  let(:class_instance) { (Class.new { include ::Gitlab::LoopHelpers }).new }

  describe '#loop_until' do
    subject do
      class_instance.loop_until(**params) { true }
    end

    context 'when limit is not given' do
      let(:params) { { limit: nil } }

      it 'raises an error' do
        expect { subject }.to raise_error(ArgumentError)
      end
    end

    context 'when timeout is specified' do
      let(:params) { { timeout: 1.second } }

      it "returns false after it's expired" do
        is_expected.to be_falsy
      end

      it 'executes the block at least once' do
        expect { |b| class_instance.loop_until(**params, &b) }
          .to yield_control.at_least(1)
      end
    end

    context 'when iteration limit is specified' do
      let(:params) { { limit: 1 } }

      it "returns false after it's expired" do
        is_expected.to be_falsy
      end

      it 'executes the block once' do
        expect { |b| class_instance.loop_until(**params, &b) }
          .to yield_control.once
      end
    end
  end
end