Commit d580bea6 authored by Eduardo Bonet's avatar Eduardo Bonet

Adds tests for counter increment

parent ce1bf1ff
......@@ -21,7 +21,7 @@ module Gitlab
end
end
rescue Timeout::Error => e
rendered_timeout.increment(source: Gitlab::Runtime.sidekiq? ? BACKGROUND_EXECUTION : FOREGROUND_EXECUTION)
rendered_timeout.increment(source: execution_source)
log_event(LOG_IPYNBDIFF_TIMEOUT, e)
rescue IpynbDiff::InvalidNotebookError, IpynbDiff::InvalidTokenError => e
log_event(LOG_IPYNBDIFF_INVALID, e)
......@@ -74,6 +74,10 @@ module Gitlab
Gitlab::Runtime.sidekiq? ? RENDERED_TIMEOUT_BACKGROUND : RENDERED_TIMEOUT_FOREGROUND
end
def execution_source
Gitlab::Runtime.sidekiq? ? BACKGROUND_EXECUTION : FOREGROUND_EXECUTION
end
def log_event(message, error = nil)
Gitlab::AppLogger.info({ message: message })
Gitlab::ErrorTracking.log_exception(error) if error
......
......@@ -36,24 +36,45 @@ RSpec.describe Gitlab::Diff::CustomDiff do
end
context 'timeout' do
it 'utilizes timeout for web' do
expect(Timeout).to receive(:timeout).with(described_class::RENDERED_TIMEOUT_FOREGROUND).and_call_original
expect(described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)).not_to include('cells')
end
subject { described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob) }
it 'falls back to nil on timeout' do
allow(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception)
expect(Timeout).to receive(:timeout).and_raise(Timeout::Error)
expect(described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)).to be_nil
expect(subject).to be_nil
end
it 'utilizes longer timeout for sidekiq' do
allow(Gitlab::Runtime).to receive(:sidekiq?).and_return(true)
expect(Timeout).to receive(:timeout).with(described_class::RENDERED_TIMEOUT_BACKGROUND).and_call_original
context 'when in foreground' do
it 'utilizes timeout for web' do
expect(Timeout).to receive(:timeout).with(described_class::RENDERED_TIMEOUT_FOREGROUND).and_call_original
described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)
expect(subject).not_to include('cells')
end
it 'increments metrics' do
expect(Timeout).to receive(:timeout).and_raise(Timeout::Error)
counter = Gitlab::Metrics.counter(:rendered_timeout, 'desc')
expect { subject }.to change { counter.get(source: described_class::FOREGROUND_EXECUTION) }.by(1)
end
end
context 'when in background' do
before do
allow(Gitlab::Runtime).to receive(:sidekiq?).and_return(true)
end
it 'utilizes longer timeout for sidekiq' do
expect(Timeout).to receive(:timeout).with(described_class::RENDERED_TIMEOUT_BACKGROUND).and_call_original
expect(subject).not_to include('cells')
end
it 'increments metrics' do
expect(Timeout).to receive(:timeout).and_raise(Timeout::Error)
counter = Gitlab::Metrics.counter(:rendered_timeout, 'desc')
expect { subject }.to change { counter.get(source: described_class::BACKGROUND_EXECUTION) }.by(1)
end
end
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment