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
|
# frozen_string_literal: true
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
require 'async/semaphore'
require 'async/barrier'
require 'async/rspec'
require_relative 'chainable_async_examples'
RSpec.describe Async::Semaphore do
include_context Async::RSpec::Reactor
context '#async' do
let(:repeats) {40}
let(:limit) {4}
it 'should process work in batches' do
semaphore = Async::Semaphore.new(limit)
current, maximum = 0, 0
result = repeats.times.map do |i|
semaphore.async do |task|
current += 1
maximum = [current, maximum].max
task.sleep(rand * 0.1)
current -= 1
i
end
end.collect(&:result)
# Verify that the maximum number of concurrent tasks was the specificed limit:
expect(maximum).to be == limit
# Verify that the results were in the correct order:
expect(result).to be == (0...repeats).to_a
end
it 'only allows one task at a time' do
semaphore = Async::Semaphore.new(1)
order = []
3.times.map do |i|
semaphore.async do |task|
order << i
task.sleep(0.1)
order << i
end
end.collect(&:result)
expect(order).to be == [0, 0, 1, 1, 2, 2]
end
it 'allows tasks to execute concurrently' do
semaphore = Async::Semaphore.new(3)
order = []
3.times.map do |i|
semaphore.async do |task|
order << i
task.sleep(0.1)
order << i
end
end.collect(&:result)
expect(order).to be == [0, 1, 2, 0, 1, 2]
end
end
context '#waiting' do
subject {Async::Semaphore.new(0)}
it 'handles exceptions thrown while waiting' do
expect do
reactor.with_timeout(0.1) do
subject.acquire do
end
end
end.to raise_error(Async::TimeoutError)
expect(subject.waiting).to be_empty
end
end
context '#count' do
it 'should count number of current acquisitions' do
expect(subject.count).to be == 0
subject.acquire do
expect(subject.count).to be == 1
end
end
end
context '#limit' do
it 'should have a default limit' do
expect(subject.limit).to be == 1
end
end
context '#empty?' do
it 'should be empty unless acquired' do
expect(subject).to be_empty
subject.acquire do
expect(subject).to_not be_empty
end
end
end
context '#blocking?' do
it 'will be blocking when acquired' do
expect(subject).to_not be_blocking
subject.acquire do
expect(subject).to be_blocking
end
end
end
context '#acquire/#release' do
it 'works when called without block' do
subject.acquire
expect(subject.count).to be == 1
subject.release
expect(subject.count).to be == 0
end
end
context 'with barrier' do
let(:capacity) {2}
let(:barrier) {Async::Barrier.new}
let(:repeats) {capacity * 2}
it 'should execute several tasks and wait using a barrier' do
repeats.times do
subject.async(parent: barrier) do |task|
task.sleep 0.1
end
end
expect(barrier.size).to be == repeats
barrier.wait
end
end
it_behaves_like 'chainable async'
end
|