File: report_distributor_spec.rb

package info (click to toggle)
ruby-knapsack 1.18.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 1,084 kB
  • sloc: ruby: 2,832; makefile: 4; sh: 3
file content (186 lines) | stat: -rw-r--r-- 4,112 bytes parent folder | download
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
describe Knapsack::Distributors::ReportDistributor do
  let(:report) { { 'a_spec.rb' => 1.0 } }
  let(:default_args) do
    {
      report: report,
      test_file_pattern: 'spec/**{,/*/**}/*_spec.rb',
      ci_node_total: '1',
      ci_node_index: '0'
    }
  end
  let(:args) { default_args.merge(custom_args) }
  let(:custom_args) { {} }
  let(:distributor) { described_class.new(args) }

  describe '#sorted_report' do
    subject { distributor.sorted_report }

    let(:report) do
      {
        'e_spec.rb' => 3.0,
        'f_spec.rb' => 3.5,
        'c_spec.rb' => 2.0,
        'd_spec.rb' => 2.5,
        'a_spec.rb' => 1.0,
        'b_spec.rb' => 1.5,
      }
    end

    it do
      should eql([
        ['f_spec.rb', 3.5],
        ['e_spec.rb', 3.0],
        ['d_spec.rb', 2.5],
        ['c_spec.rb', 2.0],
        ['b_spec.rb', 1.5],
        ['a_spec.rb', 1.0],
      ])
    end
  end

  describe '#sorted_report_with_existing_tests' do
    subject { distributor.sorted_report_with_existing_tests }

    let(:report) do
      {
        'e_spec.rb' => 3.0,
        'f_spec.rb' => 3.5,
        'c_spec.rb' => 2.0,
        'd_spec.rb' => 2.5,
        'a_spec.rb' => 1.0,
        'b_spec.rb' => 1.5,
      }
    end

    before do
      expect(distributor).to receive(:all_tests).exactly(6).times.and_return([
        'b_spec.rb',
        'd_spec.rb',
        'f_spec.rb',
      ])
    end

    it do
      should eql([
        ['f_spec.rb', 3.5],
        ['d_spec.rb', 2.5],
        ['b_spec.rb', 1.5],
      ])
    end
  end

  context do
    let(:report) do
      {
        'a_spec.rb' => 3.0,
        'b_spec.rb' => 1.0,
        'c_spec.rb' => 1.5,
      }
    end

    before do
      allow(distributor).to receive(:all_tests).and_return(report.keys)
    end

    describe '#total_time_execution' do
      subject { distributor.total_time_execution }

      context 'when time is float' do
        it { should eql 5.5 }
      end

      context 'when time is not float' do
        let(:report) do
          {
            'a_spec.rb' => 3,
            'b_spec.rb' => 1,
          }
        end

        it { should eql 4.0 }
      end
    end

    describe '#node_time_execution' do
      subject { distributor.node_time_execution }
      let(:custom_args) { { ci_node_total: 4 } }
      it { should eql 1.375 }
    end
  end

  context do
    let(:report) do
      {
        'g_spec.rb' => 9.0,
        'h_spec.rb' => 3.0,
        'i_spec.rb' => 3.0,
        'f_spec.rb' => 3.5,
        'c_spec.rb' => 2.0,
        'd_spec.rb' => 2.5,
        'a_spec.rb' => 1.0,
        'b_spec.rb' => 1.5,
      }
    end
    let(:custom_args) { { ci_node_total: 3 } }

    before do
      allow(distributor).to receive(:all_tests).and_return(report.keys)
    end

    describe '#assign_test_files_to_node' do
      before { distributor.assign_test_files_to_node }

      it do
        expect(distributor.node_tests[0]).to eql({
          :node_index => 0,
          :time_left => -0.5,
          :test_files_with_time => [
            ["g_spec.rb", 9.0]
          ]
        })
      end

      it do
        expect(distributor.node_tests[1]).to eql({
          :node_index => 1,
          :time_left => 0.0,
          :test_files_with_time => [
            ["f_spec.rb", 3.5],
            ["d_spec.rb", 2.5],
            ["b_spec.rb", 1.5],
            ["a_spec.rb", 1.0]
          ]
        })
      end

      it do
        expect(distributor.node_tests[2]).to eql({
          :node_index => 2,
          :time_left => 0.5,
          :test_files_with_time => [
            ["i_spec.rb", 3.0],
            ["h_spec.rb", 3.0],
            ["c_spec.rb", 2.0]
          ]
        })
      end
    end

    describe '#tests_for_node' do
      context 'when node exists' do
        it do
          expect(distributor.tests_for_node(1)).to eql([
            'f_spec.rb',
            'd_spec.rb',
            'b_spec.rb',
            'a_spec.rb'
          ])
        end
      end

      context "when node doesn't exist" do
        it { expect(distributor.tests_for_node(42)).to be_nil }
      end
    end
  end
end