Commit d580bea6 authored by Eduardo Bonet's avatar Eduardo Bonet

Adds tests for counter increment

parent ce1bf1ff
...@@ -21,7 +21,7 @@ module Gitlab ...@@ -21,7 +21,7 @@ module Gitlab
end end
end end
rescue Timeout::Error => e 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) log_event(LOG_IPYNBDIFF_TIMEOUT, e)
rescue IpynbDiff::InvalidNotebookError, IpynbDiff::InvalidTokenError => e rescue IpynbDiff::InvalidNotebookError, IpynbDiff::InvalidTokenError => e
log_event(LOG_IPYNBDIFF_INVALID, e) log_event(LOG_IPYNBDIFF_INVALID, e)
...@@ -74,6 +74,10 @@ module Gitlab ...@@ -74,6 +74,10 @@ module Gitlab
Gitlab::Runtime.sidekiq? ? RENDERED_TIMEOUT_BACKGROUND : RENDERED_TIMEOUT_FOREGROUND Gitlab::Runtime.sidekiq? ? RENDERED_TIMEOUT_BACKGROUND : RENDERED_TIMEOUT_FOREGROUND
end end
def execution_source
Gitlab::Runtime.sidekiq? ? BACKGROUND_EXECUTION : FOREGROUND_EXECUTION
end
def log_event(message, error = nil) def log_event(message, error = nil)
Gitlab::AppLogger.info({ message: message }) Gitlab::AppLogger.info({ message: message })
Gitlab::ErrorTracking.log_exception(error) if error Gitlab::ErrorTracking.log_exception(error) if error
......
...@@ -36,24 +36,45 @@ RSpec.describe Gitlab::Diff::CustomDiff do ...@@ -36,24 +36,45 @@ RSpec.describe Gitlab::Diff::CustomDiff do
end end
context 'timeout' do context 'timeout' do
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(subject).to be_nil
end
context 'when in foreground' do
it 'utilizes timeout for web' do it 'utilizes timeout for web' do
expect(Timeout).to receive(:timeout).with(described_class::RENDERED_TIMEOUT_FOREGROUND).and_call_original 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') expect(subject).not_to include('cells')
end end
it 'falls back to nil on timeout' do it 'increments metrics' do
allow(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception)
expect(Timeout).to receive(:timeout).and_raise(Timeout::Error) 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
expect(described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)).to be_nil context 'when in background' do
before do
allow(Gitlab::Runtime).to receive(:sidekiq?).and_return(true)
end end
it 'utilizes longer timeout for sidekiq' do 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 expect(Timeout).to receive(:timeout).with(described_class::RENDERED_TIMEOUT_BACKGROUND).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::BACKGROUND_EXECUTION) }.by(1)
end
end end
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