concurrent-ruby 0.6.1 → 0.7.0.rc0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/concurrent.rb +3 -4
- data/lib/concurrent/atomic.rb +46 -0
- data/lib/concurrent/atomic_reference/concurrent_update_error.rb +7 -0
- data/lib/concurrent/atomic_reference/delegated_update.rb +28 -0
- data/lib/concurrent/atomic_reference/direct_update.rb +28 -0
- data/lib/concurrent/atomic_reference/jruby.rb +8 -0
- data/lib/concurrent/atomic_reference/mutex_atomic.rb +47 -0
- data/lib/concurrent/atomic_reference/numeric_cas_wrapper.rb +24 -0
- data/lib/concurrent/atomic_reference/rbx.rb +16 -0
- data/lib/concurrent/atomic_reference/ruby.rb +16 -0
- data/lib/concurrent/atomics.rb +1 -1
- data/lib/concurrent/configuration.rb +1 -1
- data/lib/concurrent/supervisor.rb +1 -1
- data/lib/concurrent/timer_task.rb +0 -36
- data/lib/concurrent/version.rb +1 -1
- data/lib/concurrent_ruby_ext.so +0 -0
- data/lib/extension_helper.rb +9 -0
- metadata +16 -148
- data/lib/concurrent/actor/actor.rb +0 -270
- data/lib/concurrent/actor/postable.rb +0 -102
- data/lib/concurrent/actors.rb +0 -2
- data/lib/concurrent/atomic/atomic.rb +0 -48
- data/lib/concurrent/runnable.rb +0 -90
- data/lib/concurrent/stoppable.rb +0 -20
- data/spec/concurrent/actor/actor_spec.rb +0 -376
- data/spec/concurrent/actor/postable_shared.rb +0 -218
- data/spec/concurrent/actress_spec.rb +0 -211
- data/spec/concurrent/agent_spec.rb +0 -500
- data/spec/concurrent/async_spec.rb +0 -352
- data/spec/concurrent/atomic/atomic_boolean_spec.rb +0 -172
- data/spec/concurrent/atomic/atomic_fixnum_spec.rb +0 -186
- data/spec/concurrent/atomic/atomic_spec.rb +0 -133
- data/spec/concurrent/atomic/condition_spec.rb +0 -171
- data/spec/concurrent/atomic/copy_on_notify_observer_set_spec.rb +0 -10
- data/spec/concurrent/atomic/copy_on_write_observer_set_spec.rb +0 -10
- data/spec/concurrent/atomic/count_down_latch_spec.rb +0 -151
- data/spec/concurrent/atomic/cyclic_barrier_spec.rb +0 -248
- data/spec/concurrent/atomic/event_spec.rb +0 -200
- data/spec/concurrent/atomic/observer_set_shared.rb +0 -242
- data/spec/concurrent/atomic/thread_local_var_spec.rb +0 -113
- data/spec/concurrent/channel/buffered_channel_spec.rb +0 -151
- data/spec/concurrent/channel/channel_spec.rb +0 -39
- data/spec/concurrent/channel/probe_spec.rb +0 -77
- data/spec/concurrent/channel/unbuffered_channel_spec.rb +0 -132
- data/spec/concurrent/collection/blocking_ring_buffer_spec.rb +0 -149
- data/spec/concurrent/collection/priority_queue_spec.rb +0 -317
- data/spec/concurrent/collection/ring_buffer_spec.rb +0 -126
- data/spec/concurrent/configuration_spec.rb +0 -69
- data/spec/concurrent/dataflow_spec.rb +0 -242
- data/spec/concurrent/delay_spec.rb +0 -91
- data/spec/concurrent/dereferenceable_shared.rb +0 -146
- data/spec/concurrent/exchanger_spec.rb +0 -66
- data/spec/concurrent/executor/cached_thread_pool_shared.rb +0 -115
- data/spec/concurrent/executor/fixed_thread_pool_shared.rb +0 -136
- data/spec/concurrent/executor/global_thread_pool_shared.rb +0 -35
- data/spec/concurrent/executor/immediate_executor_spec.rb +0 -12
- data/spec/concurrent/executor/java_cached_thread_pool_spec.rb +0 -44
- data/spec/concurrent/executor/java_fixed_thread_pool_spec.rb +0 -64
- data/spec/concurrent/executor/java_single_thread_executor_spec.rb +0 -21
- data/spec/concurrent/executor/java_thread_pool_executor_spec.rb +0 -71
- data/spec/concurrent/executor/per_thread_executor_spec.rb +0 -57
- data/spec/concurrent/executor/ruby_cached_thread_pool_spec.rb +0 -69
- data/spec/concurrent/executor/ruby_fixed_thread_pool_spec.rb +0 -39
- data/spec/concurrent/executor/ruby_single_thread_executor_spec.rb +0 -18
- data/spec/concurrent/executor/ruby_thread_pool_executor_spec.rb +0 -171
- data/spec/concurrent/executor/safe_task_executor_spec.rb +0 -103
- data/spec/concurrent/executor/thread_pool_class_cast_spec.rb +0 -52
- data/spec/concurrent/executor/thread_pool_executor_shared.rb +0 -155
- data/spec/concurrent/executor/thread_pool_shared.rb +0 -269
- data/spec/concurrent/executor/timer_set_spec.rb +0 -183
- data/spec/concurrent/future_spec.rb +0 -329
- data/spec/concurrent/ivar_spec.rb +0 -215
- data/spec/concurrent/mvar_spec.rb +0 -380
- data/spec/concurrent/obligation_shared.rb +0 -102
- data/spec/concurrent/obligation_spec.rb +0 -282
- data/spec/concurrent/observable_shared.rb +0 -177
- data/spec/concurrent/observable_spec.rb +0 -56
- data/spec/concurrent/options_parser_spec.rb +0 -71
- data/spec/concurrent/promise_spec.rb +0 -367
- data/spec/concurrent/runnable_shared.rb +0 -68
- data/spec/concurrent/runnable_spec.rb +0 -235
- data/spec/concurrent/scheduled_task_spec.rb +0 -340
- data/spec/concurrent/stoppable_shared.rb +0 -37
- data/spec/concurrent/supervisor_spec.rb +0 -1149
- data/spec/concurrent/timer_task_spec.rb +0 -256
- data/spec/concurrent/tvar_spec.rb +0 -137
- data/spec/concurrent/utility/processor_count_spec.rb +0 -20
- data/spec/concurrent/utility/timeout_spec.rb +0 -50
- data/spec/concurrent/utility/timer_spec.rb +0 -52
- data/spec/spec_helper.rb +0 -41
- data/spec/support/example_group_extensions.rb +0 -52
- data/spec/support/less_than_or_equal_to_matcher.rb +0 -5
@@ -1,39 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Concurrent
|
4
|
-
|
5
|
-
describe Channel do
|
6
|
-
|
7
|
-
describe '.select' do
|
8
|
-
|
9
|
-
context 'without timeout' do
|
10
|
-
it 'returns the first value available on a channel' do
|
11
|
-
channels = [ UnbufferedChannel.new, UnbufferedChannel.new]
|
12
|
-
|
13
|
-
Thread.new { channels[1].push 77 }
|
14
|
-
|
15
|
-
value, channel = Channel.select(*channels)
|
16
|
-
|
17
|
-
value.should eq 77
|
18
|
-
channel.should be channels[1]
|
19
|
-
end
|
20
|
-
|
21
|
-
it 'cleans up' do
|
22
|
-
channels = [ UnbufferedChannel.new, UnbufferedChannel.new]
|
23
|
-
channels.each { |ch| ch.stub(:remove_probe).with( an_instance_of(Channel::Probe) )}
|
24
|
-
|
25
|
-
Thread.new { channels[1].push 77 }
|
26
|
-
|
27
|
-
value, channel = Channel.select(*channels)
|
28
|
-
|
29
|
-
value.should eq 77
|
30
|
-
channel.should be channels[1]
|
31
|
-
|
32
|
-
channels.each { |ch| expect(ch).to have_received(:remove_probe).with( an_instance_of(Channel::Probe) ) }
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
|
38
|
-
end
|
39
|
-
end
|
@@ -1,77 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require_relative '../observable_shared'
|
3
|
-
|
4
|
-
module Concurrent
|
5
|
-
|
6
|
-
describe Channel::Probe do
|
7
|
-
|
8
|
-
let(:channel) { Object.new }
|
9
|
-
let(:probe) { Channel::Probe.new }
|
10
|
-
|
11
|
-
describe 'behavior' do
|
12
|
-
|
13
|
-
# observable
|
14
|
-
|
15
|
-
subject{ Channel::Probe.new }
|
16
|
-
|
17
|
-
def trigger_observable(observable)
|
18
|
-
observable.set('value')
|
19
|
-
end
|
20
|
-
|
21
|
-
it_should_behave_like :observable
|
22
|
-
end
|
23
|
-
|
24
|
-
describe '#set_unless_assigned' do
|
25
|
-
context 'empty probe' do
|
26
|
-
it 'assigns the value' do
|
27
|
-
probe.set_unless_assigned(32, channel)
|
28
|
-
probe.value.should eq 32
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'assign the channel' do
|
32
|
-
probe.set_unless_assigned(32, channel)
|
33
|
-
probe.channel.should be channel
|
34
|
-
end
|
35
|
-
|
36
|
-
it 'returns true' do
|
37
|
-
probe.set_unless_assigned('hi', channel).should eq true
|
38
|
-
end
|
39
|
-
end
|
40
|
-
|
41
|
-
context 'fulfilled probe' do
|
42
|
-
before(:each) { probe.set([27, nil]) }
|
43
|
-
|
44
|
-
it 'does not assign the value' do
|
45
|
-
probe.set_unless_assigned(88, channel)
|
46
|
-
probe.value.should eq 27
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'returns false' do
|
50
|
-
probe.set_unless_assigned('hello', channel).should eq false
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
context 'rejected probe' do
|
55
|
-
before(:each) { probe.fail }
|
56
|
-
|
57
|
-
it 'does not assign the value' do
|
58
|
-
probe.set_unless_assigned(88, channel)
|
59
|
-
probe.should be_rejected
|
60
|
-
end
|
61
|
-
|
62
|
-
it 'has a nil value' do
|
63
|
-
probe.value.should be_nil
|
64
|
-
end
|
65
|
-
|
66
|
-
it 'has a nil channel' do
|
67
|
-
probe.channel.should be_nil
|
68
|
-
end
|
69
|
-
|
70
|
-
it 'returns false' do
|
71
|
-
probe.set_unless_assigned('hello', channel).should eq false
|
72
|
-
end
|
73
|
-
end
|
74
|
-
end
|
75
|
-
|
76
|
-
end
|
77
|
-
end
|
@@ -1,132 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Concurrent
|
4
|
-
|
5
|
-
describe UnbufferedChannel do
|
6
|
-
|
7
|
-
let!(:channel) { subject }
|
8
|
-
let(:probe) { Channel::Probe.new }
|
9
|
-
|
10
|
-
context 'with one thread' do
|
11
|
-
|
12
|
-
context 'without timeout' do
|
13
|
-
|
14
|
-
describe '#push' do
|
15
|
-
it 'should block' do
|
16
|
-
t = Thread.new { channel.push 5 }
|
17
|
-
sleep(0.05)
|
18
|
-
t.status.should eq 'sleep'
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe '#pop' do
|
23
|
-
it 'should block' do
|
24
|
-
t = Thread.new { channel.pop }
|
25
|
-
sleep(0.05)
|
26
|
-
t.status.should eq 'sleep'
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
|
34
|
-
context 'cooperating threads' do
|
35
|
-
|
36
|
-
it 'passes the pushed value to thread waiting on pop' do
|
37
|
-
result = nil
|
38
|
-
|
39
|
-
Thread.new { channel.push 42 }
|
40
|
-
Thread.new { result = channel.pop; }
|
41
|
-
|
42
|
-
sleep(0.1)
|
43
|
-
|
44
|
-
result.should eq 42
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'passes the pushed value to only one thread' do
|
48
|
-
result = []
|
49
|
-
|
50
|
-
Thread.new { channel.push 37 }
|
51
|
-
Thread.new { result << channel.pop }
|
52
|
-
Thread.new { result << channel.pop }
|
53
|
-
|
54
|
-
sleep(0.1)
|
55
|
-
|
56
|
-
result.should have(1).items
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'gets the pushed value when ready' do
|
60
|
-
result = nil
|
61
|
-
|
62
|
-
Thread.new { result = channel.pop; }
|
63
|
-
Thread.new { channel.push 57 }
|
64
|
-
|
65
|
-
sleep(0.1)
|
66
|
-
|
67
|
-
result.should eq 57
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
describe 'select' do
|
72
|
-
|
73
|
-
it 'does not block' do
|
74
|
-
t = Thread.new { channel.select(probe) }
|
75
|
-
|
76
|
-
sleep(0.05)
|
77
|
-
|
78
|
-
t.status.should eq false
|
79
|
-
end
|
80
|
-
|
81
|
-
it 'gets notified by writer thread' do
|
82
|
-
channel.select(probe)
|
83
|
-
|
84
|
-
Thread.new { channel.push 82 }
|
85
|
-
|
86
|
-
probe.value.should eq 82
|
87
|
-
end
|
88
|
-
|
89
|
-
it 'ignores already set probes and waits for a new one' do
|
90
|
-
probe.set(27)
|
91
|
-
|
92
|
-
channel.select(probe)
|
93
|
-
|
94
|
-
t = Thread.new { channel.push 72 }
|
95
|
-
|
96
|
-
sleep(0.05)
|
97
|
-
|
98
|
-
t.status.should eq 'sleep'
|
99
|
-
|
100
|
-
new_probe = Channel::Probe.new
|
101
|
-
|
102
|
-
channel.select(new_probe)
|
103
|
-
|
104
|
-
sleep(0.05)
|
105
|
-
|
106
|
-
new_probe.value.should eq 72
|
107
|
-
end
|
108
|
-
|
109
|
-
end
|
110
|
-
|
111
|
-
describe 'probe set' do
|
112
|
-
|
113
|
-
it 'has size zero after creation' do
|
114
|
-
channel.probe_set_size.should eq 0
|
115
|
-
end
|
116
|
-
|
117
|
-
it 'increases size after a select' do
|
118
|
-
channel.select(probe)
|
119
|
-
channel.probe_set_size.should eq 1
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'decreases size after a removal' do
|
123
|
-
channel.select(probe)
|
124
|
-
channel.remove_probe(probe)
|
125
|
-
channel.probe_set_size.should eq 0
|
126
|
-
end
|
127
|
-
|
128
|
-
end
|
129
|
-
|
130
|
-
|
131
|
-
end
|
132
|
-
end
|
@@ -1,149 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
module Concurrent
|
4
|
-
|
5
|
-
describe BlockingRingBuffer do
|
6
|
-
|
7
|
-
let(:capacity) { 3 }
|
8
|
-
let(:buffer) { BlockingRingBuffer.new(capacity) }
|
9
|
-
|
10
|
-
def fill_buffer
|
11
|
-
capacity.times { buffer.put 3 }
|
12
|
-
end
|
13
|
-
|
14
|
-
describe '#capacity' do
|
15
|
-
it 'returns the value passed in constructor' do
|
16
|
-
buffer.capacity.should eq capacity
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
describe '#count' do
|
21
|
-
it 'is zero when created' do
|
22
|
-
buffer.count.should eq 0
|
23
|
-
end
|
24
|
-
|
25
|
-
it 'increases when an element is added' do
|
26
|
-
buffer.put 5
|
27
|
-
buffer.count.should eq 1
|
28
|
-
|
29
|
-
buffer.put 1
|
30
|
-
buffer.count.should eq 2
|
31
|
-
end
|
32
|
-
|
33
|
-
it 'decreases when an element is removed' do
|
34
|
-
buffer.put 10
|
35
|
-
|
36
|
-
buffer.take
|
37
|
-
|
38
|
-
buffer.count.should eq 0
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
describe '#empty?' do
|
43
|
-
it 'is true when count is zero' do
|
44
|
-
buffer.empty?.should be_true
|
45
|
-
end
|
46
|
-
|
47
|
-
it 'is false when count is not zero' do
|
48
|
-
buffer.put 82
|
49
|
-
buffer.empty?.should be_false
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
describe '#full?' do
|
54
|
-
it 'is true when count is capacity' do
|
55
|
-
fill_buffer
|
56
|
-
buffer.full?.should be_true
|
57
|
-
end
|
58
|
-
|
59
|
-
it 'is false when count is not capacity' do
|
60
|
-
buffer.full?.should be_false
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
describe '#put' do
|
65
|
-
it 'block when buffer is full' do
|
66
|
-
fill_buffer
|
67
|
-
|
68
|
-
t = Thread.new { buffer.put 32 }
|
69
|
-
|
70
|
-
sleep(0.1)
|
71
|
-
|
72
|
-
t.status.should eq 'sleep'
|
73
|
-
end
|
74
|
-
|
75
|
-
it 'continues when an element is removed' do
|
76
|
-
latch = CountDownLatch.new(1)
|
77
|
-
|
78
|
-
Thread.new { (capacity + 1).times { buffer.put 'hi' }; latch.count_down }
|
79
|
-
Thread.new { sleep(0.1); buffer.take }
|
80
|
-
|
81
|
-
latch.wait(0.2).should be_true
|
82
|
-
end
|
83
|
-
end
|
84
|
-
|
85
|
-
describe '#take' do
|
86
|
-
it 'blocks when buffer is empty' do
|
87
|
-
t = Thread.new { buffer.take }
|
88
|
-
|
89
|
-
sleep(0.1)
|
90
|
-
|
91
|
-
t.status.should eq 'sleep'
|
92
|
-
end
|
93
|
-
|
94
|
-
it 'continues when an element is added' do
|
95
|
-
latch = CountDownLatch.new(1)
|
96
|
-
|
97
|
-
Thread.new { buffer.take; latch.count_down }
|
98
|
-
Thread.new { sleep(0.1); buffer.put 3 }
|
99
|
-
|
100
|
-
latch.wait(0.2).should be_true
|
101
|
-
end
|
102
|
-
|
103
|
-
it 'returns the first added value' do
|
104
|
-
buffer.put 'hi'
|
105
|
-
buffer.put 'foo'
|
106
|
-
buffer.put 'bar'
|
107
|
-
|
108
|
-
buffer.take.should eq 'hi'
|
109
|
-
buffer.take.should eq 'foo'
|
110
|
-
buffer.take.should eq 'bar'
|
111
|
-
end
|
112
|
-
end
|
113
|
-
|
114
|
-
describe '#peek' do
|
115
|
-
context 'buffer empty' do
|
116
|
-
it 'returns nil when buffer is empty' do
|
117
|
-
buffer.peek.should be_nil
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
context 'not empty' do
|
122
|
-
|
123
|
-
before(:each) { buffer.put 'element' }
|
124
|
-
|
125
|
-
it 'returns the first value' do
|
126
|
-
buffer.peek.should eq 'element'
|
127
|
-
end
|
128
|
-
|
129
|
-
it 'does not change buffer' do
|
130
|
-
buffer.peek
|
131
|
-
buffer.count.should eq 1
|
132
|
-
end
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
context 'circular condition' do
|
137
|
-
it 'can filled many times' do
|
138
|
-
fill_buffer
|
139
|
-
capacity.times { buffer.take }
|
140
|
-
|
141
|
-
buffer.put 'hi'
|
142
|
-
|
143
|
-
buffer.take.should eq 'hi'
|
144
|
-
buffer.capacity.should eq capacity
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
|
-
end
|
149
|
-
end
|
@@ -1,317 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
share_examples_for :priority_queue do
|
4
|
-
|
5
|
-
subject{ described_class.new }
|
6
|
-
|
7
|
-
context '#initialize' do
|
8
|
-
|
9
|
-
it 'sorts from high to low when :order is :max' do
|
10
|
-
subject = described_class.from_list([2, 1, 4, 5, 3, 0], order: :max)
|
11
|
-
subject.pop.should eq 5
|
12
|
-
subject.pop.should eq 4
|
13
|
-
subject.pop.should eq 3
|
14
|
-
end
|
15
|
-
|
16
|
-
it 'sorts from high to low when :order is :high' do
|
17
|
-
subject = described_class.new(order: :high)
|
18
|
-
[2, 1, 4, 5, 3, 0].each{|item| subject << item }
|
19
|
-
subject.pop.should eq 5
|
20
|
-
subject.pop.should eq 4
|
21
|
-
subject.pop.should eq 3
|
22
|
-
end
|
23
|
-
|
24
|
-
it 'sorts from low to high when :order is :min' do
|
25
|
-
subject = described_class.from_list([2, 1, 4, 5, 3, 0], order: :min)
|
26
|
-
subject.pop.should eq 0
|
27
|
-
subject.pop.should eq 1
|
28
|
-
subject.pop.should eq 2
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'sorts from low to high when :order is :low' do
|
32
|
-
subject = described_class.new(order: :low)
|
33
|
-
[2, 1, 4, 5, 3, 0].each{|item| subject << item }
|
34
|
-
subject.pop.should eq 0
|
35
|
-
subject.pop.should eq 1
|
36
|
-
subject.pop.should eq 2
|
37
|
-
end
|
38
|
-
|
39
|
-
it 'sorts from high to low by default' do
|
40
|
-
subject = described_class.new
|
41
|
-
subject = described_class.from_list([2, 1, 4, 5, 3, 0])
|
42
|
-
subject.pop.should eq 5
|
43
|
-
subject.pop.should eq 4
|
44
|
-
subject.pop.should eq 3
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
context '#clear' do
|
49
|
-
|
50
|
-
it 'removes all items from a populated queue' do
|
51
|
-
10.times{|i| subject << i}
|
52
|
-
subject.clear
|
53
|
-
subject.should be_empty
|
54
|
-
end
|
55
|
-
|
56
|
-
it 'has no effect on an empty queue' do
|
57
|
-
subject.clear
|
58
|
-
subject.should be_empty
|
59
|
-
end
|
60
|
-
|
61
|
-
specify { subject.clear.should be_true }
|
62
|
-
end
|
63
|
-
|
64
|
-
context '#delete' do
|
65
|
-
|
66
|
-
it 'deletes the requested item when found' do
|
67
|
-
10.times{|item| subject << item }
|
68
|
-
subject.delete(5)
|
69
|
-
subject.pop.should eq 9
|
70
|
-
subject.pop.should eq 8
|
71
|
-
subject.pop.should eq 7
|
72
|
-
subject.pop.should eq 6
|
73
|
-
subject.pop.should eq 4
|
74
|
-
subject.pop.should eq 3
|
75
|
-
subject.pop.should eq 2
|
76
|
-
subject.pop.should eq 1
|
77
|
-
subject.pop.should eq 0
|
78
|
-
end
|
79
|
-
|
80
|
-
it 'deletes the requested item when it is the first element' do
|
81
|
-
10.times{|item| subject << item }
|
82
|
-
subject.delete(9)
|
83
|
-
subject.length.should eq 9
|
84
|
-
subject.pop.should eq 8
|
85
|
-
subject.pop.should eq 7
|
86
|
-
subject.pop.should eq 6
|
87
|
-
subject.pop.should eq 5
|
88
|
-
subject.pop.should eq 4
|
89
|
-
subject.pop.should eq 3
|
90
|
-
subject.pop.should eq 2
|
91
|
-
subject.pop.should eq 1
|
92
|
-
subject.pop.should eq 0
|
93
|
-
end
|
94
|
-
|
95
|
-
it 'deletes the requested item when it is the last element' do
|
96
|
-
10.times{|item| subject << item }
|
97
|
-
subject.delete(2)
|
98
|
-
subject.length.should eq 9
|
99
|
-
subject.pop.should eq 9
|
100
|
-
subject.pop.should eq 8
|
101
|
-
subject.pop.should eq 7
|
102
|
-
subject.pop.should eq 6
|
103
|
-
subject.pop.should eq 5
|
104
|
-
subject.pop.should eq 4
|
105
|
-
subject.pop.should eq 3
|
106
|
-
subject.pop.should eq 1
|
107
|
-
subject.pop.should eq 0
|
108
|
-
end
|
109
|
-
|
110
|
-
it 'deletes multiple matching items when present' do
|
111
|
-
[2, 1, 2, 2, 2, 3, 2].each{|item| subject << item }
|
112
|
-
subject.delete(2)
|
113
|
-
subject.pop.should eq 3
|
114
|
-
subject.pop.should eq 1
|
115
|
-
end
|
116
|
-
|
117
|
-
it 'returns true when found' do
|
118
|
-
10.times{|i| subject << i}
|
119
|
-
subject.delete(2).should be_true
|
120
|
-
end
|
121
|
-
|
122
|
-
it 'returns false when not found' do
|
123
|
-
10.times{|i| subject << i}
|
124
|
-
subject.delete(100).should be_false
|
125
|
-
end
|
126
|
-
|
127
|
-
it 'returns false when called on an empty queue' do
|
128
|
-
subject.delete(:foo).should be_false
|
129
|
-
end
|
130
|
-
end
|
131
|
-
|
132
|
-
context '#empty?' do
|
133
|
-
|
134
|
-
it 'returns true for an empty queue' do
|
135
|
-
subject.should be_empty
|
136
|
-
end
|
137
|
-
|
138
|
-
it 'returns false for a populated queue' do
|
139
|
-
10.times{|i| subject << i}
|
140
|
-
subject.should_not be_empty
|
141
|
-
end
|
142
|
-
end
|
143
|
-
|
144
|
-
context '#include?' do
|
145
|
-
|
146
|
-
it 'returns true if the item is found' do
|
147
|
-
10.times{|i| subject << i}
|
148
|
-
subject.should include(5)
|
149
|
-
end
|
150
|
-
|
151
|
-
it 'returns false if the item is not found' do
|
152
|
-
10.times{|i| subject << i}
|
153
|
-
subject.should_not include(50)
|
154
|
-
end
|
155
|
-
|
156
|
-
it 'returns false when the queue is empty' do
|
157
|
-
subject.should_not include(1)
|
158
|
-
end
|
159
|
-
|
160
|
-
it 'is aliased as #has_priority?' do
|
161
|
-
10.times{|i| subject << i}
|
162
|
-
subject.should have_priority(5)
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
context '#length' do
|
167
|
-
|
168
|
-
it 'returns the length of a populated queue' do
|
169
|
-
10.times{|i| subject << i}
|
170
|
-
subject.length.should eq 10
|
171
|
-
end
|
172
|
-
|
173
|
-
it 'returns zero when the queue is empty' do
|
174
|
-
subject.length.should eq 0
|
175
|
-
end
|
176
|
-
|
177
|
-
it 'is aliased as #size' do
|
178
|
-
10.times{|i| subject << i}
|
179
|
-
subject.size.should eq 10
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
context '#peek' do
|
184
|
-
|
185
|
-
it 'returns the item at the head of the queue' do
|
186
|
-
10.times{|i| subject << i}
|
187
|
-
subject.peek.should eq 9
|
188
|
-
end
|
189
|
-
|
190
|
-
it 'does not remove the item from the queue' do
|
191
|
-
10.times{|i| subject << i}
|
192
|
-
subject.peek
|
193
|
-
subject.length.should eq 10
|
194
|
-
subject.should include(9)
|
195
|
-
end
|
196
|
-
|
197
|
-
it 'returns nil when the queue is empty' do
|
198
|
-
subject.peek.should be_nil
|
199
|
-
end
|
200
|
-
end
|
201
|
-
|
202
|
-
context '#pop' do
|
203
|
-
|
204
|
-
it 'returns the item at the head of the queue' do
|
205
|
-
10.times{|i| subject << i}
|
206
|
-
subject.pop.should eq 9
|
207
|
-
end
|
208
|
-
|
209
|
-
it 'removes the item from the queue' do
|
210
|
-
10.times{|i| subject << i}
|
211
|
-
subject.pop
|
212
|
-
subject.length.should eq 9
|
213
|
-
subject.should_not include(9)
|
214
|
-
end
|
215
|
-
|
216
|
-
it 'returns nil when the queue is empty' do
|
217
|
-
subject.pop.should be_nil
|
218
|
-
end
|
219
|
-
|
220
|
-
it 'is aliased as #deq' do
|
221
|
-
10.times{|i| subject << i}
|
222
|
-
subject.deq.should eq 9
|
223
|
-
end
|
224
|
-
|
225
|
-
it 'is aliased as #shift' do
|
226
|
-
10.times{|i| subject << i}
|
227
|
-
subject.shift.should eq 9
|
228
|
-
end
|
229
|
-
end
|
230
|
-
|
231
|
-
context '#push' do
|
232
|
-
|
233
|
-
it 'adds the item to the queue' do
|
234
|
-
subject.push(1)
|
235
|
-
subject.should include(1)
|
236
|
-
end
|
237
|
-
|
238
|
-
it 'sorts the new item in priority order' do
|
239
|
-
3.times{|i| subject << i}
|
240
|
-
subject.pop.should eq 2
|
241
|
-
subject.pop.should eq 1
|
242
|
-
subject.pop.should eq 0
|
243
|
-
end
|
244
|
-
|
245
|
-
it 'arbitrarily orders equal items with respect to each other' do
|
246
|
-
3.times{|i| subject << i}
|
247
|
-
subject.push(1)
|
248
|
-
subject.pop.should eq 2
|
249
|
-
subject.pop.should eq 1
|
250
|
-
subject.pop.should eq 1
|
251
|
-
subject.pop.should eq 0
|
252
|
-
end
|
253
|
-
|
254
|
-
specify { subject.push(10).should be_true }
|
255
|
-
|
256
|
-
it 'is aliased as <<' do
|
257
|
-
subject << 1
|
258
|
-
subject.should include(1)
|
259
|
-
end
|
260
|
-
|
261
|
-
it 'is aliased as enq' do
|
262
|
-
subject.enq(1)
|
263
|
-
subject.should include(1)
|
264
|
-
end
|
265
|
-
end
|
266
|
-
|
267
|
-
context '.from_list' do
|
268
|
-
|
269
|
-
it 'creates an empty queue from an empty list' do
|
270
|
-
subject = described_class.from_list([])
|
271
|
-
subject.should be_empty
|
272
|
-
end
|
273
|
-
|
274
|
-
it 'creates a sorted, populated queue from an Array' do
|
275
|
-
subject = described_class.from_list([2, 1, 4, 5, 3, 0])
|
276
|
-
subject.pop.should eq 5
|
277
|
-
subject.pop.should eq 4
|
278
|
-
subject.pop.should eq 3
|
279
|
-
subject.pop.should eq 2
|
280
|
-
subject.pop.should eq 1
|
281
|
-
subject.pop.should eq 0
|
282
|
-
end
|
283
|
-
|
284
|
-
it 'creates a sorted, populated queue from a Hash' do
|
285
|
-
subject = described_class.from_list(two: 2, one: 1, three: 3, zero: 0)
|
286
|
-
subject.length.should eq 4
|
287
|
-
end
|
288
|
-
end
|
289
|
-
end
|
290
|
-
|
291
|
-
module Concurrent
|
292
|
-
|
293
|
-
describe MutexPriorityQueue do
|
294
|
-
|
295
|
-
it_should_behave_like :priority_queue
|
296
|
-
end
|
297
|
-
|
298
|
-
if TestHelpers.jruby?
|
299
|
-
|
300
|
-
describe JavaPriorityQueue do
|
301
|
-
|
302
|
-
it_should_behave_like :priority_queue
|
303
|
-
end
|
304
|
-
end
|
305
|
-
|
306
|
-
describe PriorityQueue do
|
307
|
-
if jruby?
|
308
|
-
it 'inherits from JavaPriorityQueue' do
|
309
|
-
PriorityQueue.ancestors.should include(JavaPriorityQueue)
|
310
|
-
end
|
311
|
-
else
|
312
|
-
it 'inherits from MutexPriorityQueue' do
|
313
|
-
PriorityQueue.ancestors.should include(MutexPriorityQueue)
|
314
|
-
end
|
315
|
-
end
|
316
|
-
end
|
317
|
-
end
|