summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/ci/pipeline/duration_spec.rb
blob: a4984092f35fa7c6844b78c80173b0a554643368 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# frozen_string_literal: true

require 'spec_helper'

describe Gitlab::Ci::Pipeline::Duration do
  let(:calculated_duration) { calculate(data) }

  shared_examples 'calculating duration' do
    it do
      expect(calculated_duration).to eq(duration)
    end
  end

  context 'test sample A' do
    let(:data) do
      [[0, 1],
       [1, 2],
       [3, 4],
       [5, 6]]
    end

    let(:duration) { 4 }

    it_behaves_like 'calculating duration'
  end

  context 'test sample B' do
    let(:data) do
      [[0, 1],
       [1, 2],
       [2, 3],
       [3, 4],
       [0, 4]]
    end

    let(:duration) { 4 }

    it_behaves_like 'calculating duration'
  end

  context 'test sample C' do
    let(:data) do
      [[0, 4],
       [2, 6],
       [5, 7],
       [8, 9]]
    end

    let(:duration) { 8 }

    it_behaves_like 'calculating duration'
  end

  context 'test sample D' do
    let(:data) do
      [[0, 1],
       [2, 3],
       [4, 5],
       [6, 7]]
    end

    let(:duration) { 4 }

    it_behaves_like 'calculating duration'
  end

  context 'test sample E' do
    let(:data) do
      [[0, 1],
       [3, 9],
       [3, 4],
       [3, 5],
       [3, 8],
       [4, 5],
       [4, 7],
       [5, 8]]
    end

    let(:duration) { 7 }

    it_behaves_like 'calculating duration'
  end

  context 'test sample F' do
    let(:data) do
      [[1, 3],
       [2, 4],
       [2, 4],
       [2, 4],
       [5, 8]]
    end

    let(:duration) { 6 }

    it_behaves_like 'calculating duration'
  end

  context 'test sample G' do
    let(:data) do
      [[1, 3],
       [2, 4],
       [6, 7]]
    end

    let(:duration) { 4 }

    it_behaves_like 'calculating duration'
  end

  def calculate(data)
    periods = data.shuffle.map do |(first, last)|
      described_class::Period.new(first, last)
    end

    described_class.from_periods(periods.sort_by(&:first))
  end
end