@@ -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
0 commit comments