Skip to content

Commit 186af09

Browse files
committed
Fix agent spec timing
1 parent 7c7f815 commit 186af09

File tree

5 files changed

+86
-37
lines changed

5 files changed

+86
-37
lines changed

spec/concurrent/agent_spec.rb

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ def update(time, old_value, new_value)
119119
end
120120
1
121121
end
122+
expect(subject.await_for(1)).to eq true
123+
expect(subject).to_not be_failed
122124
end
123125

124126
specify 'when the action raises an error the value will not change' do
@@ -372,7 +374,7 @@ def update(time, old_value, new_value)
372374

373375
subject.restart(42)
374376
latch.wait(0.1)
375-
sleep(0.1)
377+
expect(subject.await_for(1)).to eq true
376378
expect(subject.value).to eq expected
377379
end
378380

@@ -409,6 +411,7 @@ def update(time, old_value, new_value)
409411
end
410412

411413
latch.wait(2)
414+
expect(subject.await_for(1)).to eq true
412415
expect(actual).to eq expected
413416
end
414417

@@ -438,25 +441,31 @@ def update(time, old_value, new_value)
438441
it 'returns true when the job is post' do
439442
subject = Agent.new(0)
440443
expect(subject.send { nil }).to be true
444+
expect(subject.await_for(1)).to eq true
441445
end
442446

443447
it 'returns false when #failed?' do
444448
subject = Agent.new(0)
445449
allow(subject).to receive(:failed?).and_return(true)
446450
expect(subject.send { nil }).to be false
451+
expect(subject.await_for(1)).to eq true
447452
end
448453

449454
it 'posts to the global fast executor' do
450455
subject = Agent.new(0)
451456
expect(subject).to receive(:enqueue_action_job).with(anything, anything, Concurrent.global_fast_executor).and_call_original
452457
subject.send { nil }
458+
expect(subject.await_for(1)).to eq true
453459
end
454460

455461
it 'does not wait for the action to process' do
456462
job_done = false
457463
subject = Agent.new(0)
458-
subject.send { sleep(5); job_done = true }
464+
latch = CountDownLatch.new
465+
subject.send { latch.wait; job_done = true }
459466
expect(job_done).to be false
467+
latch.count_down
468+
expect(subject.await_for(1)).to eq true
460469
end
461470
end
462471

@@ -465,6 +474,7 @@ def update(time, old_value, new_value)
465474
it 'returns true when the job is post' do
466475
subject = Agent.new(0)
467476
expect(subject.send! { nil }).to be true
477+
expect(subject.await_for(1)).to eq true
468478
end
469479

470480
it 'raises an error when #failed?' do
@@ -479,13 +489,17 @@ def update(time, old_value, new_value)
479489
subject = Agent.new(0)
480490
expect(subject).to receive(:enqueue_action_job).with(anything, anything, Concurrent.global_fast_executor).and_call_original
481491
subject.send! { nil }
492+
expect(subject.await_for(1)).to eq true
482493
end
483494

484495
it 'does not wait for the action to process' do
485496
job_done = false
486497
subject = Agent.new(0)
487-
subject.send! { sleep(5); job_done = true }
498+
latch = CountDownLatch.new
499+
subject.send! { latch.wait; job_done = true }
488500
expect(job_done).to be false
501+
latch.count_down
502+
expect(subject.await_for(1)).to eq true
489503
end
490504
end
491505

@@ -494,6 +508,7 @@ def update(time, old_value, new_value)
494508
it 'returns true when the job is post' do
495509
subject = Agent.new(0)
496510
expect(subject.send_off { nil }).to be true
511+
expect(subject.await_for(1)).to eq true
497512
end
498513

499514
it 'returns false when #failed?' do
@@ -506,13 +521,17 @@ def update(time, old_value, new_value)
506521
subject = Agent.new(0)
507522
expect(subject).to receive(:enqueue_action_job).with(anything, anything, Concurrent.global_io_executor).and_call_original
508523
subject.send_off { nil }
524+
expect(subject.await_for(1)).to eq true
509525
end
510526

