diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-31 12:08:53 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-01-31 12:08:53 +0300 |
commit | fd3a95f07ae9cd78fecffcfa5de4494f933a7808 (patch) | |
tree | a38a8abb0afb14aa396edd30137ddf45e71d2713 /spec/workers | |
parent | 6a7005feed2e88568f42627e7190ff5c4f2aa8d3 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'spec/workers')
-rw-r--r-- | spec/workers/concerns/worker_context_spec.rb | 78 |
1 files changed, 77 insertions, 1 deletions
diff --git a/spec/workers/concerns/worker_context_spec.rb b/spec/workers/concerns/worker_context_spec.rb index a7d0ba2b8bd..97a88eecd73 100644 --- a/spec/workers/concerns/worker_context_spec.rb +++ b/spec/workers/concerns/worker_context_spec.rb @@ -5,7 +5,11 @@ require 'spec_helper' describe WorkerContext do let(:worker) do Class.new do - include WorkerContext + def self.name + "TestWorker" + end + + include ApplicationWorker end end @@ -24,6 +28,78 @@ describe WorkerContext do end end + shared_examples 'tracking bulk scheduling contexts' do + describe "context contents" do + before do + # stub clearing the contexts, so we can check what's inside + allow(worker).to receive(:batch_context=).and_call_original + allow(worker).to receive(:batch_context=).with(nil) + end + + it 'keeps track of the context per key to schedule' do + subject + + expect(worker.context_for_arguments(["hello"])).to be_a(Gitlab::ApplicationContext) + end + + it 'does not share contexts across threads' do + t1_context = nil + t2_context = nil + + Thread.new do + subject + + t1_context = worker.context_for_arguments(["hello"]) + end.join + Thread.new do + t2_context = worker.context_for_arguments(["hello"]) + end.join + + expect(t1_context).to be_a(Gitlab::ApplicationContext) + expect(t2_context).to be_nil + end + end + + it 'clears the contexts' do + subject + + expect(worker.__send__(:batch_context)).to be_nil + end + end + + describe '.bulk_perform_async_with_contexts' do + subject do + worker.bulk_perform_async_with_contexts(%w(hello world), + context_proc: -> (_) { { user: build_stubbed(:user) } }, + arguments_proc: -> (word) { word }) + end + + it 'calls bulk_perform_async with the arguments' do + expect(worker).to receive(:bulk_perform_async).with([["hello"], ["world"]]) + + subject + end + + it_behaves_like 'tracking bulk scheduling contexts' + end + + describe '.bulk_perform_in_with_contexts' do + subject do + worker.bulk_perform_in_with_contexts(10.minutes, + %w(hello world), + context_proc: -> (_) { { user: build_stubbed(:user) } }, + arguments_proc: -> (word) { word }) + end + + it 'calls bulk_perform_in with the arguments and delay' do + expect(worker).to receive(:bulk_perform_in).with(10.minutes, [["hello"], ["world"]]) + + subject + end + + it_behaves_like 'tracking bulk scheduling contexts' + end + describe '#with_context' do it 'allows modifying context when the job is running' do worker.new.with_context(user: build_stubbed(:user, username: 'jane-doe')) do |