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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
|
module Concurrent
RSpec.describe Async do
let(:async_class) do
Class.new do
include Concurrent::Async
attr_accessor :accessor
def initialize(*args)
end
def echo(msg)
msg
end
def gather(first, second = nil)
return first, second
end
def boom(ex = StandardError.new)
raise ex
end
def wait(seconds)
sleep(seconds)
end
def with_block
yield
end
end
end
subject do
async_class.new
end
context 'object creation' do
it 'delegates to the original constructor' do
args = [:foo, 'bar', 42]
expect(async_class).to receive(:original_new).once.with(*args).and_call_original
async_class.new(*args)
end
specify 'passes all args to the original constructor' do
clazz = Class.new do
include Concurrent::Async
attr_reader :args
def initialize(*args)
@args = args
end
end
object = clazz.new(:foo, :bar)
expect(object.args).to eq [:foo, :bar]
end
specify 'passes a given block to the original constructor' do
clazz = Class.new do
include Concurrent::Async
attr_reader :block
def initialize(&block)
@block = yield
end
end
object = clazz.new{ 42 }
expect(object.block).to eq 42
end
specify 'initializes synchronization' do
mock = async_class.new
allow(async_class).to receive(:original_new).and_return(mock)
expect(mock).to receive(:init_synchronization).once.with(no_args)
async_class.new
end
end
context '#validate_argc' do
subject do
Class.new {
def zero() nil; end
def three(a, b, c, &block) nil; end
def two_plus_two(a, b, c=nil, d=nil, &block) nil; end
def many(*args, &block) nil; end
}.new
end
it 'raises an exception when the method is not defined' do
expect {
Async::validate_argc(subject, :bogus)
}.to raise_error(StandardError)
end
it 'raises an exception for too many args on a zero arity method' do
expect {
Async::validate_argc(subject, :zero, 1, 2, 3)
}.to raise_error(ArgumentError)
end
it 'does not raise an exception for correct zero arity' do
expect {
Async::validate_argc(subject, :zero)
}.not_to raise_error
end
it 'raises an exception for too many args on a method with positive arity' do
expect {
Async::validate_argc(subject, :three, 1, 2, 3, 4)
}.to raise_error(ArgumentError)
end
it 'raises an exception for too few args on a method with positive arity' do
expect {
Async::validate_argc(subject, :three, 1, 2)
}.to raise_error(ArgumentError)
end
it 'does not raise an exception for correct positive arity' do
expect {
Async::validate_argc(subject, :three, 1, 2, 3)
}.not_to raise_error
end
it 'raises an exception for too few args on a method with negative arity' do
expect {
Async::validate_argc(subject, :two_plus_two, 1)
}.to raise_error(ArgumentError)
end
it 'does not raise an exception for correct negative arity' do
expect {
Async::validate_argc(subject, :two_plus_two, 1, 2)
Async::validate_argc(subject, :two_plus_two, 1, 2, 3, 4)
Async::validate_argc(subject, :two_plus_two, 1, 2, 3, 4, 5, 6)
Async::validate_argc(subject, :many)
Async::validate_argc(subject, :many, 1, 2)
Async::validate_argc(subject, :many, 1, 2, 3, 4)
}.not_to raise_error
end
end
context '#async' do
it 'raises an error when calling a method that does not exist' do
expect {
subject.async.bogus
}.to raise_error(StandardError)
end
it 'raises an error when passing too few arguments' do
expect {
subject.async.gather
}.to raise_error(ArgumentError)
end
it 'raises an error when pasing too many arguments (arity >= 0)' do
expect {
subject.async.echo(1, 2, 3, 4, 5)
}.to raise_error(StandardError)
end
it 'returns the existence of the method' do
expect(subject.async.respond_to?(:echo)).to be_truthy
expect(subject.async.respond_to?(:not_exist_method)).to be_falsy
end
it 'returns a :pending IVar' do
val = subject.async.wait(1)
expect(val).to be_a Concurrent::IVar
expect(val).to be_pending
end
it 'runs the future on the global executor' do
expect(Concurrent.global_io_executor).to receive(:post).with(any_args).
and_call_original
subject.async.echo(:foo)
end
it 'sets the value on success' do
val = subject.async.echo(:foo)
expect(val.value).to eq :foo
expect(val).to be_fulfilled
end
it 'sets the reason on failure' do
ex = ArgumentError.new
val = subject.async.boom(ex)
val.wait
expect(val.reason).to eq ex
expect(val).to be_rejected
end
it 'sets the reason when giving too many optional arguments' do
val = subject.async.gather(1, 2, 3, 4, 5)
val.wait
expect(val.reason).to be_a StandardError
expect(val).to be_rejected
end
it 'supports attribute accessors' do
subject.async.accessor = :foo
val = subject.async.accessor
expect(val.value).to eq :foo
expect(subject.accessor).to eq :foo
end
it 'supports methods with blocks' do
val = subject.async.with_block{ :foo }
expect(val.value).to eq :foo
end
end
context '#await' do
it 'raises an error when calling a method that does not exist' do
expect {
subject.await.bogus
}.to raise_error(StandardError)
end
it 'raises an error when passing too few arguments' do
expect {
subject.await.gather
}.to raise_error(ArgumentError)
end
it 'raises an error when pasing too many arguments (arity >= 0)' do
expect {
subject.await.echo(1, 2, 3, 4, 5)
}.to raise_error(StandardError)
end
it 'returns the existence of the method' do
expect(subject.await.respond_to?(:echo)).to be_truthy
expect(subject.await.respond_to?(:not_exist_method)).to be_falsy
end
it 'returns a :fulfilled IVar' do
val = subject.await.echo(5)
expect(val).to be_a Concurrent::IVar
expect(val).to be_fulfilled
end
it 'runs the future on the global executor' do
expect(Concurrent.global_io_executor).to receive(:post).with(any_args).
and_call_original
subject.await.echo(:foo)
end
it 'sets the value on success' do
val = subject.await.echo(:foo)
expect(val.value).to eq :foo
expect(val).to be_fulfilled
end
it 'sets the reason on failure' do
ex = ArgumentError.new
val = subject.await.boom(ex)
expect(val.reason).to eq ex
expect(val).to be_rejected
end
it 'sets the reason when giving too many optional arguments' do
val = subject.await.gather(1, 2, 3, 4, 5)
expect(val.reason).to be_a StandardError
expect(val).to be_rejected
end
it 'supports attribute accessors' do
subject.await.accessor = :foo
val = subject.await.accessor
expect(val.value).to eq :foo
expect(subject.accessor).to eq :foo
end
it 'supports methods with blocks' do
val = subject.await.with_block{ :foo }
expect(val.value).to eq :foo
end
end
context 'locking' do
it 'uses the same lock for both #async and #await' do
object = Class.new {
include Concurrent::Async
attr_reader :bucket
def gather(seconds, first, *rest)
sleep(seconds)
(@bucket ||= []).concat([first])
@bucket.concat(rest)
end
}.new
object.async.gather(0.5, :a, :b)
object.await.gather(0, :c, :d)
expect(object.bucket).to eq [:a, :b, :c, :d]
end
end
end
end
|