8000 More generic implementation, add Async `stable-v1` and `head` to the … · envoy/async-rspec@7352121 · GitHub
[go: up one dir, main page]

8000
Skip to content
This repository was archived by the owner on Jul 8, 2025. It is now read-only.

Commit 7352121

Browse files
committed
More generic implementation, add Async stable-v1 and head to the test matrix.
1 parent f3d6327 commit 7352121

File tree

8 files changed

+144
-33
lines changed

8 files changed

+144
-33
lines changed

.github/workflows/async-head.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Async head
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ${{matrix.os}}-latest
8+
9+
strategy:
10+
matrix:
11+
os:
12+
- ubuntu
13+
14+
ruby:
15+
- head
16+
17+
env:
18+
BUNDLE_GEMFILE: gems/async-head.rb
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
- uses: ruby/setup-ruby@v1
23+
with:
24+
ruby-version: ${{matrix.ruby}}
25+
bundler-cache: true
26+
27+
- name: Run tests
28+
timeout-minutes: 5
29+
run: bundle exec rspec

.github/workflows/async-v1.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Async v1
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
test:
7+
runs-on: ${{matrix.os}}-latest
8+
9+
strategy:
10+
matrix:
11+
os:
12+
- ubuntu
13+
14+
ruby:
15+
- 2.7
16+
17+
env:
18+
BUNDLE_GEMFILE: gems/async-v1.rb
19+
20+
steps:
21+
- uses: actions/checkout@v2
22+
- uses: ruby/setup-ruby@v1
23+
with:
24+
ruby-version: ${{matrix.ruby}}
25+
bundler-cache: true
26+
27+
- name: Run tests
28+
timeout-minutes: 5
29+
run: bundle exec rspec

.github/workflows/documentation.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Documentation
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
8+
jobs:
9+
deploy:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- uses: actions/checkout@v2
14+
- uses: ruby/setup-ruby@v1
15+
env:
16+
BUNDLE_WITH: maintenance
17+
with:
18+
ruby-version: 2.7
19+
bundler-cache: true
20+
21+
- name: Installing packages
22+
run: sudo apt-get install wget
23+
24+
- name: Generate documentation
25+
timeout-minutes: 5
26+
run: bundle exec bake utopia:project:static
27+
28+
- name: Deploy documentation
29+
uses: JamesIves/github-pages-deploy-action@4.0.0
30+
with:
31+
branch: docs
32+
folder: docs

async-rspec.gemspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
1717
spec.add_dependency "rspec-files", "~> 1.0"
1818
spec.add_dependency "rspec-memory", "~> 1.0"
1919

20-
spec.add_development_dependency "async", "~> 1.24"
20+
spec.add_development_dependency "async"
2121
spec.add_development_dependency "async-io"
2222
spec.add_development_dependency "bundler"
2323
spec.add_development_dependency "covered"

gems/async-head.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
gemspec path: "../"
6+
7+
gem 'async', git: "https://github.com/socketry/async"

gems/async- F438 v1.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# frozen_string_literal: true
2+
3+
source 'https://rubygems.org'
4+
5+
gemspec path: "../"
6+
7+
gem 'async', '~> 1.0'

lib/async/rspec/reactor.rb

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,10 @@
2020

2121
require_relative 'leaks'
2222

23+
require 'kernel/sync'
2324
require 'kernel/async'
2425
require 'async/reactor'
26+
require 'async/task'
2527

2628
module Async
2729
module RSpec
@@ -32,64 +34,67 @@ def notify_failure(exception = $!)
3234

3335
def run_in_reactor(reactor, duration = nil)
3436
result = nil
35-
3637
timer_task = nil
3738

3839
if duration
3940
timer_task = reactor.async do |task|
4041
# Wait for the timeout, at any point this task might be cancelled if the user code completes:
41-
task.annotate("timer task duration=#{duration}")
42+
task.annotate("Timer task duration=#{duration}.")
4243
task.sleep(duration)
4344

4445
# The timeout expired, so generate an error:
4546
buffer = StringIO.new
4647
reactor.print_hierarchy(buffer)
4748

4849
# Raise an error so it is logged:
49-
raise TimeoutError, "run time exceeded duration #{duration}s:\r\n#{buffer.string}"
50+
raise TimeoutError, "Run time exceeded duration #{duration}s:\n#{buffer.string}"
5051
end
5152
end
5253

53-
reactor.run do |task|
54-
task.annotate(self.class)
54+
spec_task = reactor.async do |spec_task|
55+
spec_task.annotate("running example")
5556

56-
spec_task = task.async do |spec_task|
57-
spec_task.annotate("running example")
58-
59-
result = yield
60-
61-
timer_task&.stop
62-
63-
raise Async::Stop
64-
end
57+
result = yield(spec_task)
6558

66-
begin
67-
timer_task&.wait
68-
spec_task.wait
69-
ensure
70-
spec_task.stop
71-
end
72-
end.wait
59+
# We are finished, so stop the timer task if it was started:
60+
timer_task&.stop
61+
62+
# Now stop the entire reactor:
63+
raise Async::Stop
64+
end
65+
66+
begin
67+
timer_task&.wait
68+
spec_task.wait
69+
ensure
70+
spec_task.stop
71+
end
7372

7473
return result
7574
end
7675
end
7776

7877
::RSpec.shared_context Reactor do
7978
include Reactor
80-
let(:reactor) {Async::Reactor.new}
79+
let(:reactor) {@reactor}
8180

8281
include_context Async::RSpec::Leaks
8382

8483
around(:each) do |example|
8584
duration = example.metadata.fetch(:timeout, 10)
8685

8786
begin
88-
run_in_reactor(reactor, duration) do
89-
example.run
87+
Sync do |task|
88+
@reactor = task.reactor
89+
90+
task.annotate(self.class)
91+
92+
run_in_reactor(@reactor, duration) do
93+
example.run
94+
end
95+
ensure
96+
@reactor = nil
9097
end
91-
ensure
92-
reactor.close
9398
end
9499
end
95100
end

spec/async/rspec/reactor_spec.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,20 +74,22 @@
7474
context "timeouts", timeout: 1 do
7575
include Async::RSpec::Reactor
7676

77-
let(:reactor) {Async::Reactor.new}
78-
7977
it "times out" do
8078
expect do
81-
run_in_reactor(reactor, 0.05) do
82-
Async::Task.current.sleep(0.1)
79+
Sync do |task|
80+
run_in_reactor(task.reactor, 0.05) do |spec_task|
81+
spec_task.sleep(0.1)
82+
end
8383
end
8484
end.to raise_error(Async::TimeoutError)
8585
end
8686

8787
it "doesn't time out" do
8888
expect do
89-
run_in_reactor(reactor, 0.05) do
90-
Async::Task.current.sleep(0.01)
89+
Sync do |task|
90+
run_in_reactor(task.reactor, 0.05) do |spec_task|
91+
spec_task.sleep(0.01)
92+
end
9193
end
9294
end.to_not raise_error
9395
end

0 commit comments

Comments
 (0)
0