Skip to content
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

minimal fix for issue 97 #99

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/sweet_xml.ex
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ defmodule SweetXml do
{ref, pid, monref, do_after} ->
_ = Process.demonitor(monref)
_ = SweetXml.Options.clean_up(do_after)
flush_halt(pid, ref)
flush_halt(pid, ref, monref)
end
end

Expand Down Expand Up @@ -545,7 +545,7 @@ defmodule SweetXml do
{ref, pid, monref, do_after} ->
_ = Process.demonitor(monref)
_ = SweetXml.Options.clean_up(do_after)
flush_halt(pid, ref)
flush_halt(pid, ref, monref)
end
end

Expand Down Expand Up @@ -843,12 +843,20 @@ defmodule SweetXml do
end
end

defp flush_halt(pid, ref) do
defp flush_halt(pid, ref, monref) do
receive do
{:event, ^ref, _} ->
flush_halt(pid, ref) # flush all emitted elems after :halt
# flush all emitted elems after :halt
flush_halt(pid, ref, monref)

{:wait, ^ref} ->
send(pid, {:halt, ref}) # tell the continuation function to halt the underlying stream
# tell the continuation function to halt the underlying stream
send(pid, {:halt, ref})
:done

{:DOWN, ^monref, :process, ^pid, :normal} ->
# flush down message from possibly dead process before demonitoring
:done
end
end

Expand Down
29 changes: 29 additions & 0 deletions test/issue_97_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
defmodule Issue97Test do
use ExUnit.Case

test "stream_tags doesn't hang when used with stream take on empty xml" do
Comment on lines +1 to +4
Copy link
Member

@half-shell half-shell Feb 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
defmodule Issue97Test do
use ExUnit.Case
test "stream_tags doesn't hang when used with stream take on empty xml" do
defmodule SweetXmlTest do
use ExUnit.Case
describe "stream_tags/3" do
# c.f. https://github.com/kbrw/sweet_xml/issues/97
test "should not hang when used with Stream.take/2 on an empty xml" do

💅
While I do like the fact that this helps linking these tests to the issue raised to gain an understanding of the reason it was added, i think having a meaningful test module name might be better for overall comprehension and reading of test files.
Especially considering there are several ways of achieving the same results through other means; for instance, the commit itself already have that information. We could even put a link to the Github issue in the commit description, and / or even in a @doc or @docmodule attribute here.

parent = self()
ref = make_ref()
spawn_link fn ->
"<feed></feed>"
|> SweetXml.stream_tags(:feed)
|> Stream.take(1)
|> Enum.to_list()
send(parent, {ref, :ok})
end
assert_receive {^ref, :ok}, :timer.seconds(1)
end

test "stream_tags! doesn't hang when used with stream take on empty xml" do
parent = self()
ref = make_ref()
spawn_link fn ->
"<feed></feed>"
|> SweetXml.stream_tags!(:feed)
|> Stream.take(1)
|> Enum.to_list()
send(parent, {ref, :ok})
end
assert_receive {^ref, :ok}, :timer.seconds(1)
end
end