Skip to content

Commit

Permalink
100% branch coverage of SGML#await
Browse files Browse the repository at this point in the history
  • Loading branch information
joeldrapper committed Sep 18, 2024
1 parent 84ff4e6 commit 718a9ca
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 17 deletions.
5 changes: 3 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ group :test do
gem "sus"
gem "quickdraw", github: "joeldrapper/quickdraw"
gem "simplecov", require: false
gem "async" if RUBY_ENGINE == "ruby"
gem "concurrent-ruby"
gem "selenium-webdriver"
end

Expand All @@ -20,3 +18,6 @@ group :development do
gem "yard"
gem "benchmark-ips"
end

gem "concurrent-ruby"
gem "async" if RUBY_ENGINE == "ruby"
61 changes: 46 additions & 15 deletions quickdraw/sgml/await.test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,70 @@ def view_template
end
end

# The Async gem only works with CRuby 3.0 and above.
if RUBY_ENGINE == "ruby" && RUBY_VERSION[0] > "3"
test "async task flushes when waiting" do
test "when given a non async task" do
expect { Example.new("Hello").call }.to_raise(Phlex::ArgumentError) do |error|
expect(error.message) == "Expected an asynchronous task / promise."
end
end

test "async task flushes when waiting" do
pid = Process.fork do
require "async"

Sync do
task = Async { sleep 0.01; "Hello" }
buffer = []
Example.new(task).call(buffer)
expect(buffer) == (["<h1>Before</h1>", "<h1>Hello</h1><h1>After</h1>"])
buffer => ["<h1>Before</h1>", "<h1>Hello</h1><h1>After</h1>"]
end
end

test "async task doesn't flush when it doesn't need to wait" do
pid, result = Process.wait2(pid)

assert result.success?
end

test "async task doesn't flush when it doesn't need to wait" do
pid = Process.fork do
require "async"

Sync do
task = Async { sleep 0.01; "Hello" }
task.wait
buffer = []
Example.new(task).call(buffer)
expect(buffer) == (["<h1>Before</h1><h1>Hello</h1><h1>After</h1>"])
buffer => ["<h1>Before</h1><h1>Hello</h1><h1>After</h1>"]
end
end

pid, result = Process.wait2(pid)

assert result.success?
end

test "concurrent promise flushes when waiting" do
task = Concurrent::Promise.execute { sleep 0.01; "Hello" }
buffer = []
Example.new(task).call(buffer)
expect(buffer) == (["<h1>Before</h1>", "<h1>Hello</h1><h1>After</h1>"])
pid = Process.fork do
require "concurrent"
task = Concurrent::Promise.execute { sleep 0.01; "Hello" }
buffer = []
Example.new(task).call(buffer)
buffer => ["<h1>Before</h1>", "<h1>Hello</h1><h1>After</h1>"]
end

pid, result = Process.wait2(pid)
assert result.success?
end

test "concurrent promise doesn't flush when it doesn't need to wait" do
task = Concurrent::Promise.execute { sleep 0.01; "Hello" }
task.wait
buffer = []
Example.new(task).call(buffer)
expect(buffer) == (["<h1>Before</h1><h1>Hello</h1><h1>After</h1>"])
pid = Process.fork do
require "concurrent"
task = Concurrent::Promise.execute { sleep 0.01; "Hello" }
task.wait
buffer = []
Example.new(task).call(buffer)
buffer => ["<h1>Before</h1><h1>Hello</h1><h1>After</h1>"]
end

pid, result = Process.wait2(pid)
assert result.success?
end

0 comments on commit 718a9ca

Please sign in to comment.