Skip to content

Commit 5749073

Browse files
committed
Fix tests
1 parent aa9925e commit 5749073

3 files changed

Lines changed: 104 additions & 70 deletions

File tree

config/environments/test.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,6 @@
5151

5252
# Raise error when a before_action's only/except options reference missing actions.
5353
config.action_controller.raise_on_missing_callback_actions = true
54+
55+
config.active_job.queue_adapter = :test
5456
end

test/lib/prometheus/que_stats_test.rb

Lines changed: 74 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -12,73 +12,89 @@ class Prometheus::QueStatsTest < ActiveSupport::TestCase
1212
Prometheus::QueStats.read_only_transaction = @_readonly_transaction
1313
end
1414

15+
def with_que_adapter(&block)
16+
ApplicationJob.stub(:queue_adapter, ActiveJob::QueueAdapters::QueAdapter.new, &block)
17+
end
18+
1519
test 'worker stats' do
1620
assert Prometheus::QueStats::WorkerStats.new.call
1721
end
1822

1923
test 'job stats' do
20-
Que.stop!
21-
ApplicationJob.perform_later
22-
assert_equal 1, stats_count
23-
assert_equal 1, stats_count(where: ['1 > 0'])
24-
assert_equal 1, stats_count(where: ['1 > 0', '2 > 1'])
25-
assert_equal 0, stats_count(where: ['1 > 0', '2 < 1'])
24+
with_que_adapter do
25+
Que.stop!
26+
ApplicationJob.perform_later
27+
assert_equal 1, stats_count
28+
assert_equal 1, stats_count(where: ['1 > 0'])
29+
assert_equal 1, stats_count(where: ['1 > 0', '2 > 1'])
30+
assert_equal 0, stats_count(where: ['1 > 0', '2 < 1'])
31+
end
2632
end
2733

2834
test 'ready jobs stats' do
29-
Que.stop!
30-
assert_equal 0, stats_count(type: :ready)
31-
jobs = Array.new(3) { ApplicationJob.perform_later }
32-
jobs << ApplicationJob.set(wait_until: 1.day.from_now).perform_later
33-
assert_equal 3, stats_count(type: :ready)
34-
update_job(jobs[0], error_count: 1)
35-
assert_equal 2, stats_count(type: :ready)
36-
update_job(jobs[1], expired_at: 1.minute.ago)
37-
assert_equal 1, stats_count(type: :ready)
38-
update_job(jobs[2], finished_at: 1.minute.ago)
39-
assert_equal 0, stats_count(type: :ready)
35+
with_que_adapter do
36+
Que.stop!
37+
assert_equal 0, stats_count(type: :ready)
38+
jobs = Array.new(3) { ApplicationJob.perform_later }
39+
jobs << ApplicationJob.set(wait_until: 1.day.from_now).perform_later
40+
assert_equal 3, stats_count(type: :ready)
41+
update_job(jobs[0], error_count: 1)
42+
assert_equal 2, stats_count(type: :ready)
43+
update_job(jobs[1], expired_at: 1.minute.ago)
44+
assert_equal 1, stats_count(type: :ready)
45+
update_job(jobs[2], finished_at: 1.minute.ago)
46+
assert_equal 0, stats_count(type: :ready)
47+
end
4048
end
4149

4250
test 'scheduled jobs stats' do
43-
Que.stop!
44-
assert_equal 0, stats_count(type: :scheduled)
45-
jobs = [ApplicationJob, ApplicationJob.set(wait_until: 1.day.from_now), ApplicationJob.set(wait_until: 2.days.from_now)].map(&:perform_later)
46-
assert_equal 2, stats_count(type: :scheduled)
47-
update_job(jobs[1], error_count: 16, expired_at: 1.minute.ago)
48-
assert_equal 1, stats_count(type: :scheduled)
49-
update_job(jobs.last, run_at: 1.minute.ago)
50-
assert_equal 0, stats_count(type: :scheduled)
51+
with_que_adapter do
52+
Que.stop!
53+
assert_equal 0, stats_count(type: :scheduled)
54+
jobs = [ApplicationJob, ApplicationJob.set(wait_until: 1.day.from_now), ApplicationJob.set(wait_until: 2.days.from_now)].map(&:perform_later)
55+
assert_equal 2, stats_count(type: :scheduled)
56+
update_job(jobs[1], error_count: 16, expired_at: 1.minute.ago)
57+
assert_equal 1, stats_count(type: :scheduled)
58+
update_job(jobs.last, run_at: 1.minute.ago)
59+
assert_equal 0, stats_count(type: :scheduled)
60+
end
5161
end
5262

5363
test 'finished jobs stats' do
54-
Que.stop!
55-
assert_equal 0, stats_count(type: :finished)
56-
jobs = Array.new(2) { ApplicationJob.perform_later }
57-
assert_equal 0, stats_count(type: :finished)
58-
update_job(jobs.first, finished_at: Time.now)
59-
assert_equal 1, stats_count(type: :finished)
64+
with_que_adapter do
65+
Que.stop!
66+
assert_equal 0, stats_count(type: :finished)
67+
jobs = Array.new(2) { ApplicationJob.perform_later }
68+
assert_equal 0, stats_count(type: :finished)
69+
update_job(jobs.first, finished_at: Time.now)
70+
assert_equal 1, stats_count(type: :finished)
71+
end
6072
end
6173

