Skip to content

Commit d89e098

Browse files
Add :batcher option to test_batch/3 (#366)
1 parent 7094ce8 commit d89e098

2 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎lib/broadway.ex‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,18 @@ defmodule Broadway do
13221322
when set to `:flush`, the batch the message is in is immediately delivered. When set
13231323
to `:bulk`, batch is delivered when its size or timeout is reached.
13241324
"""
1325+
],
1326+
batcher: [
1327+
type: :atom,
1328+
default: :default,
1329+
doc: """
1330+
the name of the batcher assigned to the test message. This option is useful
1331+
when the producer's `:transform` option sets the batcher, as test messages
1332+
do not run through the producer. If `handle_message/3` sets the batcher, its
1333+
value replaces this one.
1334+
1335+
*Available since v1.3.0*.
1336+
"""
13251337
]
13261338
]
13271339

@@ -1426,6 +1438,7 @@ defmodule Broadway do
14261438

14271439
defp test_messages(broadway, data, batch_mode, opts) when is_broadway_name(broadway) do
14281440
metadata = opts |> Keyword.fetch!(:metadata) |> Map.new()
1441+
batcher = Keyword.fetch!(opts, :batcher)
14291442

14301443
acknowledger =
14311444
Keyword.get(opts, :acknowledger, fn _data, ack_ref ->
@@ -1437,7 +1450,14 @@ defmodule Broadway do
14371450
messages =
14381451
Enum.map(data, fn data ->
14391452
ack = acknowledger.(data, {self(), ref})
1440-
%Message{data: data, acknowledger: ack, batch_mode: batch_mode, metadata: metadata}
1453+
1454+
%Message{
1455+
data: data,
1456+
acknowledger: ack,
1457+
batch_mode: batch_mode,
1458+
metadata: metadata,
1459+
batcher: batcher
1460+
}
14411461
end)
14421462

14431463
:ok = push_messages(broadway, messages)

‎test/broadway_test.exs‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,23 @@ defmodule BroadwayTest do
791791
assert_receive {:ack, ^ref, [%{status: :ok}, %{status: :ok}], []}
792792
end
793793

794+
test "messages use the batcher set through the :batcher option" do
795+
broadway_name = new_unique_name()
796+
797+
{:ok, _broadway} =
798+
Broadway.start_link(Forwarder,
799+
name: broadway_name,
800+
context: %{test_pid: self()},
801+
producer: [module: {ManualProducer, []}],
802+
processors: [default: [concurrency: 1, min_demand: 1, max_demand: 2]],
803+
batchers: [default: [batch_size: 2], custom_batcher: [batch_size: 2]]
804+
)
805+
806+
ref = Broadway.test_batch(broadway_name, [1, 2], batcher: :custom_batcher)
807+
assert_receive {:batch_handled, :custom_batcher, [%{data: 1}, %{data: 2}]}
808+
assert_receive {:ack, ^ref, [%{data: 1, status: :ok}, %{data: 2, status: :ok}], []}
809+
end
810+
794811
test "failed messages are marked as {:failed, reason}", %{broadway: broadway} do
795812
ref = Broadway.test_message(broadway, :fail)
796813
assert_receive {:ack, ^ref, _, [%{status: {:failed, "Failed message"}}]}

0 commit comments

Comments
 (0)