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

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'spec/models/hooks/web_hook_spec.rb')
-rw-r--r--spec/models/hooks/web_hook_spec.rb198
1 files changed, 192 insertions, 6 deletions
diff --git a/spec/models/hooks/web_hook_spec.rb b/spec/models/hooks/web_hook_spec.rb
index 413e69fb071..b528dbedd2c 100644
--- a/spec/models/hooks/web_hook_spec.rb
+++ b/spec/models/hooks/web_hook_spec.rb
@@ -3,7 +3,15 @@
require 'spec_helper'
RSpec.describe WebHook do
- let(:hook) { build(:project_hook) }
+ include AfterNextHelpers
+
+ let_it_be(:project) { create(:project) }
+
+ let(:hook) { build(:project_hook, project: project) }
+
+ around do |example|
+ freeze_time { example.run }
+ end
describe 'associations' do
it { is_expected.to have_many(:web_hook_logs) }
@@ -69,18 +77,30 @@ RSpec.describe WebHook do
let(:data) { { key: 'value' } }
let(:hook_name) { 'project hook' }
- before do
- expect(WebHookService).to receive(:new).with(hook, data, hook_name).and_call_original
+ it '#execute' do
+ expect_next(WebHookService).to receive(:execute)
+
+ hook.execute(data, hook_name)
end
- it '#execute' do
- expect_any_instance_of(WebHookService).to receive(:execute)
+ it 'does not execute non-executable hooks' do
+ hook.update!(disabled_until: 1.day.from_now)
+
+ expect(WebHookService).not_to receive(:new)
hook.execute(data, hook_name)
end
it '#async_execute' do
- expect_any_instance_of(WebHookService).to receive(:async_execute)
+ expect_next(WebHookService).to receive(:async_execute)
+
+ hook.async_execute(data, hook_name)
+ end
+
+ it 'does not async execute non-executable hooks' do
+ hook.update!(disabled_until: 1.day.from_now)
+
+ expect(WebHookService).not_to receive(:new)
hook.async_execute(data, hook_name)
end
@@ -94,4 +114,170 @@ RSpec.describe WebHook do
expect { web_hook.destroy! }.to change(web_hook.web_hook_logs, :count).by(-3)
end
end
+
+ describe '.executable' do
+ let(:not_executable) do
+ [
+ [0, Time.current],
+ [0, 1.minute.from_now],
+ [1, 1.minute.from_now],
+ [3, 1.minute.from_now],
+ [4, nil],
+ [4, 1.day.ago],
+ [4, 1.minute.from_now]
+ ].map do |(recent_failures, disabled_until)|
+ create(:project_hook, project: project, recent_failures: recent_failures, disabled_until: disabled_until)
+ end
+ end
+
+ let(:executables) do
+ [
+ [0, nil],
+ [0, 1.day.ago],
+ [1, nil],
+ [1, 1.day.ago],
+ [3, nil],
+ [3, 1.day.ago]
+ ].map do |(recent_failures, disabled_until)|
+ create(:project_hook, project: project, recent_failures: recent_failures, disabled_until: disabled_until)
+ end
+ end
+
+ it 'finds the correct set of project hooks' do
+ expect(described_class.where(project_id: project.id).executable).to match_array executables
+ end
+
+ context 'when the feature flag is not enabled' do
+ before do
+ stub_feature_flags(web_hooks_disable_failed: false)
+ end
+
+ it 'is the same as all' do
+ expect(described_class.where(project_id: project.id).executable).to match_array(executables + not_executable)
+ end
+ end
+ end
+
+ describe '#executable?' do
+ let(:web_hook) { create(:project_hook, project: project) }
+
+ where(:recent_failures, :not_until, :executable) do
+ [
+ [0, :not_set, true],
+ [0, :past, true],
+ [0, :future, false],
+ [0, :now, false],
+ [1, :not_set, true],
+ [1, :past, true],
+ [1, :future, false],
+ [3, :not_set, true],
+ [3, :past, true],
+ [3, :future, false],
+ [4, :not_set, false],
+ [4, :past, false],
+ [4, :future, false]
+ ]
+ end
+
+ with_them do
+ # Phasing means we cannot put these values in the where block,
+ # which is not subject to the frozen time context.
+ let(:disabled_until) do
+ case not_until
+ when :not_set
+ nil
+ when :past
+ 1.minute.ago
+ when :future
+ 1.minute.from_now
+ when :now
+ Time.current
+ end
+ end
+
+ before do
+ web_hook.update!(recent_failures: recent_failures, disabled_until: disabled_until)
+ end
+
+ it 'has the correct state' do
+ expect(web_hook.executable?).to eq(executable)
+ end
+
+ context 'when the feature flag is enabled for a project' do
+ before do
+ stub_feature_flags(web_hooks_disable_failed: project)
+ end
+
+ it 'has the expected value' do
+ expect(web_hook.executable?).to eq(executable)
+ end
+ end
+
+ context 'when the feature flag is not enabled' do
+ before do
+ stub_feature_flags(web_hooks_disable_failed: false)
+ end
+
+ it 'is executable' do
+ expect(web_hook).to be_executable
+ end
+ end
+ end
+ end
+
+ describe '#next_backoff' do
+ context 'when there was no last backoff' do
+ before do
+ hook.backoff_count = 0
+ end
+
+ it 'is 10 minutes' do
+ expect(hook.next_backoff).to eq(described_class::INITIAL_BACKOFF)
+ end
+ end
+
+ context 'when we have backed off once' do
+ before do
+ hook.backoff_count = 1
+ end
+
+ it 'is twice the initial value' do
+ expect(hook.next_backoff).to eq(20.minutes)
+ end
+ end
+
+ context 'when we have backed off 3 times' do
+ before do
+ hook.backoff_count = 3
+ end
+
+ it 'grows exponentially' do
+ expect(hook.next_backoff).to eq(80.minutes)
+ end
+ end
+
+ context 'when the previous backoff was large' do
+ before do
+ hook.backoff_count = 8 # last value before MAX_BACKOFF
+ end
+
+ it 'does not exceed the max backoff value' do
+ expect(hook.next_backoff).to eq(described_class::MAX_BACKOFF)
+ end
+ end
+ end
+
+ describe '#enable!' do
+ it 'makes a hook executable' do
+ hook.recent_failures = 1000
+
+ expect { hook.enable! }.to change(hook, :executable?).from(false).to(true)
+ end
+ end
+
+ describe '#disable!' do
+ it 'disables a hook' do
+ expect { hook.disable! }.to change(hook, :executable?).from(true).to(false)
+ end
+ end
end