From ca082cb6f4dcc3ff3765a848493fc215fb0be88e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Kita?= Date: Mon, 13 Feb 2023 16:26:21 +0100 Subject: [PATCH] Bugfix pipeline call (#527) * Make sure state of the pipeline is updated after handle_call --- lib/membrane/core/pipeline.ex | 15 +++++----- .../synchronous_pipeline_call_test.exs | 30 +++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/lib/membrane/core/pipeline.ex b/lib/membrane/core/pipeline.ex index 18f978416..004a73e31 100644 --- a/lib/membrane/core/pipeline.ex +++ b/lib/membrane/core/pipeline.ex @@ -146,13 +146,14 @@ defmodule Membrane.Core.Pipeline do def handle_call(message, from, state) do context = &CallbackContext.from_state(&1, from: from) - CallbackHandler.exec_and_handle_callback( - :handle_call, - Membrane.Core.Pipeline.ActionHandler, - %{context: context}, - [message], - state - ) + state = + CallbackHandler.exec_and_handle_callback( + :handle_call, + Membrane.Core.Pipeline.ActionHandler, + %{context: context}, + [message], + state + ) {:noreply, state} end diff --git a/test/membrane/integration/synchronous_pipeline_call_test.exs b/test/membrane/integration/synchronous_pipeline_call_test.exs index 332f486a7..28eb96f0c 100644 --- a/test/membrane/integration/synchronous_pipeline_call_test.exs +++ b/test/membrane/integration/synchronous_pipeline_call_test.exs @@ -1,6 +1,8 @@ defmodule PipelineSynchronousCallTest do use ExUnit.Case, async: false + import Membrane.Testing.Assertions + alias Membrane.Pipeline @msg "Some message" @@ -46,4 +48,32 @@ defmodule PipelineSynchronousCallTest do reply = Pipeline.call(pid, {:instant_reply, @msg}) assert reply == @msg end + + defmodule PipelineSpawningChildrenOnCall do + use Membrane.Pipeline + + @impl true + def handle_init(_ctx, _options) do + {[], %{}} + end + + @impl true + def handle_call(:spawn_children, _ctx, state) do + spec = + child(:source, %Membrane.Testing.Source{output: [1, 2, 3]}) + |> child(:sink, Membrane.Testing.Sink) + + {[spec: spec, reply: nil], state} + end + end + + test "Pipeline should be able to perform actions before replying on handle_call" do + {:ok, _supervisor, pipeline_pid} = + Membrane.Testing.Pipeline.start(module: PipelineSpawningChildrenOnCall) + + Membrane.Pipeline.call(pipeline_pid, :spawn_children) + assert_pipeline_play(pipeline_pid) + assert_end_of_stream(pipeline_pid, :sink) + Pipeline.terminate(pipeline_pid, blocking?: true) + end end