Skip to content

Commit

Permalink
Slightly improve some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
whatyouhide committed Jun 4, 2023
1 parent 8bff7c1 commit 71f37c9
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions test/plug/conn_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -420,27 +420,32 @@ defmodule Plug.ConnTest do
|> send_chunked(200)
|> chunk("CHUNK")

assert_received {:before_chunk, 2, "CHUNK"}
assert_received {:before_chunk, 1, "CHUNK"}
# We need to match with n1 and n2 because if we match directly on 1 and 2 then
# selective receive will match on the messages even if they're out of order.
assert_receive {:before_chunk, n1, "CHUNK"}
assert_receive {:before_chunk, n2, "CHUNK"}
assert n1 == 2
assert n2 == 1
end

test "chunk/2 uses the updated conn from before_chunk callbacks" do
pid = self()

conn =
conn(:get, "/foo")
|> assign(:test_counter, 0)
|> register_before_chunk(fn conn, _chunk ->
{count, conn} = get_and_update_in(conn.assigns[:test_counter], &{&1, (&1 || 0) + 1})
{count, conn} = get_and_update_in(conn.assigns[:test_counter], &{&1, &1 + 1})
send(pid, {:before_chunk, count})
conn
end)
|> send_chunked(200)

{:ok, conn} = chunk(conn, "CHUNK")
{:ok, conn} = chunk(conn, "CHUNK")
{:ok, _} = chunk(conn, "CHUNK")
assert {:ok, conn} = chunk(conn, "CHUNK")
assert {:ok, conn} = chunk(conn, "CHUNK")
assert {:ok, _conn} = chunk(conn, "CHUNK")

assert_received {:before_chunk, nil}
assert_received {:before_chunk, 0}
assert_received {:before_chunk, 1}
assert_received {:before_chunk, 2}
end
Expand Down

0 comments on commit 71f37c9

Please sign in to comment.