511527
it 'does not wait for the action to process' do
512528
job_done = false
513529
subject = Agent.new(0)
514-
subject.send_off { sleep(5); job_done = true }
530+
latch = CountDownLatch.new
531+
subject.send_off { latch.wait; job_done = true }
515532
expect(job_done).to be false
533+
latch.count_down
534+
expect(subject.await_for(1)).to eq true
516535
end
517536
end
518537

@@ -521,6 +540,7 @@ def update(time, old_value, new_value)
521540
it 'returns true when the job is post' do
522541
subject = Agent.new(0)
523542
expect(subject.send_off! { nil }).to be true
543+
expect(subject.await_for(1)).to eq true
524544
end
525545

526546
it 'raises an error when #failed?' do
@@ -535,13 +555,17 @@ def update(time, old_value, new_value)
535555
subject = Agent.new(0)
536556
expect(subject).to receive(:enqueue_action_job).with(anything, anything, Concurrent.global_io_executor).and_call_original
537557
subject.send_off! { nil }
558+
expect(subject.await_for(1)).to eq true
538559
end
539560

540561
it 'does not wait for the action to process' do
541562
job_done = false
542563
subject = Agent.new(0)
543-
subject.send_off! { sleep(5); job_done = true }
564+
latch = CountDownLatch.new
565+
subject.send_off! { latch.wait; job_done = true }
544566
expect(job_done).to be false
567+
latch.count_down
568+
expect(subject.await_for(1)).to eq true
545569
end
546570
end
547571

@@ -563,13 +587,6 @@ def update(time, old_value, new_value)
563587
subject = Agent.new(0)
564588
subject.send_via(immediate) { nil }
565589
end
566-
567-
it 'does not wait for the action to process' do
568-
job_done = false
569-
subject = Agent.new(0)
570-
subject.send_via(executor) { sleep(5); job_done = true }
571-
expect(job_done).to be false
572-
end
573590
end
574591

575592
context 'with #send_via!' do
@@ -592,20 +609,14 @@ def update(time, old_value, new_value)
592609
subject = Agent.new(0)
593610
subject.send_via!(immediate) { nil }
594611
end
595-
596-
it 'does not wait for the action to process' do
597-
job_done = false
598-
subject = Agent.new(0)
599-
subject.send_via!(executor) { sleep(5); job_done = true }
600-
expect(job_done).to be false
601-
end
602612
end
603613

604614
context 'with #post' do
605615

606616
it 'returns true when the job is post' do
607617
subject = Agent.new(0)
608618
expect(subject.post { nil }).to be true
619+
expect(subject.await_for(1)).to eq true
609620
end
610621

611622
it 'returns false when #failed?' do
@@ -618,13 +629,17 @@ def update(time, old_value, new_value)
618629
subject = Agent.new(0)
619630
expect(subject).to receive(:enqueue_action_job).with(anything, anything, Concurrent.global_io_executor).and_call_original
620631
subject.post { nil }
632+
expect(subject.await_for(1)).to eq true
621633
end
622634

623635
it 'does not wait for the action to process' do
624636
job_done = false
625637
subject = Agent.new(0)
626-
subject.post { sleep(5); job_done = true }
638+
latch = CountDownLatch.new
639+
subject.post { latch.wait; job_done = true }
627640
expect(job_done).to be false
641+
latch.count_down
642+
expect(subject.await_for(1)).to eq true
628643
end
629644
end
630645

@@ -633,6 +648,7 @@ def update(time, old_value, new_value)
633648
it 'returns self when the job is post' do
634649
subject = Agent.new(0)
635650
expect(subject << proc { nil }).to be subject
651+
expect(subject.await_for(1)).to eq true
636652
end
637653

