Closed schmijos closed 7 months ago
Using an around hook instead of a single step works correctly without warning.
Around("@connected") do |scenario, block|
Async do
@endpoint = Async::HTTP::Endpoint.parse("ws://127.0.0.1:3000")
Async::WebSocket::Client.connect(@endpoint) do |connection|
@connection = connection
block.call
end
end
end
I guess this is because connect
ensures connection closing itself if called with a block?
The following works as well
Around("@connected") do |scenario, block|
Async do
@endpoint = Async::HTTP::Endpoint.parse("ws://127.0.0.1:3000")
@connection = Async::WebSocket::Client.connect(@endpoint)
block.call
ensure
@connection.close
end
end
And this more generic version works as well.
But it bugs me that I cannot find out how to generically delay the Async::Stop
until all the work is done.
Given(/^the client is connected to the relay$/) do
@endpoint = Async::HTTP::Endpoint.parse("ws://127.0.0.1:3000")
@connection = Async::WebSocket::Client.connect(@endpoint)
end
Around do |scenario, block|
Async do
block.call
ensure
@connection&.close
end
end
As you figured out, the connection is closed automatically when it goes out of scope. But the connection is still in use by the WebSocket instance. So, a warning is issued that we are closing the HTTP connection while it still appears to be in use (i.e. not checked back into the connection pool).
As you implemented, calling @connection.close
is the correct solution, as this closes the WebSocket correctly and returns the HTTP connection to the connection pool.
Async::Stop
is used at the end of the top level Async{}
block to shut down all tasks. There is no way to persist a task outside of the Async{}
block by design. However, the Fiber.scheduler
interface does not enforce this. Generally speaking, you don't want to leak resources between tests, so this should be the right behaviour.
Does that answer your question?
Yes, thank you very much! I ended up with the following "idiomatic" solution to Cucumber testing with Async:
"Configure" Async support for Cucumber:
# features/support/env.rb
Around do |_scenario, block|
Async { block.call }
end
Use Async-dependent tools like Async::WebSocket
in step definitions
# features/step_definitions/connection.rb
Given(/^the client is connected to the relay$/) do
@endpoint = Async::HTTP::Endpoint.parse("ws://127.0.0.1:3000")
@connection = Async::WebSocket::Client.connect(@endpoint)
end
After do
@connection&.close
end
I've got the following Cucumber step definitions implemented:
Running them returns the following stdout:
The assertion passes. Everything works as expected. But I don't like this warning. Where does this come from?