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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
|
require_relative 'global_thread_pool_shared'
RSpec.shared_examples :executor_service do
after(:each) do
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
end
it_should_behave_like :global_thread_pool
context '#post' do
it 'rejects the block while shutting down' do
latch = Concurrent::CountDownLatch.new(1)
subject.post{ sleep(1) }
subject.shutdown
begin
subject.post{ latch.count_down }
rescue Concurrent::RejectedExecutionError
end
expect(latch.wait(0.1)).to be_falsey
end
it 'rejects the block once shutdown' do
subject.shutdown
latch = Concurrent::CountDownLatch.new(1)
begin
subject.post{ latch.count_down }
rescue Concurrent::RejectedExecutionError
end
expect(latch.wait(0.1)).to be_falsey
end
end
context 'auto terminate' do
# https://github.com/ruby-concurrency/concurrent-ruby/issues/817
# https://github.com/ruby-concurrency/concurrent-ruby/issues/839
it 'does not stop shutdown ' do
Timeout.timeout(10) do
begin
test_file = File.join File.dirname(__FILE__), 'executor_quits.rb'
pid = spawn RbConfig.ruby, test_file
Process.waitpid pid
expect($?.success?).to eq true
rescue Errno::ECHILD
# child already gone
rescue Timeout::Error => e
Process.kill :KILL, pid
raise e
end
end
end
end
context '#running?' do
it 'returns true when the thread pool is running' do
expect(subject).to be_running
end
it 'returns false when the thread pool is shutting down' do
subject.post{ sleep(0.5) }
subject.shutdown
expect(subject).not_to be_running
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
end
it 'returns false when the thread pool is shutdown' do
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(subject).not_to be_running
end
it 'returns false when the thread pool is killed' do
subject.kill
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(subject).not_to be_running
end
end
context '#shutdown' do
it 'stops accepting new tasks' do
latch1 = Concurrent::CountDownLatch.new(1)
latch2 = Concurrent::CountDownLatch.new(1)
subject.post{ sleep(0.1); latch1.count_down }
latch1.wait(1)
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
begin
subject.post{ latch2.count_down }
rescue Concurrent::RejectedExecutionError
end
expect(latch2.wait(0.2)).to be_falsey
end
it 'allows in-progress tasks to complete' do
latch = Concurrent::CountDownLatch.new(1)
subject.post{ sleep(0.1); latch.count_down }
subject.shutdown
expect(latch.wait(1)).to be_truthy
end
it 'allows pending tasks to complete' do
latch = Concurrent::CountDownLatch.new(2)
subject.post{ sleep(0.2); latch.count_down }
subject.post{ sleep(0.2); latch.count_down }
subject.shutdown
expect(latch.wait(1)).to be_truthy
end
end
context '#shutdown followed by #wait_for_termination' do
it 'allows in-progress tasks to complete' do
latch = Concurrent::CountDownLatch.new(1)
subject.post{ sleep(0.1); latch.count_down }
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(latch.wait(1)).to be_truthy
end
it 'allows pending tasks to complete' do
q = Queue.new
5.times do |i|
subject.post { sleep 0.1; q << i }
end
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(q.length).to eq 5
end
it 'stops accepting/running new tasks' do
expected = Concurrent::AtomicFixnum.new(0)
subject.post{ sleep(0.1); expected.increment }
subject.post{ sleep(0.1); expected.increment }
subject.shutdown
begin
subject.post{ expected.increment }
rescue Concurrent::RejectedExecutionError
end
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(expected.value).to eq(2)
end
end
context '#kill' do
it 'stops accepting new tasks' do
expected = Concurrent::AtomicBoolean.new(false)
latch = Concurrent::CountDownLatch.new(1)
subject.post{ sleep(0.1); latch.count_down }
latch.wait(1)
subject.kill
begin
subject.post{ expected.make_true }
rescue Concurrent::RejectedExecutionError
end
sleep(0.1)
expect(expected.value).to be_falsey
end
it 'rejects all pending tasks' do
subject.post{ sleep(1) }
sleep(0.1)
subject.kill
sleep(0.1)
begin
expect(subject.post{ nil }).to be_falsey
rescue Concurrent::RejectedExecutionError
end
end
end
context '#wait_for_termination' do
it 'immediately returns true when no operations are pending' do
subject.shutdown
expect(subject.wait_for_termination(0)).to be_truthy
end
it 'returns true after shutdown has complete' do
10.times { subject << proc{ nil } }
sleep(0.1)
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to be_truthy
end
it 'returns true when shutdown successfully completes before timeout' do
subject.post{ sleep(0.5) }
sleep(0.1)
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to be_truthy
end
it 'returns false when shutdown fails to complete before timeout' do
unless subject.serialized?
latch = Concurrent::CountDownLatch.new 1
100.times{ subject.post{ latch.wait } }
sleep(0.1)
subject.shutdown
expect(subject.wait_for_termination(0.01)).to be_falsey
latch.count_down
end
end
it 'waits forever when no timeout value is given' do
subject.post{ sleep(0.5) }
sleep(0.1)
subject.shutdown
expect(subject.wait_for_termination).to be_truthy
end
end
end
|