forked from resque/resque
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreaded_exector_pool_test.rb
More file actions
97 lines (77 loc) · 1.96 KB
/
threaded_exector_pool_test.rb
File metadata and controls
97 lines (77 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
require "test_helper"
module Resque
describe "ThreadedExecutorPool" do
class Actionable
attr_reader :ran
def initialize
@ran = false
end
def run
@ran = true
end
end
class WaitingJob
attr_reader :ran
def initialize(worker_latch, main_latch)
@worker_latch = worker_latch
@main_latch = main_latch
@ran = false
end
def run
@main_latch.release
@worker_latch.await
@ran = true
end
end
class SecondJob
attr_reader :ran
def initialize(latch)
@latch = latch
@ran = false
end
def run
@latch.release
@ran = true
end
end
it "can be constructed" do
assert ThreadedExecutorPool.new(::Queue.new, 1)
end
it "runs the job" do
pool = ThreadedExecutorPool.new(::Queue.new, 1)
job = Actionable.new
pool.execute(job)
pool.shutdown
assert job.ran
end
it "shuts down" do
pool = ThreadedExecutorPool.new(::Queue.new, 1)
job = Actionable.new
pool.shutdown
assert_raises(RejectedJob) { pool.execute(job) }
assert !job.ran
end
it "calls block when executing a job on a shutdown pool" do
a = 1
pool = ThreadedExecutorPool.new(::Queue.new, 1) { a = 2 }
job = Actionable.new
pool.shutdown
pool.execute(job)
assert !job.ran
assert_equal 2, a
end
it "runs the jobs concurrently" do
pool = ThreadedExecutorPool.new(::Queue.new, 2)
waiting_latch = Consumer::Latch.new
main_latch = Consumer::Latch.new
waiting_job = WaitingJob.new(waiting_latch, main_latch)
second_job = SecondJob.new(waiting_latch)
pool.execute(waiting_job)
Timeout.timeout(1) { main_latch.await }
pool.execute(second_job)
pool.shutdown
assert waiting_job.ran
assert second_job.ran
end
end
end