-
Notifications
You must be signed in to change notification settings - Fork 18
feat: Add thread reader to client #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
@Exterm1nate thank you for raising the issue and working on this contribution. I would prefer to not expose the thread as that's an implementation detail, and it enables a lot of misuse. I've updated the PR to add a Would this work for you? |
This solution appears to work, but it has a noticeable disadvantage: tests will take longer if we wait for the last |
I wouldn't expect this to take long at all. That method should early exit when it sees we have called close. This makes me think something else is preventing the thread from exiting immediately, like a hung IO reader. Do you have a small reproduction case I could reference to dig into this further? |
This code emulates my test suite: RSpec.describe "stream processor" do
context "with stream" do
subject(:call_method) do
stream
# Wait for SSE client preparations
sleep 0.1
end
let!(:obj) { instance_double("object", success: nil, failure: nil) }
let(:stream) do
stream = SSE::Client.new("http://localhost:3000")
stream.on_event do |event|
# Success logic
obj.success
end
stream.on_error do |e|
# Fail logic
obj.failure
end
stream
end
before do
stub_request(:get, "http://localhost:3000").to_return({
body: "event: created\ndata: my_data\n\n",
headers: { "Content-Type" => "text/event-stream" },
})
end
after do
# Uncomment one of:
# stream.close
# stream.close_and_wait
end
10.times do |i|
it "executes success callback [#{i}]" do
call_method
expect(obj).to have_received(:success)
end
end
end
context "without stream" do
10.times do |i|
it "does nothing [#{i}]" do
sleep 0.3
expect(true).to be(true)
end
end
end
end Context When using
When using When using The performance difference between If you don't want to expose the thread (and I agree with it, it's an implementation detail that should be private) maybe we can add a method, that immediately stops the stream? Like |
When stopping the client its thread continues working for some time. This is not a problem in production, but in tests it is. Webmock removes its stub after the test, while the thread is still working, so we sometimes receive such errors:
This PR adds a reader for thread, so it can be killed after the test example.