Skip to content

Commit 2030b8b

Browse files
committed
Wrote queue specs for thread pool executors.
1 parent 6089278 commit 2030b8b

File tree

1 file changed

+69
-18
lines changed

1 file changed

+69
-18
lines changed

spec/concurrent/thread_pool_executor_shared.rb

Lines changed: 69 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,64 +10,115 @@
1010

1111
it_should_behave_like :thread_pool
1212

13-
context '#max_queue' do
13+
context '#initialize' do
14+
15+
it 'defaults :min_length to DEFAULT_MIN_POOL_SIZE' do
16+
subject = described_class.new
17+
subject.min_length.should eq described_class::DEFAULT_MIN_POOL_SIZE
18+
end
19+
20+
it 'defaults :max_length to DEFAULT_MAX_POOL_SIZE' do
21+
subject = described_class.new
22+
subject.max_length.should eq described_class::DEFAULT_MAX_POOL_SIZE
23+
end
24+
25+
it 'defaults :idletime to DEFAULT_THREAD_IDLETIMEOUT' do
26+
subject = described_class.new
27+
subject.idletime.should eq described_class::DEFAULT_THREAD_IDLETIMEOUT
28+
end
1429

15-
it 'is set to zero by default' do
16-
pending
30+
it 'defaults :max_queue to DEFAULT_MAX_QUEUE_SIZE' do
31+
subject = described_class.new
32+
subject.max_queue.should eq described_class::DEFAULT_MAX_QUEUE_SIZE
1733
end
1834

35+
it 'defaults :overflow_policy to :abort' do
36+
subject = described_class.new
37+
subject.overflow_policy.should eq :abort
38+
end
39+
end
40+
41+
context '#max_queue' do
42+
43+
let!(:expected_max){ 100 }
44+
subject{ described_class.new(max_queue: expected_max) }
45+
1946
it 'returns the set value on creation' do
20-
pending
47+
subject.max_queue.should eq expected_max
2148
end
2249

2350
it 'returns the set value when running' do
24-
pending
51+
5.times{ subject.post{ sleep(0.1) } }
52+
sleep(0.1)
53+
subject.max_queue.should eq expected_max
2554
end
2655

2756
it 'returns the set value after stopping' do
28-
pending
57+
5.times{ subject.post{ sleep(0.1) } }
58+
sleep(0.1)
59+
subject.shutdown
60+
subject.wait_for_termination(1)
61+
subject.max_queue.should eq expected_max
2962
end
3063
end
3164

3265
context '#queue_length' do
3366

3467
it 'returns zero on creation' do
35-
pending
68+
subject.queue_length.should eq 0
3669
end
3770

3871
it 'returns zero when there are no enqueued tasks' do
39-
pending
72+
5.times{ subject.post{ nil } }
73+
sleep(0.1)
74+
subject.queue_length.should eq 0
4075
end
4176

4277
it 'returns the size of the queue when tasks are enqueued' do
43-
pending
78+
100.times{ subject.post{ sleep(0.5) } }
79+
sleep(0.1)
80+
subject.queue_length.should > 0
4481
end
4582

4683
it 'returns zero when stopped' do
47-
pending
84+
100.times{ subject.post{ sleep(0.5) } }
85+
sleep(0.1)
86+
subject.shutdown
87+
subject.wait_for_termination(1)
88+
subject.queue_length.should eq 0
4889
end
4990
end
5091

5192
context '#remaining_capacity' do
5293

94+
let!(:expected_max){ 100 }
95+
subject{ described_class.new(max_queue: expected_max) }
96+
5397
it 'returns -1 when :max_queue is set to zero' do
54-
pending
98+
executor = described_class.new(max_queue: 0)
99+
executor.remaining_capacity.should eq -1
55100
end
56101

57-
it 'returns :max_size on creation' do
58-
pending
102+
it 'returns :max_length on creation' do
103+
subject.remaining_capacity.should eq expected_max
59104
end
60105

61-
it 'returns :max_size when no tasks are enqueued' do
62-
pending
106+
it 'returns :max_length when no tasks are enqueued' do
107+
5.times{ subject.post{ nil } }
108+
sleep(0.1)
109+
subject.remaining_capacity.should eq eq expected_max
63110
end
64111

65112
it 'returns the remaining capacity when tasks are enqueued' do
66-
pending
113+
100.times{ subject.post{ sleep(0.5) } }
114+
subject.remaining_capacity.should < expected_max
67115
end
68116

69-
it 'returns :max_size when stopped' do
70-
pending
117+
it 'returns :max_length when stopped' do
118+
100.times{ subject.post{ sleep(0.5) } }
119+
subject.shutdown
120+
subject.wait_for_termination(1)
121+
subject.remaining_capacity.should eq expected_max
71122
end
72123
end
73124

0 commit comments

Comments
 (0)