638654
it 'returns self when #failed?' do
@@ -642,16 +658,20 @@ def update(time, old_value, new_value)
642658
end
643659

644660
it 'posts to the global io executor' do
645-
expect(Concurrent.global_io_executor).to receive(:post).with(any_args).and_call_original
646661
subject = Agent.new(0)
662+
expect(subject).to receive(:enqueue_action_job).with(anything, anything, Concurrent.global_io_executor).and_call_original
647663
subject << proc { nil }
664+
expect(subject.await_for(1)).to eq true
648665
end
649666

650667
it 'does not wait for the action to process' do
651668
job_done = false
652669
subject = Agent.new(0)
653-
subject << proc { sleep(5); job_done = true }
670+
latch = CountDownLatch.new
671+
subject << proc { latch.wait; job_done = true }
654672
expect(job_done).to be false
673+
latch.count_down
674+
expect(subject.await_for(1)).to eq true
655675
end
656676
end
657677
end
@@ -707,6 +727,7 @@ def update(time, old_value, new_value)
707727
subject.restart(42, clear_actions: true)
708728
result = end_latch.wait(0.1)
709729
expect(result).to be false
730+
expect(subject.await_for(1)).to eq true
710731
end
711732

712733
it 'does not clear the action queue when :clear_actions is false' do
@@ -723,6 +744,7 @@ def update(time, old_value, new_value)
723744
subject.restart(42, clear_actions: false)
724745
result = end_latch.wait(3)
725746
expect(result).to be true
747+
expect(subject.await_for(1)).to eq true
726748
end
727749

728750
it 'does not clear the action queue when :clear_actions is not given' do
@@ -739,6 +761,7 @@ def update(time, old_value, new_value)
739761
subject.restart(42)
740762
result = end_latch.wait(3)
741763
expect(result).to be true
764+
expect(subject.await_for(1)).to eq true
742765
end
743766

744767
it 'resumes action processing if actions are enqueued' do
@@ -759,6 +782,7 @@ def update(time, old_value, new_value)
759782

760783
subject.restart(42, clear_actions: false)
761784
expect(finish_latch.wait(5)).to be true
785+
expect(subject.await_for(1)).to eq true
762786
end
763787

764788
it 'does not trigger observation' do
@@ -876,14 +900,16 @@ def update(time, old_value, new_value)
876900

877901
it 'does not block on actions from other threads' do
878902
latch = Concurrent::CountDownLatch.new
903+
finish = Concurrent::CountDownLatch.new
879904
subject = Agent.new(0)
880905
in_thread do
881-
subject.send_via(executor) { sleep }
906+
subject.send_via(executor) { finish.wait }
882907
latch.count_down
883908
end
884909

885910
latch.wait(0.1)
886-
expect(subject.await).to be_truthy
911+
expect(subject.await_for(1)).to eq true
912+
finish.count_down
887913
end
888914

889915
it 'blocks indefinitely' do
@@ -930,14 +956,16 @@ def update(time, old_value, new_value)
930956

931957
it 'does not block on actions from other threads' do
932958
latch = Concurrent::CountDownLatch.new
959+
finish = Concurrent::CountDownLatch.new
933960
subject = Agent.new(0)
934961
in_thread do
935-
subject.send_via(executor) { sleep }
962+
subject.send_via(executor) { finish.wait }
936963
latch.count_down
937964
end
938965

939966
latch.wait(0.1)
940967
expect(subject.await_for(0.1)).to be true
968+
finish.count_down
941969
end
942970

943971
it 'returns true when all prior actions have processed', notravis: true do
@@ -952,6 +980,7 @@ def update(time, old_value, new_value)
952980
subject.send_via(executor) { sleep(1) }
953981
5.times { subject.send_via(executor) { nil } }
954982
expect(subject.await_for(0.1)).to be false
983+
expect(subject.await_for(5)).to eq true
955984
end
956985

