async 1.18.0 → 1.19.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +14 -21
- data/.travis.yml +1 -0
- data/lib/async/semaphore.rb +5 -4
- data/lib/async/task.rb +7 -1
- data/lib/async/version.rb +1 -1
- data/spec/async/queue_spec.rb +13 -0
- data/spec/async/semaphore_spec.rb +15 -2
- data/spec/async/task_spec.rb +26 -1
- data/spec/async_spec.rb +39 -0
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0bbe9b738c758dc531ae3a13f10a4a5bdcfdadaf29e3dc51bc11e2daca28781d
|
4
|
+
data.tar.gz: 31872ca49209a1038eb02ec0497bf968c68c84122c81f3bafd3b87773c653f1d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2caef4acc794a33d79a7f74a7bf98642d5b882e406a31cca62a7714fa4933123ff9565c769fe2aaf88e71f1ae34499cff847402fa07194c85dd70f61fa19b594
|
7
|
+
data.tar.gz: 93a95a79cd6db4646108863121b60406f82e2ab4142ebba4108ac0cc7b23eec271d5a1cf4f1c9c7d3939ae17e3aba402f6f729a7bc3d56770fae49dac430ebe3
|
data/.gitignore
CHANGED
@@ -1,21 +1,14 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
.
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
test/tmp
|
16
|
-
test/version_tmp
|
17
|
-
tmp
|
18
|
-
.tags*
|
19
|
-
documentation/run/*
|
20
|
-
documentation/public/code/*
|
21
|
-
.rspec_status
|
1
|
+
.tags
|
2
|
+
|
3
|
+
/.bundle/
|
4
|
+
/.yardoc
|
5
|
+
/Gemfile.lock
|
6
|
+
/_yardoc/
|
7
|
+
/coverage/
|
8
|
+
/doc/
|
9
|
+
/pkg/
|
10
|
+
/spec/reports/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
.rspec_status
|
14
|
+
.covered.db
|
data/.travis.yml
CHANGED
data/lib/async/semaphore.rb
CHANGED
@@ -33,6 +33,9 @@ module Async
|
|
33
33
|
# The maximum number of tasks that can acquire the semaphore.
|
34
34
|
attr :limit
|
35
35
|
|
36
|
+
# The tasks waiting on this semaphore.
|
37
|
+
attr :waiting
|
38
|
+
|
36
39
|
# Is the semaphore currently acquired?
|
37
40
|
def empty?
|
38
41
|
@count.zero?
|
@@ -95,12 +98,10 @@ module Async
|
|
95
98
|
def wait
|
96
99
|
fiber = Fiber.current
|
97
100
|
|
98
|
-
|
101
|
+
if blocking?
|
99
102
|
@waiting << fiber
|
100
|
-
Task.yield
|
103
|
+
Task.yield while blocking?
|
101
104
|
end
|
102
|
-
|
103
|
-
# ensure when raise, throw
|
104
105
|
rescue Exception
|
105
106
|
@waiting.delete(fiber)
|
106
107
|
raise
|
data/lib/async/task.rb
CHANGED
@@ -136,7 +136,9 @@ module Async
|
|
136
136
|
def stop
|
137
137
|
@children&.each(&:stop)
|
138
138
|
|
139
|
-
if
|
139
|
+
if self.current?
|
140
|
+
raise Stop, "Stopping current fiber!"
|
141
|
+
elsif @fiber.alive?
|
140
142
|
@fiber.resume(Stop.new)
|
141
143
|
end
|
142
144
|
end
|
@@ -154,6 +156,10 @@ module Async
|
|
154
156
|
Thread.current[:async_task]
|
155
157
|
end
|
156
158
|
|
159
|
+
def current?
|
160
|
+
self.equal?(Thread.current[:async_task])
|
161
|
+
end
|
162
|
+
|
157
163
|
# Check if the task is running.
|
158
164
|
# @return [Boolean]
|
159
165
|
def running?
|
data/lib/async/version.rb
CHANGED
data/spec/async/queue_spec.rb
CHANGED
@@ -18,7 +18,9 @@
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
19
|
# THE SOFTWARE.
|
20
20
|
|
21
|
+
require 'async'
|
21
22
|
require 'async/queue'
|
23
|
+
require 'async/rspec'
|
22
24
|
|
23
25
|
require_relative 'condition_examples'
|
24
26
|
|
@@ -35,6 +37,17 @@ RSpec.shared_context Async::Queue do
|
|
35
37
|
expect(subject.dequeue).to be == j
|
36
38
|
end
|
37
39
|
end
|
40
|
+
|
41
|
+
it 'can dequeue items asynchronously' do
|
42
|
+
reactor.async do |task|
|
43
|
+
subject << 1
|
44
|
+
subject << nil
|
45
|
+
end
|
46
|
+
|
47
|
+
subject.async do |task, item|
|
48
|
+
expect(item).to be 1
|
49
|
+
end
|
50
|
+
end
|
38
51
|
end
|
39
52
|
|
40
53
|
RSpec.describe Async::Queue do
|
@@ -19,8 +19,7 @@
|
|
19
19
|
# THE SOFTWARE.
|
20
20
|
|
21
21
|
require 'async/semaphore'
|
22
|
-
|
23
|
-
require_relative 'condition_examples'
|
22
|
+
require 'async/rspec'
|
24
23
|
|
25
24
|
RSpec.describe Async::Semaphore do
|
26
25
|
include_context Async::RSpec::Reactor
|
@@ -82,6 +81,20 @@ RSpec.describe Async::Semaphore do
|
|
82
81
|
end
|
83
82
|
end
|
84
83
|
|
84
|
+
context '#waiting' do
|
85
|
+
subject {Async::Semaphore.new(0)}
|
86
|
+
it 'handles exceptions thrown while waiting' do
|
87
|
+
expect do
|
88
|
+
reactor.with_timeout(0.1) do
|
89
|
+
subject.acquire do
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end.to raise_error(Async::TimeoutError)
|
93
|
+
|
94
|
+
expect(subject.waiting).to be_empty
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
85
98
|
context '#count' do
|
86
99
|
it 'should count number of current acquisitions' do
|
87
100
|
expect(subject.count).to be == 0
|
data/spec/async/task_spec.rb
CHANGED
@@ -18,7 +18,7 @@
|
|
18
18
|
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
19
|
# THE SOFTWARE.
|
20
20
|
|
21
|
-
require 'async
|
21
|
+
require 'async'
|
22
22
|
require 'async/clock'
|
23
23
|
|
24
24
|
RSpec.describe Async::Task do
|
@@ -33,6 +33,17 @@ RSpec.describe Async::Task do
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
+
describe '#current?' do
|
37
|
+
it "can check if it is the currently running task" do
|
38
|
+
task = reactor.async do |task|
|
39
|
+
expect(task).to be_current
|
40
|
+
task.sleep(0.1)
|
41
|
+
end
|
42
|
+
|
43
|
+
expect(task).to_not be_current
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
36
47
|
describe '#async' do
|
37
48
|
it "can start child async tasks" do
|
38
49
|
child = nil
|
@@ -134,6 +145,20 @@ RSpec.describe Async::Task do
|
|
134
145
|
task.stop
|
135
146
|
|
136
147
|
expect(state).to be == :started
|
148
|
+
expect(task).to be_stopped
|
149
|
+
end
|
150
|
+
|
151
|
+
it "can stop current task" do
|
152
|
+
state = nil
|
153
|
+
|
154
|
+
task = reactor.async do |task|
|
155
|
+
state = :started
|
156
|
+
task.stop
|
157
|
+
state = :finished
|
158
|
+
end
|
159
|
+
|
160
|
+
expect(state).to be == :started
|
161
|
+
expect(task).to be_stopped
|
137
162
|
end
|
138
163
|
|
139
164
|
it "should kill direct child" do
|
data/spec/async_spec.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# Copyright, 2019, by Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'async'
|
22
|
+
|
23
|
+
RSpec.describe Async do
|
24
|
+
describe '#Async' do
|
25
|
+
it "can run an asynchronous task" do
|
26
|
+
Async do |task|
|
27
|
+
expect(task).to be_a Async::Task
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
describe '.run' do
|
33
|
+
it "can run an asynchronous task" do
|
34
|
+
Async.run do |task|
|
35
|
+
expect(task).to be_a Async::Task
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: async
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.19.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Samuel Williams
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: nio4r
|
@@ -180,6 +180,7 @@ files:
|
|
180
180
|
- spec/async/semaphore_spec.rb
|
181
181
|
- spec/async/task_spec.rb
|
182
182
|
- spec/async/wrapper_spec.rb
|
183
|
+
- spec/async_spec.rb
|
183
184
|
- spec/enumerator_spec.rb
|
184
185
|
- spec/spec_helper.rb
|
185
186
|
homepage: https://github.com/socketry/async
|
@@ -219,5 +220,6 @@ test_files:
|
|
219
220
|
- spec/async/semaphore_spec.rb
|
220
221
|
- spec/async/task_spec.rb
|
221
222
|
- spec/async/wrapper_spec.rb
|
223
|
+
- spec/async_spec.rb
|
222
224
|
- spec/enumerator_spec.rb
|
223
225
|
- spec/spec_helper.rb
|