Skip to content

Commit dea0914

Browse files
committed
fix: resolve RSpec/SubjectStub offense in connection pool config check spec
1 parent bc6c96f commit dea0914

2 files changed

Lines changed: 27 additions & 22 deletions

File tree

lib/delayed/worker.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,10 @@ def check_connection_pool_config!
247247
return unless pool_size
248248
return if self.class.max_claims < pool_size
249249

250-
say "WARNING: max_claims (#{self.class.max_claims}) >= DB connection pool size (#{pool_size}). " \
251-
"The worker process needs at least 1 connection for its own housekeeping, so job threads may " \
252-
"starve waiting for a connection. Set Delayed::Worker.max_claims to at most #{[pool_size - 1, 1].max}.", 'warn'
250+
say "WARNING: Delayed::Worker.max_claims (#{self.class.max_claims}) >= ActiveRecord connection pool size (#{pool_size}). " \
251+
"The worker process itself also needs a connection for polling and locking, so at least one job thread " \
252+
"will likely fail with ActiveRecord::ConnectionTimeoutError. " \
253+
"Set Delayed::Worker.max_claims to #{[pool_size - 1, 1].max} or less, or increase your connection pool size.", 'warn'
253254
rescue StandardError
254255
nil
255256
end

spec/worker_spec.rb

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,6 @@
2323
describe 'connection pool config check' do
2424
let(:connection_pool) { instance_double(ActiveRecord::ConnectionAdapters::ConnectionPool, size: pool_size) }
2525

26-
before do
27-
subject.send(:stop) # prevent start from running more than one loop
28-
allow(Delayed::Job).to receive(:reserve).and_return([])
29-
allow(Delayed::Job).to receive(:connection_pool).and_return(connection_pool)
30-
allow(Delayed::Job).to receive(:clear_locks!)
31-
allow(subject).to receive(:say).and_call_original
32-
end
33-
3426
around do |example|
3527
max_claims_was = described_class.max_claims
3628
described_class.max_claims = max_claims
@@ -39,17 +31,27 @@
3931
described_class.max_claims = max_claims_was
4032
end
4133

34+
before do
35+
allow(Delayed.logger).to receive(:warn)
36+
subject.send(:stop) # prevent start from running more than one loop
37+
allow(Delayed::Job).to receive(:reserve).and_return([])
38+
allow(Delayed::Job).to receive(:connection_pool).and_return(connection_pool)
39+
allow(Delayed::Job).to receive(:clear_locks!)
40+
end
41+
4242
context 'when max_claims equals pool size' do
4343
let(:max_claims) { 5 }
4444
let(:pool_size) { 5 }
4545

4646
it 'logs a warning at startup' do
4747
subject.start
48-
expect(subject).to have_received(:say).with(
49-
'WARNING: max_claims (5) >= DB connection pool size (5). ' \
50-
'The worker process needs at least 1 connection for its own housekeeping, so job threads may ' \
51-
'starve waiting for a connection. Set Delayed::Worker.max_claims to at most 4.',
52-
'warn',
48+
expect(Delayed.logger).to have_received(:warn).with(
49+
include(
50+
'WARNING: Delayed::Worker.max_claims (5) >= ActiveRecord connection pool size (5). ' \
51+
'The worker process itself also needs a connection for polling and locking, so at least one job thread ' \
52+
'will likely fail with ActiveRecord::ConnectionTimeoutError. ' \
53+
'Set Delayed::Worker.max_claims to 4 or less, or increase your connection pool size.',
54+
),
5355
)
5456
end
5557
end
@@ -60,11 +62,13 @@
6062

6163
it 'logs a warning at startup' do
6264
subject.start
63-
expect(subject).to have_received(:say).with(
64-
'WARNING: max_claims (6) >= DB connection pool size (5). ' \
65-
'The worker process needs at least 1 connection for its own housekeeping, so job threads may ' \
66-
'starve waiting for a connection. Set Delayed::Worker.max_claims to at most 4.',
67-
'warn',
65+
expect(Delayed.logger).to have_received(:warn).with(
66+
include(
67+
'WARNING: Delayed::Worker.max_claims (6) >= ActiveRecord connection pool size (5). ' \
68+
'The worker process itself also needs a connection for polling and locking, so at least one job thread ' \
69+
'will likely fail with ActiveRecord::ConnectionTimeoutError. ' \
70+
'Set Delayed::Worker.max_claims to 4 or less, or increase your connection pool size.',
71+
),
6872
)
6973
end
7074
end
@@ -75,7 +79,7 @@
7579

7680
it 'does not log a warning' do
7781
subject.start
78-
expect(subject).not_to have_received(:say).with(/WARNING/i, 'warn')
82+
expect(Delayed.logger).not_to have_received(:warn).with(/WARNING/i)
7983
end
8084
end
8185

0 commit comments

Comments
 (0)