|
| 1 | +require 'spec_helper' |
| 2 | +require_relative 'global_thread_pool_shared' |
| 3 | + |
| 4 | +share_examples_for :executor_service do |
| 5 | + |
| 6 | + after(:each) do |
| 7 | + subject.kill |
| 8 | + sleep(0.1) |
| 9 | + end |
| 10 | + |
| 11 | + it_should_behave_like :global_thread_pool |
| 12 | + |
| 13 | + context '#post' do |
| 14 | + |
| 15 | + it 'rejects the block while shutting down' do |
| 16 | + latch = Concurrent::CountDownLatch.new(1) |
| 17 | + subject.post{ sleep(1) } |
| 18 | + subject.shutdown |
| 19 | + subject.post{ latch.count_down } |
| 20 | + latch.wait(0.1).should be_false |
| 21 | + end |
| 22 | + |
| 23 | + it 'returns false while shutting down' do |
| 24 | + subject.post{ sleep(1) } |
| 25 | + subject.shutdown |
| 26 | + subject.post{ nil }.should be_false |
| 27 | + end |
| 28 | + |
| 29 | + it 'rejects the block once shutdown' do |
| 30 | + subject.shutdown |
| 31 | + latch = Concurrent::CountDownLatch.new(1) |
| 32 | + subject.post{ sleep(1) } |
| 33 | + subject.post{ latch.count_down } |
| 34 | + latch.wait(0.1).should be_false |
| 35 | + end |
| 36 | + |
| 37 | + it 'returns false once shutdown' do |
| 38 | + subject.post{ nil } |
| 39 | + subject.shutdown |
| 40 | + sleep(0.1) |
| 41 | + subject.post{ nil }.should be_false |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + context '#running?' do |
| 46 | + |
| 47 | + it 'returns true when the thread pool is running' do |
| 48 | + subject.should be_running |
| 49 | + end |
| 50 | + |
| 51 | + it 'returns false when the thread pool is shutting down' do |
| 52 | + subject.post{ sleep(1) } |
| 53 | + subject.shutdown |
| 54 | + subject.wait_for_termination(1) |
| 55 | + subject.should_not be_running |
| 56 | + end |
| 57 | + |
| 58 | + it 'returns false when the thread pool is shutdown' do |
| 59 | + subject.shutdown |
| 60 | + subject.wait_for_termination(1) |
| 61 | + subject.should_not be_running |
| 62 | + end |
| 63 | + |
| 64 | + it 'returns false when the thread pool is killed' do |
| 65 | + subject.kill |
| 66 | + subject.wait_for_termination(1) |
| 67 | + subject.should_not be_running |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + context '#shutdown' do |
| 72 | + |
| 73 | + it 'stops accepting new tasks' do |
| 74 | + latch1 = Concurrent::CountDownLatch.new(1) |
| 75 | + latch2 = Concurrent::CountDownLatch.new(1) |
| 76 | + subject.post{ sleep(0.2); latch1.count_down } |
| 77 | + subject.shutdown |
| 78 | + subject.post{ latch2.count_down }.should be_false |
| 79 | + latch1.wait(1).should be_true |
| 80 | + latch2.wait(0.2).should be_false |
| 81 | + end |
| 82 | + |
| 83 | + it 'allows in-progress tasks to complete' do |
| 84 | + latch = Concurrent::CountDownLatch.new(1) |
| 85 | + subject.post{ sleep(0.1); latch.count_down } |
| 86 | + subject.shutdown |
| 87 | + latch.wait(1).should be_true |
| 88 | + end |
| 89 | + |
| 90 | + it 'allows pending tasks to complete' do |
| 91 | + latch = Concurrent::CountDownLatch.new(2) |
| 92 | + subject.post{ sleep(0.2); latch.count_down } |
| 93 | + subject.post{ sleep(0.2); latch.count_down } |
| 94 | + subject.shutdown |
| 95 | + latch.wait(1).should be_true |
| 96 | + end |
| 97 | + end |
| 98 | + |
| 99 | + context '#shutdown followed by #wait_for_termination' do |
| 100 | + |
| 101 | + it 'allows in-progress tasks to complete' do |
| 102 | + latch = Concurrent::CountDownLatch.new(1) |
| 103 | + subject.post{ sleep(0.1); latch.count_down } |
| 104 | + subject.shutdown |
| 105 | + subject.wait_for_termination(1) |
| 106 | + latch.wait(1).should be_true |
| 107 | + end |
| 108 | + |
| 109 | + it 'allows pending tasks to complete' do |
| 110 | + q = Queue.new |
| 111 | + 5.times do |i| |
| 112 | + subject.post { sleep 0.1; q << i } |
| 113 | + end |
| 114 | + subject.shutdown |
| 115 | + subject.wait_for_termination(1) |
| 116 | + q.length.should eq 5 |
| 117 | + end |
| 118 | + |
| 119 | + it 'stops accepting/running new tasks' do |
| 120 | + expected = Concurrent::AtomicFixnum.new(0) |
| 121 | + subject.post{ sleep(0.1); expected.increment } |
| 122 | + subject.post{ sleep(0.1); expected.increment } |
| 123 | + subject.shutdown |
| 124 | + subject.post{ expected.increment } |
| 125 | + subject.wait_for_termination(1) |
| 126 | + expected.value.should == 2 |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + context '#kill' do |
| 131 | + |
| 132 | + it 'stops accepting new tasks' do |
| 133 | + expected = Concurrent::AtomicBoolean.new(false) |
| 134 | + latch = Concurrent::CountDownLatch.new(1) |
| 135 | + subject.post{ sleep(0.1); latch.count_down } |
| 136 | + latch.wait(1) |
| 137 | + subject.kill |
| 138 | + subject.post{ expected.make_true }.should be_false |
| 139 | + sleep(0.1) |
| 140 | + expected.value.should be_false |
| 141 | + end |
| 142 | + |
| 143 | + it 'rejects all pending tasks' do |
| 144 | + subject.post{ sleep(1) } |
| 145 | + sleep(0.1) |
| 146 | + subject.kill |
| 147 | + sleep(0.1) |
| 148 | + subject.post{ nil }.should be_false |
| 149 | + end |
| 150 | + end |
| 151 | + |
| 152 | + context '#wait_for_termination' do |
| 153 | + |
| 154 | + it 'immediately returns true when no operations are pending' do |
| 155 | + subject.shutdown |
| 156 | + subject.wait_for_termination(0).should be_true |
| 157 | + end |
| 158 | + |
| 159 | + it 'returns true after shutdown has complete' do |
| 160 | + 10.times { subject << proc{ nil } } |
| 161 | + sleep(0.1) |
| 162 | + subject.shutdown |
| 163 | + subject.wait_for_termination(1).should be_true |
| 164 | + end |
| 165 | + |
| 166 | + it 'returns true when shutdown sucessfully completes before timeout' do |
| 167 | + subject.post{ sleep(0.5) } |
| 168 | + sleep(0.1) |
| 169 | + subject.shutdown |
| 170 | + subject.wait_for_termination(1).should be_true |
| 171 | + end |
| 172 | + |
| 173 | + it 'returns false when shutdown fails to complete before timeout' do |
| 174 | + unless subject.is_a?(Concurrent::SerialExecutor) |
| 175 | + 100.times{ subject.post{ sleep(1) } } |
| 176 | + sleep(0.1) |
| 177 | + subject.shutdown |
| 178 | + subject.wait_for_termination(0).should be_false |
| 179 | + end |
| 180 | + end |
| 181 | + end |
| 182 | +end |
0 commit comments