2018-10-22 14:49:20 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Gitlab::Checks::TimedLogger do
|
2018-10-25 09:36:40 +00:00
|
|
|
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
|
2018-10-22 14:49:20 +00:00
|
|
|
{
|
|
|
|
foo: "Foo message..."
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2018-10-25 09:36:40 +00:00
|
|
|
before do
|
|
|
|
logger.append_message("Checking ref: #{ref}")
|
2018-10-22 14:49:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#log_timed' do
|
|
|
|
it 'logs message' do
|
|
|
|
Timecop.freeze(start + 30.seconds) do
|
2018-10-25 09:36:40 +00:00
|
|
|
logger.log_timed(log_messages[:foo], start) { bar_check }
|
2018-10-22 14:49:20 +00:00
|
|
|
end
|
|
|
|
|
2018-10-25 09:36:40 +00:00
|
|
|
expect(logger.full_message).to eq("Checking ref: bar\nFoo message... (30000.0ms)")
|
2018-10-22 14:49:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when time limit was reached' do
|
|
|
|
it 'cancels action' do
|
|
|
|
Timecop.freeze(start + 50.seconds) do
|
|
|
|
expect do
|
2018-10-25 09:36:40 +00:00
|
|
|
logger.log_timed(log_messages[:foo], start) do
|
|
|
|
bar_check
|
2018-10-22 14:49:20 +00:00
|
|
|
end
|
|
|
|
end.to raise_error(described_class::TimeoutError)
|
|
|
|
end
|
|
|
|
|
2018-10-25 09:36:40 +00:00
|
|
|
expect(logger.full_message).to eq("Checking ref: bar\nFoo message... (cancelled)")
|
2018-10-22 14:49:20 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'cancels action with time elapsed if work was performed' do
|
|
|
|
Timecop.freeze(start + 30.seconds) do
|
|
|
|
expect do
|
2018-10-25 09:36:40 +00:00
|
|
|
logger.log_timed(log_messages[:foo], start) do
|
|
|
|
grpc_check
|
2018-10-22 14:49:20 +00:00
|
|
|
end
|
|
|
|
end.to raise_error(described_class::TimeoutError)
|
|
|
|
|
2018-10-25 09:36:40 +00:00
|
|
|
expect(logger.full_message).to eq("Checking ref: bar\nFoo message... (cancelled after 30000.0ms)")
|
2018-10-22 14:49:20 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-10-25 09:36:40 +00:00
|
|
|
|
|
|
|
def bar_check
|
|
|
|
2 + 2
|
|
|
|
end
|
|
|
|
|
|
|
|
def grpc_check
|
|
|
|
raise GRPC::DeadlineExceeded
|
|
|
|
end
|
2018-10-22 14:49:20 +00:00
|
|
|
end
|