957986
it 'returns false if restarted with :clear_actions true', notravis: true do
@@ -978,14 +1007,16 @@ def update(time, old_value, new_value)
9781007

9791008
it 'does not block on actions from other threads' do
9801009
latch = Concurrent::CountDownLatch.new
1010+
finish = Concurrent::CountDownLatch.new
9811011
subject = Agent.new(0)
9821012
in_thread do
983-
subject.send_via(executor) { sleep }
1013+
subject.send_via(executor) { finish.wait }
9841014
latch.count_down
9851015
end
9861016

9871017
latch.wait(0.1)
9881018
expect(subject.await_for!(0.1)).to be true
1019+
finish.count_down
9891020
end
9901021

9911022
it 'returns true when all prior actions have processed' do
@@ -1002,6 +1033,7 @@ def update(time, old_value, new_value)
10021033
expect {
10031034
subject.await_for!(0.1)
10041035
}.to raise_error(Concurrent::TimeoutError)
1036+
expect(subject.await_for(5)).to eq true
10051037
end
10061038

10071039
it 'raises an error if restarted with :clear_actions true', notravis: true do
@@ -1034,14 +1066,16 @@ def update(time, old_value, new_value)
10341066

10351067
it 'does not block on actions from other threads' do
10361068
latch = Concurrent::CountDownLatch.new
1069+
finish = Concurrent::CountDownLatch.new
10371070
subject = Agent.new(0)
10381071
in_thread do
1039-
subject.send_via(executor) { sleep }
1072+
subject.send_via(executor) { finish.wait }
10401073
latch.count_down
10411074
end
10421075

10431076
latch.wait(0.1)
10441077
expect(subject.wait(0.1)).to be true
1078+
finish.count_down
10451079
end
10461080

10471081
it 'blocks indefinitely when timeout is nil' do
@@ -1082,6 +1116,7 @@ def update(time, old_value, new_value)
10821116
subject.send_via(executor) { sleep(1) }
10831117
5.times { subject.send_via(executor) { nil } }
10841118
expect(subject.wait(0.1)).to be false
1119+
expect(subject.wait(5)).to eq true
10851120
end
10861121

10871122
it 'returns false when timeout is given and restarted with :clear_actions true', notravis: true do
@@ -1133,6 +1168,7 @@ def update(time, old_value, new_value)
11331168
agents.each { |agent| agent.send_via(executor) { sleep(0.3) } }
11341169
ok = Agent.await_for(0.1, *agents)
11351170
expect(ok).to be false
1171+
expect(Agent.await_for!(1, *agents)).to eq true
11361172
end
11371173
end
11381174

@@ -1159,6 +1195,7 @@ def update(time, old_value, new_value)
11591195
expect {
11601196
Agent.await_for!(0.1, *agents)
11611197
}.to raise_error(Concurrent::TimeoutError)
1198+
expect(Agent.await_for!(1, *agents)).to eq true
11621199
end
11631200
end
11641201
end

spec/concurrent/atomic/cyclic_barrier_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ module Concurrent
3737

3838
context 'with waiting threads' do
3939
it 'should be equal to the waiting threads count' do
40-
threads = [in_thread { barrier.wait }, in_thread { barrier.wait }]
41-
Thread.pass until threads.all? { |t| t.status == 'sleep' }
42-
expect(barrier.number_waiting).to eq 2
40+
in_thread { barrier.wait }
41+
in_thread { barrier.wait }
42+
repeat_until_success { expect(barrier.number_waiting).to eq 2 }
4343
end
4444
end
4545
end

spec/concurrent/map_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ module Concurrent
107107
getter_threads = ::Array.new(getter_threads_count) do
108108
in_thread do
109109
block_until_compute_started.call('getter')
110-
Thread.pass while @cache[:a].nil?
111-
expect(1).to eq @cache[:a]
110+
repeat_until_success { expect(1).to eq @cache[:a] }
112111
end
113112
end
114113

0 commit comments

Comments
 (0)