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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
# frozen_string_literal: true
# Copyright, 2017, 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'
require 'async/queue'
require 'async/rspec'
require 'async/semaphore'
require_relative 'condition_examples'
require_relative 'chainable_async_examples'
RSpec.shared_context Async::Queue do
it 'should process items in order' do
reactor.async do |task|
10.times do |i|
task.sleep(0.001)
subject.enqueue(i)
end
end
10.times do |j|
expect(subject.dequeue).to be == j
end
end
it 'can enqueue multiple items' do
items = Array.new(10) { rand(10) }
reactor.async do |task|
subject.enqueue(*items)
end
items.each do |item|
expect(subject.dequeue).to be == item
end
end
it 'can dequeue items asynchronously' do
reactor.async do |task|
subject << 1
subject << nil
end
subject.async do |task, item|
expect(item).to be 1
end
end
describe '#<<' do
it 'adds an item to the queue' do
subject << :item
expect(subject.size).to be == 1
expect(subject.dequeue).to be == :item
end
end
describe '#size' do
it 'returns queue size' do
reactor.async do |task|
10.times do |i|
subject.enqueue(i)
expect(subject.size).to be i + 1
end
end
end
end
context 'with an empty queue' do
it {is_expected.to be_empty}
end
context 'with semaphore' do
let(:capacity) {2}
let(:semaphore) {Async::Semaphore.new(capacity)}
let(:repeats) {capacity * 2}
it 'should process several items limited by a semaphore' do
count = 0
Async do
repeats.times do
subject.enqueue :item
end
subject.enqueue nil
end
subject.async(parent: semaphore) do |task|
count += 1
end
expect(count).to be == repeats
end
end
it_behaves_like 'chainable async' do
before do
subject.enqueue(:item)
# The limited queue may block.
Async do
subject.enqueue(nil)
end
end
end
end
RSpec.describe Async::Queue do
include_context Async::RSpec::Reactor
it_behaves_like Async::Queue
end
RSpec.describe Async::LimitedQueue do
include_context Async::RSpec::Reactor
it_behaves_like Async::Queue
it 'should become limited' do
expect(subject).to_not be_limited
subject.enqueue(10)
expect(subject).to be_limited
end
it 'enqueues items up to a limit' do
items = Array.new(2) { rand(10) }
reactor.async do
subject.enqueue(*items)
end
expect(subject.size).to be 1
expect(subject.dequeue).to be == items.first
end
it 'should resume waiting tasks in order' do
total_resumed = 0
total_dequeued = 0
Async do |producer|
10.times do
producer.async do
subject.enqueue('foo')
total_resumed += 1
end
end
end
10.times do
item = subject.dequeue
total_dequeued += 1
expect(total_resumed).to be == total_dequeued
end
end
describe '#<<' do
context 'when queue is limited' do
before do
subject << :item1
expect(subject.size).to be == 1
expect(subject).to be_limited
end
it 'waits until a queue is dequeued' do
reactor.async do
subject << :item2
end
reactor.async do |task|
task.sleep 0.01
expect(subject.items).to contain_exactly :item1
subject.dequeue
expect(subject.items).to contain_exactly :item2
end
end
end
end
end
|