Welcome to mirror list, hosted at ThFree Co, Russian Federation.

timed_logger_spec.rb « checks « gitlab « lib « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 261fdd6c0027a747a62615b7c9990889acb5c4a3 (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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::Checks::TimedLogger do
  let!(:timeout) { 50.seconds }
  let!(:start) { Time.now }
  let!(:ref) { "bar" }
  let!(:logger) { described_class.new(start_time: start, timeout: timeout) }
  let!(:log_messages) do
    {
      foo: "Foo message..."
    }
  end

  before do
    logger.append_message("Checking ref: #{ref}")
  end

  around do |example|
    freeze_time do
      example.run
    end
  end

  describe '#log_timed' do
    it 'logs message' do
      travel_to(start + 30.seconds)

      logger.log_timed(log_messages[:foo], start) { bar_check }

      expect(logger.full_message).to eq("Checking ref: bar\nFoo message... (30000.0ms)")
    end

    context 'when time limit was reached' do
      it 'cancels action' do
        travel_to(start + 50.seconds)

        expect do
          logger.log_timed(log_messages[:foo], start) do
            bar_check
          end
        end.to raise_error(described_class::TimeoutError)

        expect(logger.full_message).to eq("Checking ref: bar\nFoo message... (cancelled)")
      end

      it 'cancels action with time elapsed if work was performed' do
        travel_to(start + 30.seconds)

        expect do
          logger.log_timed(log_messages[:foo], start) do
            grpc_check
          end
        end.to raise_error(described_class::TimeoutError)

        expect(logger.full_message).to eq("Checking ref: bar\nFoo message... (cancelled after 30000.0ms)")
      end
    end
  end

  def bar_check
    2 + 2
  end

  def grpc_check
    raise GRPC::DeadlineExceeded
  end
end