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
it 'utilizes timeout for web' do subject { described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob) }
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
it 'falls back to nil on timeout' do it 'falls back to nil on timeout' do
allow(Gitlab::ErrorTracking).to receive(:track_and_raise_for_dev_exception) 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)
expect(described_class.preprocess_before_diff(ipynb_blob.path, nil, ipynb_blob)).to be_nil expect(subject).to be_nil
end end
it 'utilizes longer timeout for sidekiq' do context 'when in foreground' do
allow(Gitlab::Runtime).to receive(:sidekiq?).and_return(true) it 'utilizes timeout for web' do
expect(Timeout).to receive(:timeout).with(described_class::RENDERED_TIMEOUT_BACKGROUND).and_call_original 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 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