@@ -19,6 +19,40 @@ class ThrottleTestJob < ActiveJob::Base
1919 throttle threshold : 2 , period : 1 . second , drop : true
2020
2121 def perform
22+ sleep 0.5
23+ $count += 1
24+ end
25+ end
26+
27+ class ThrottleWithKeyTestJob < ActiveJob ::Base
28+ include ActiveJob ::TrafficControl ::Throttle
29+
30+ throttle threshold : 2 , period : 1 . second , drop : true , key : "throttle_test_key"
31+
32+ def perform
33+ sleep 0.5
34+ $count += 1
35+ end
36+ end
37+
38+ class ThrottleWithProcKeyTestJob < ActiveJob ::Base
39+ include ActiveJob ::TrafficControl ::Throttle
40+
41+ throttle threshold : 2 , period : 1 . second , drop : true , key : -> ( _ ) { "throttle_proc_job_name" }
42+
43+ def perform
44+ sleep 0.5
45+ $count += 1
46+ end
47+ end
48+
49+ class ThrottleNotDroppedTestJob < ActiveJob ::Base
50+ include ActiveJob ::TrafficControl ::Throttle
51+
52+ throttle threshold : 2 , period : 1 . second , drop : false
53+
54+ def perform
55+ sleep 0.5
2256 $count += 1
2357 end
2458 end
@@ -34,6 +68,39 @@ def perform
3468 end
3569 end
3670
71+ class ConcurrencyNotDroppedTestJob < ActiveJob ::Base
72+ include ActiveJob ::TrafficControl ::Concurrency
73+
74+ concurrency 1 , drop : false
75+
76+ def perform
77+ sleep 0.5
78+ $count += 1
79+ end
80+ end
81+
82+ class ConcurrencyWithKeyTestJob < ActiveJob ::Base
83+ include ActiveJob ::TrafficControl ::Concurrency
84+
85+ concurrency 1 , drop : true , key : "concurrency_test_key"
86+
87+ def perform
88+ sleep 0.5
89+ $count += 1
90+ end
91+ end
92+
93+ class ConcurrencyWithProcKeyTestJob < ActiveJob ::Base
94+ include ActiveJob ::TrafficControl ::Concurrency
95+
96+ concurrency 1 , drop : true , key : -> ( _ ) { "concurrency_proc_job_name" }
97+
98+ def perform
99+ sleep 0.5
100+ $count += 1
101+ end
102+ end
103+
37104 class InheritedConcurrencyJob < ConcurrencyTestJob
38105 def perform
39106 $count += 1
@@ -70,33 +137,80 @@ def test_disable
70137 assert_equal 2 , $count
71138 end
72139
73- def test_throttle
74- t1 = Thread . new { ThrottleTestJob . perform_now }
75- t2 = Thread . new { ThrottleTestJob . perform_now }
76- t3 = Thread . new { ThrottleTestJob . perform_now }
140+ def throttle_helper ( klass )
141+ t1 = Thread . new { klass . perform_now }
142+ t2 = Thread . new { klass . perform_now }
143+ t3 = Thread . new { klass . perform_now }
77144 [ t1 , t2 , t3 ] . map ( &:join )
145+ sleep 0.5
78146 assert_equal 2 , $count
79147 sleep 1
80- ThrottleTestJob . perform_now
148+ klass . perform_now
149+ assert_equal 3 , $count
150+ end
151+
152+ def test_throttle
153+ throttle_helper ( ThrottleTestJob )
154+ end
155+
156+ def test_throttle_with_key
157+ throttle_helper ( ThrottleWithKeyTestJob )
158+ end
159+
160+ def test_throttle_with_proc_key
161+ throttle_helper ( ThrottleWithProcKeyTestJob )
162+ end
163+
164+ def test_throttle_not_dropped
165+ return unless ActiveJob ::Base . queue_adapter == :async
166+
167+ t1 = Thread . new { ThrottleNotDroppedTestJob . perform_now }
168+ t2 = Thread . new { ThrottleNotDroppedTestJob . perform_now }
169+ t3 = Thread . new { ThrottleNotDroppedTestJob . perform_now }
170+ [ t1 , t2 , t3 ] . map ( &:join )
171+ sleep 0.5
172+ assert_equal 2 , $count
173+ sleep 6
81174 assert_equal 3 , $count
82175 end
83176
177+ def concurrency_helper ( klass )
178+ t1 = Thread . new { klass . perform_now }
179+ t2 = Thread . new { klass . perform_now }
180+ [ t1 , t2 ] . map ( &:join )
181+ sleep 0.5
182+ assert_equal 1 , $count
183+ klass . perform_now
184+ assert_equal 2 , $count
185+ end
186+
84187 def test_concurrency
85- t1 = Thread . new { ConcurrencyTestJob . perform_now }
86- t2 = Thread . new { ConcurrencyTestJob . perform_now }
188+ concurrency_helper ( ConcurrencyTestJob )
189+ end
190+
191+ def test_concurrency_with_key
192+ concurrency_helper ( ConcurrencyWithKeyTestJob )
193+ end
194+
195+ def test_concurrency_with_proc_key
196+ concurrency_helper ( ConcurrencyWithProcKeyTestJob )
197+ end
198+
199+ def test_concurrent_not_dropped
200+ return unless ActiveJob ::Base . queue_adapter == :async
201+
202+ t1 = Thread . new { ConcurrencyNotDroppedTestJob . perform_now }
203+ t2 = Thread . new { ConcurrencyNotDroppedTestJob . perform_now }
87204 [ t1 , t2 ] . map ( &:join )
88- sleep 1
89205 assert_equal 1 , $count
90- ConcurrencyTestJob . perform_later
91- sleep 1
206+ sleep 6
92207 assert_equal 2 , $count
93208 end
94209
95210 def test_concurrency_is_not_inherited
96- t1 = Thread . new { InheritedConcurrencyJob . perform_later }
97- t2 = Thread . new { InheritedConcurrencyJob . perform_later }
211+ t1 = Thread . new { InheritedConcurrencyJob . perform_now }
212+ t2 = Thread . new { InheritedConcurrencyJob . perform_now }
98213 [ t1 , t2 ] . map ( &:join )
99- sleep 1
100214 assert_equal 2 , $count
101215 end
102216
0 commit comments