Repository: plataformatec/broadway
Pull request #17 (2019-01-12 16:00:20)

Fix example comment
Adds `alias` so that the `Message` function can be used and the example comment will compile.


Patch:
diff --git a/lib/broadway.ex b/lib/broadway.ex
index a373225..d305920 100644
--- a/lib/broadway.ex
+++ b/lib/broadway.ex
@@ -88,6 +88,7 @@ defmodule Broadway do
 
       defmodule MyBroadway do
         use Broadway
+        alias Broadway.Message
 
         ...start_link...
 
@@ -95,8 +96,8 @@ defmodule Broadway do
         def handle_message(%Message{data: data} = message, _) when is_odd(data) do
           new_message =
             message
-            |> update_data(&process_data/1)
-            |> put_publisher(:sqs)
+            |> Message.update_data(&process_data/1)
+            |> Message.put_publisher(:sqs)
 
           {:ok, new_message}
         end
@@ -104,8 +105,8 @@ defmodule Broadway do
         def handle_message(%Message{data: data} = message, _context) do
           new_message =
             message
-            |> update_data(&process_data/1)
-            |> put_publisher(:s3)
+            |> Message.update_data(&process_data/1)
+            |> Message.put_publisher(:s3)
 
           {:ok, new_message}
         end
diff --git a/lib/broadway/producer.ex b/lib/broadway/producer.ex
index 056c8cb..e51276d 100644
--- a/lib/broadway/producer.ex
+++ b/lib/broadway/producer.ex
@@ -49,6 +49,10 @@ defmodule Broadway.Producer do
 
   @impl true
   def terminate(reason, %{module: module, module_state: module_state}) do
-    module.terminate(reason, module_state)
+    if function_exported?(module, :terminate, 2) do
+      module.terminate(reason, module_state)
+    else
+      :ok
+    end
   end
 end


Test patch:
diff --git a/test/broadway/producer_test.exs b/test/broadway/producer_test.exs
index 27bcd9a..d2911c7 100644
--- a/test/broadway/producer_test.exs
+++ b/test/broadway/producer_test.exs
@@ -37,6 +37,12 @@ defmodule Broadway.ProducerTest do
     end
   end
 
+  defmodule ProducerWithOutTerminate do
+    use GenStage
+
+    def init(_), do: {:producer, nil}
+  end
+
   describe "wrap handle_demand" do
     test "returning {:noreply, [event], new_state}" do
       state = %{module: FakeProducer, module_state: :return_no_reply}
@@ -84,12 +90,20 @@ defmodule Broadway.ProducerTest do
     end
   end
 
-  test "wrap terminate" do
-    state = %{module: FakeProducer, module_state: :module_state}
+  describe "wrap terminate" do
+    test "forward call to wrapped module" do
+      state = %{module: FakeProducer, module_state: :module_state}
 
-    assert Producer.terminate(:normal, state) == {:normal, :module_state}
+      assert Producer.terminate(:normal, state) == {:normal, :module_state}
 
-    assert Producer.terminate({:shutdown, :a_term}, state) ==
-             {{:shutdown, :a_term}, :module_state}
+      assert Producer.terminate({:shutdown, :a_term}, state) ==
+               {{:shutdown, :a_term}, :module_state}
+    end
+
+    test "returns :ok when the wrapped module doesn't define a terminate/2 callback" do
+      state = %{module: ProducerWithOutTerminate, module_state: :module_state}
+
+      assert Producer.terminate(:normal, state) == :ok
+    end
   end
 end