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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
|
require_relative 'thread_pool_shared'
RSpec.shared_examples :thread_pool_executor do
after(:each) do
subject.shutdown
subject.wait_for_termination(pool_termination_timeout)
end
context '#initialize defaults' do
subject { described_class.new }
it 'defaults :min_length to DEFAULT_MIN_POOL_SIZE' do
expect(subject.min_length).to eq described_class::DEFAULT_MIN_POOL_SIZE
end
it 'defaults :max_length to DEFAULT_MAX_POOL_SIZE' do
expect(subject.max_length).to eq described_class::DEFAULT_MAX_POOL_SIZE
end
it 'defaults :idletime to DEFAULT_THREAD_IDLETIMEOUT' do
expect(subject.idletime).to eq described_class::DEFAULT_THREAD_IDLETIMEOUT
end
it 'defaults :max_queue to DEFAULT_MAX_QUEUE_SIZE' do
expect(subject.max_queue).to eq described_class::DEFAULT_MAX_QUEUE_SIZE
end
it 'defaults :fallback_policy to :abort' do
expect(subject.fallback_policy).to eq :abort
end
it 'defaults :name to nil' do
expect(subject.name).to be_nil
end
end
context "#initialize explicit values" do
it "sets :min_threads" do
pool = described_class.new(min_threads: 2)
expect(pool.min_length).to eq 2
pool.shutdown
expect(pool.wait_for_termination(pool_termination_timeout)).to eq true
end
it "sets :max_threads" do
pool = described_class.new(max_threads: 2)
expect(pool.max_length).to eq 2
pool.shutdown
expect(pool.wait_for_termination(pool_termination_timeout)).to eq true
end
it "sets :idletime" do
pool = described_class.new(idletime: 2)
expect(pool.idletime).to eq 2
pool.shutdown
expect(pool.wait_for_termination(pool_termination_timeout)).to eq true
end
it "doesn't allow max_threads < min_threads" do
expect {
described_class.new(min_threads: 2, max_threads: 1)
}.to raise_error(ArgumentError)
end
it 'accepts all valid fallback policies' do
Concurrent::RubyThreadPoolExecutor::FALLBACK_POLICIES.each do |policy|
subject = described_class.new(fallback_policy: policy)
expect(subject.fallback_policy).to eq policy
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
end
end
it 'raises an exception if :max_threads is less than zero' do
expect {
described_class.new(max_threads: -1)
}.to raise_error(ArgumentError)
end
it 'raises an exception if :min_threads is less than zero' do
expect {
described_class.new(min_threads: -1)
}.to raise_error(ArgumentError)
end
it 'raises an exception if :max_threads greater than the max allowable' do
expect {
described_class.new(max_threads: described_class::DEFAULT_MAX_POOL_SIZE+1)
}.to raise_error(ArgumentError)
end
it 'raises an exception if :max_threads is less than :min_threads' do
expect {
described_class.new(max_threads: 1, min_threads: 100)
}.to raise_error(ArgumentError)
end
it 'raises an exception if given an invalid :fallback_policy' do
expect {
described_class.new(fallback_policy: :bogus)
}.to raise_error(ArgumentError)
end
it 'sets :name' do
pool = described_class.new(name: 'MyPool')
expect(pool.name).to eq 'MyPool'
pool.shutdown
pool.wait_for_termination(pool_termination_timeout)
end
end
context '#max_queue' do
let!(:expected_max){ 100 }
subject{ described_class.new(max_queue: expected_max) }
it 'returns the set value on creation' do
expect(subject.max_queue).to eq expected_max
end
it 'returns the set value when running', :truffle_bug => true do # only actually fails for RubyThreadPoolExecutor
trigger = Concurrent::Event.new
5.times{ subject.post{ trigger.wait } }
expect(subject.max_queue).to eq expected_max
trigger.set
end
it 'returns the set value after stopping' do
5.times{ subject.post{ nil } }
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(subject.max_queue).to eq expected_max
end
end
context '#queue_length', :truffle_bug => true do # only actually fails for RubyThreadPoolExecutor
let!(:expected_max){ 10 }
subject do
described_class.new(
min_threads: 2,
max_threads: 5,
max_queue: expected_max,
fallback_policy: :discard
)
end
it 'returns zero on creation' do
expect(subject.queue_length).to eq 0
end
it 'returns zero when there are no enqueued tasks' do
latch = Concurrent::CountDownLatch.new(5)
5.times{ subject.post{ latch.count_down } }
latch.wait(0.1)
expect(subject.queue_length).to eq 0
end
it 'returns the size of the queue when tasks are enqueued' do
trigger = Concurrent::Event.new
20.times{ subject.post{ trigger.wait } }
expect(subject.queue_length).to be > 0
trigger.set
end
it 'returns zero when stopped' do
trigger = Concurrent::Event.new
20.times{ subject.post{ trigger.wait } }
subject.shutdown
trigger.set
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(subject.queue_length).to eq 0
end
it 'can never be greater than :max_queue' do
trigger = Concurrent::Event.new
20.times{ subject.post{ trigger.wait } }
expect(subject.queue_length).to be <= expected_max
trigger.set
end
end
context '#remaining_capacity' do
let!(:expected_max){ 100 }
subject{ described_class.new(max_queue: expected_max) }
it 'returns -1 when :max_queue is set to zero' do
executor = described_class.new(max_queue: 0)
expect(executor.remaining_capacity).to eq(-1)
executor.shutdown
expect(executor.wait_for_termination(pool_termination_timeout)).to eq true
end
it 'returns :max_length on creation' do
expect(subject.remaining_capacity).to eq expected_max
end
it 'returns :max_length when stopped' do
100.times{ subject.post{ nil } }
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(subject.remaining_capacity).to eq expected_max
end
end
context '#fallback_policy' do
let!(:min_threads){ 1 }
let!(:max_threads){ 1 }
let!(:idletime){ 60 }
let!(:max_queue){ 1 }
context ':abort' do
subject do
described_class.new(
min_threads: min_threads,
max_threads: max_threads,
idletime: idletime,
max_queue: max_queue,
fallback_policy: :abort
)
end
specify '#post raises an error when the queue is at capacity' do
trigger = Concurrent::Event.new
expect {
20.times{ subject.post{ trigger.wait } }
}.to raise_error(Concurrent::RejectedExecutionError)
trigger.set
end
specify '#<< raises an error when the queue is at capacity' do
trigger = Concurrent::Event.new
expect {
20.times{ subject << proc { trigger.wait } }
}.to raise_error(Concurrent::RejectedExecutionError)
trigger.set
end
specify '#post raises an error when the executor is shutting down' do
trigger = Concurrent::Event.new
expect {
subject.shutdown; subject.post{ trigger.wait }
}.to raise_error(Concurrent::RejectedExecutionError)
trigger.set
end
specify '#<< raises an error when the executor is shutting down' do
trigger = Concurrent::Event.new
expect {
subject.shutdown; subject << proc { trigger.wait }
}.to raise_error(Concurrent::RejectedExecutionError)
trigger.set
end
specify 'a #post task is never executed when the queue is at capacity' do
all_tasks_posted = Concurrent::Event.new
latch = Concurrent::CountDownLatch.new(max_threads)
initial_executed = Concurrent::AtomicFixnum.new(0)
subsequent_executed = Concurrent::AtomicFixnum.new(0)
# Fill up all the threads (with a task that won't complete until
# all tasks are posted)
max_threads.times do
subject.post{ latch.count_down; all_tasks_posted.wait ; initial_executed.increment;}
end
# Wait for all those tasks to be taken off the queue onto a
# worker thread and start executing
latch.wait
# Fill up the queue (with a task that won't complete until
# all tasks are posted)
max_queue.times do
subject.post{ all_tasks_posted.wait; initial_executed.increment; }
end
# Inject 20 more tasks, which should throw an exception
20.times do
expect {
subject.post { subsequent_executed.increment; }
}.to raise_error(Concurrent::RejectedExecutionError)
end
# Trigger the event, so that the tasks in the threads and on
# the queue can run to completion
all_tasks_posted.set
# Wait for all tasks to finish
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
# The tasks should have run until all the threads and the
# queue filled up...
expect(initial_executed.value).to be (max_threads + max_queue)
# ..but been dropped after that
expect(subsequent_executed.value).to be 0
end
specify 'a #<< task is never executed when the queue is at capacity' do
all_tasks_posted = Concurrent::Event.new
latch = Concurrent::CountDownLatch.new(max_threads)
initial_executed = Concurrent::AtomicFixnum.new(0)
subsequent_executed = Concurrent::AtomicFixnum.new(0)
# Fill up all the threads (with a task that won't complete until
# all tasks are posted)
max_threads.times do
subject << proc { latch.count_down; all_tasks_posted.wait ; initial_executed.increment;}
end
# Wait for all those tasks to be taken off the queue onto a
# worker thread and start executing
latch.wait
# Fill up the queue (with a task that won't complete until
# all tasks are posted)
max_queue.times do
subject << proc { all_tasks_posted.wait; initial_executed.increment; }
end
# Inject 20 more tasks, which should throw an exeption
20.times do
expect {
subject << proc { subsequent_executed.increment; }
}.to raise_error(Concurrent::RejectedExecutionError)
end
# Trigger the event, so that the tasks in the threads and on
# the queue can run to completion
all_tasks_posted.set
# Wait for all tasks to finish
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
# The tasks should have run until all the threads and the
# queue filled up...
expect(initial_executed.value).to be (max_threads + max_queue)
# ..but been rejected after that
expect(subsequent_executed.value).to be 0
end
end
context ':discard' do
subject do
described_class.new(
min_threads: min_threads,
max_threads: max_threads,
idletime: idletime,
max_queue: max_queue,
fallback_policy: :discard
)
end
specify 'a #post task is never executed when the queue is at capacity' do
all_tasks_posted = Concurrent::Event.new
latch = Concurrent::CountDownLatch.new(max_threads)
initial_executed = Concurrent::AtomicFixnum.new(0)
subsequent_executed = Concurrent::AtomicFixnum.new(0)
# Fill up all the threads (with a task that won't complete until
# all tasks are posted)
max_threads.times do
subject.post{ latch.count_down; all_tasks_posted.wait ; initial_executed.increment;}
end
# Wait for all those tasks to be taken off the queue onto a
# worker thread and start executing
latch.wait
# Fill up the queue (with a task that won't complete until
# all tasks are posted)
max_queue.times do
subject.post{ all_tasks_posted.wait; initial_executed.increment; }
end
# Inject 20 more tasks, which should be dropped without an exception
20.times do
subject.post{ subsequent_executed.increment; }
end
# Trigger the event, so that the tasks in the threads and on
# the queue can run to completion
all_tasks_posted.set
# Wait for all tasks to finish
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
# The tasks should have run until all the threads and the
# queue filled up...
expect(initial_executed.value).to be (max_threads + max_queue)
# ..but been dropped after that
expect(subsequent_executed.value).to be 0
end
specify 'a #<< task is never executed when the queue is at capacity' do
all_tasks_posted = Concurrent::Event.new
latch = Concurrent::CountDownLatch.new(max_threads)
initial_executed = Concurrent::AtomicFixnum.new(0)
subsequent_executed = Concurrent::AtomicFixnum.new(0)
# Fill up all the threads (with a task that won't complete until
# all tasks are posted)
max_threads.times do
subject << proc { latch.count_down; all_tasks_posted.wait ; initial_executed.increment;}
end
# Wait for all those tasks to be taken off the queue onto a
# worker thread and start executing
latch.wait
# Fill up the queue (with a task that won't complete until
# all tasks are posted)
max_queue.times do
subject << proc { all_tasks_posted.wait; initial_executed.increment; }
end
# Inject 20 more tasks, which should be dropped without an exception
20.times do
subject << proc { subsequent_executed.increment; }
end
# Trigger the event, so that the tasks in the threads and on
# the queue can run to completion
all_tasks_posted.set
# Wait for all tasks to finish
subject.shutdown
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
# The tasks should have run until all the threads and the
# queue filled up...
expect(initial_executed.value).to be (max_threads + max_queue)
# ..but been dropped after that
expect(subsequent_executed.value).to be 0
end
specify 'a #post task is never executed when the executor is shutting down' do
executed = Concurrent::AtomicFixnum.new(0)
subject.shutdown
subject.post{ executed.increment }
# Wait for all tasks to finish
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(executed.value).to be 0
end
specify 'a #<< task is never executed when the executor is shutting down' do
executed = Concurrent::AtomicFixnum.new(0)
subject.shutdown
subject << proc { executed.increment }
# Wait for all tasks to finish
expect(subject.wait_for_termination(pool_termination_timeout)).to eq true
expect(executed.value).to be 0
end
specify '#post returns false when the executor is shutting down' do
subject.shutdown
ret = subject.post{ nil }
expect(ret).to be false
end
end
context ':caller_runs' do
let(:executor) do
described_class.new(
min_threads: 1,
max_threads: 1,
idletime: idletime,
max_queue: 1,
fallback_policy: :caller_runs
)
end
after(:each) do
# need to replicate this w/i scope otherwise rspec may complain
executor.shutdown
expect(executor.wait_for_termination(pool_termination_timeout)).to eq true
end
specify '#post does not create any new threads when the queue is at capacity' do
trigger = Concurrent::Event.new
initial = Thread.list.length
executor = described_class.new(
min_threads: 1,
max_threads: 1,
idletime: idletime,
max_queue: 1,
fallback_policy: :caller_runs
)
# Post several tasks to the executor. Has to be a new thread,
# because it will start blocking once the queue fills up.
in_thread do
5.times{ executor.post{ trigger.wait } }
end
expect(Thread.list.length).to be < initial + 1 + 5
# Let the executor tasks complete.
trigger.set
executor.shutdown
expect(executor.wait_for_termination(pool_termination_timeout)).to eq true
end
specify '#<< executes the task on the current thread when the queue is at capacity' do
trigger = Concurrent::CountDownLatch.new(1)
latch = Concurrent::CountDownLatch.new(1)
executor.post{ trigger.wait }
main = Thread.current
executor.post do
expect(Thread.current).to eq main
latch.count_down
end
latch.wait(0.1)
trigger.count_down
end
specify '#post executes the task on the current thread when the queue is at capacity' do
trigger = Concurrent::Event.new
latch = Concurrent::CountDownLatch.new(5)
executor.post{ trigger.wait }
5.times{|i| executor.post{ latch.count_down } }
latch.wait(0.1)
trigger.set
end
specify '#post executes the task on the current thread when the executor is shutting down' do
latch = Concurrent::CountDownLatch.new(1)
executor.shutdown
executor.post{ latch.count_down }
latch.wait(0.1)
end
specify '#<< executes the task on the current thread when the executor is shutting down' do
latch = Concurrent::CountDownLatch.new(1)
executor.shutdown
executor << proc { latch.count_down }
latch.wait(0.1)
end
end
end
end
|