6274
test 'failed jobs stats' do
63-
Que.stop!
64-
assert_equal 0, stats_count(type: :failed)
65-
jobs = Array.new(2) { ApplicationJob.perform_later }
66-
assert_equal 0, stats_count(type: :failed)
67-
update_job(jobs.first, error_count: 1)
68-
assert_equal 1, stats_count(type: :failed)
69-
update_job(jobs.first, error_count: 15)
70-
assert_equal 1, stats_count(type: :failed)
71-
update_job(jobs.first, error_count: 16, expired_at: Time.now.utc)
72-
assert_equal 0, stats_count(type: :failed)
75+
with_que_adapter do
76+
Que.stop!
77+
assert_equal 0, stats_count(type: :failed)
78+
jobs = Array.new(2) { ApplicationJob.perform_later }
79+
assert_equal 0, stats_count(type: :failed)
80+
update_job(jobs.first, error_count: 1)
81+
assert_equal 1, stats_count(type: :failed)
82+
update_job(jobs.first, error_count: 15)
83+
assert_equal 1, stats_count(type: :failed)
84+
update_job(jobs.first, error_count: 16, expired_at: Time.now.utc)
85+
assert_equal 0, stats_count(type: :failed)
86+
end
7387
end
7488

7589
test 'expired jobs stats' do
76-
Que.stop!
77-
assert_equal 0, stats_count(type: :expired)
78-
jobs = Array.new(2) { ApplicationJob.perform_later }
79-
assert_equal 0, stats_count(type: :expired)
80-
update_job(jobs.first, error_count: 16, expired_at: Time.now.utc)
81-
assert_equal 1, stats_count(type: :expired)
90+
with_que_adapter do
91+
Que.stop!
92+
assert_equal 0, stats_count(type: :expired)
93+
jobs = Array.new(2) { ApplicationJob.perform_later }
94+
assert_equal 0, stats_count(type: :expired)
95+
update_job(jobs.first, error_count: 16, expired_at: Time.now.utc)
96+
assert_equal 1, stats_count(type: :expired)
97+
end
8298
end
8399

84100
class WithTransaction < ActiveSupport::TestCase
@@ -91,20 +107,22 @@ def test_readonly_transaction
91107
end
92108

93109
test 'serialize metrics' do
94-
Que.stop!
110+
with_que_adapter do
111+
Que.stop!
95112

96-
job = ApplicationJob.new
97-
job.enqueue
113+
job = ApplicationJob.new
114+
job.enqueue
98115

99-
job.scheduled_at = 1.day.ago
100-
job.enqueue
116+
job.scheduled_at = 1.day.ago
117+
job.enqueue
101118

102-
job.executions = 1
103-
job.enqueue
119+
job.executions = 1
120+
job.enqueue
104121

105-
Yabeda.collectors.each(&:call)
122+
Yabeda.collectors.each(&:call)
106123

107-
assert Prometheus::Client::Formats::Text.marshal(Yabeda::Prometheus.registry)
124+
assert Prometheus::Client::Formats::Text.marshal(Yabeda::Prometheus.registry)
125+
end
108126
end
109127

110128
protected

test/models/model_test.rb

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,40 @@
22
require 'test_helper'
33

44
class ModelTest < ActiveSupport::TestCase
5+
self.use_transactional_tests = false
6+
57
def test_weak_lock
6-
locking_connection = Model.connection_pool.checkout
8+
model = Model.first!
9+
10+
11+
# The events in the two threads must happen in a particular order:
12+
# 1. The new thread must lock the record and wait. This way the lock
13+
# and the transaction are kept open.
14+
# 2. The main thread must wait until the lock is taken, only then it can
15+
# try to take the lock
16+
# We use queues to signal threads
17+
locked_signal = Queue.new
18+
release_signal = Queue.new
719

8-
fiber = Model.stub(:connection, locking_connection) do
9-
Fiber.new do
10-
locking_connection.transaction do
11-
Fiber.yield Model.first!.weak_lock
12-
end
20+
# Take the lock and keep it taken
21+
thread = Thread.new do
22+
Model.transaction do
23+
model.weak_lock
24+
locked_signal.push(true)
25+
release_signal.pop
1326
end
1427
end
1528

16-
locked_model = fiber.resume
17-
refute_equal Model.connection, locking_connection
29+
# Wait till the lock is taken
30+
locked_signal.pop
1831

19-
connection = Model.connection_pool.checkout
20-
21-
Model.stub(:connection, connection) do
22-
assert_raises Model::LockTimeoutError do
23-
Model.find(locked_model.id).weak_lock
24-
end
32+
# Try to take the lock, we expect it to fail
33+
assert_raises Model::LockTimeoutError do
34+
model.weak_lock
2535
end
36+
37+
# Release the thread
38+
release_signal.push(true)
39+
thread.join
2640
end
2741
end

0 commit comments

Comments
 (0)