diff --git a/lib/membrane_file/sink.ex b/lib/membrane_file/sink.ex index 99e52ae..ca4bef9 100644 --- a/lib/membrane_file/sink.ex +++ b/lib/membrane_file/sink.ex @@ -63,12 +63,23 @@ defmodule Membrane.File.Sink do def handle_event(pad, event, ctx, state), do: super(pad, event, ctx, state) + @impl true + def handle_end_of_stream(:input, _ctx, state) do + {[], do_merge_and_close(state)} + end + @impl true def handle_terminate_request(_ctx, state) do + {[terminate: :normal], do_merge_and_close(state)} + end + + defp do_merge_and_close(%{fd: nil} = state), do: state + + defp do_merge_and_close(state) do state = maybe_merge_temporary(state) @common_file.close!(state.fd) - {[terminate: :normal], %{state | fd: nil}} + %{state | fd: nil} end defp seek_file(%{fd: fd} = state, position) do diff --git a/test/membrane_file/sink_test.exs b/test/membrane_file/sink_test.exs index c1f9f99..77d8dce 100644 --- a/test/membrane_file/sink_test.exs +++ b/test/membrane_file/sink_test.exs @@ -118,4 +118,21 @@ defmodule Membrane.File.SinkTest do @module.handle_terminate_request(ctx, state) end end + + describe "on handle_end_of_stream" do + setup :inject_mock_fd + + test "should merge and close the opened files", %{state: state, ctx: ctx} do + %{fd: file, temp_location: temp_location} = state + state = %{state | temp_fd: :temporary} + + CommonMock + |> expect(:copy!, fn :temporary, ^file -> 0 end) + |> expect(:close!, fn :temporary -> :ok end) + |> expect(:rm!, fn ^temp_location -> :ok end) + |> expect(:close!, fn ^file -> :ok end) + + assert {[], %{fd: nil, temp_fd: nil}} = @module.handle_end_of_stream(:input, ctx, state) + end + end end diff --git a/test/support/membrane_file/test_case_template.ex b/test/support/membrane_file/test_case_template.ex index 8931c58..a4d207c 100644 --- a/test/support/membrane_file/test_case_template.ex +++ b/test/support/membrane_file/test_case_template.ex @@ -23,8 +23,8 @@ defmodule Membrane.File.TestCaseTemplate do assert {[], %{fd: :file}} = unquote(module).handle_setup(ctx, state) end - test "on handle_terminate_request should close the opened file", %{state: state, ctx: ctx} do - %{fd: file} = state + test "on handle_terminate_request should close the opened file", context do + %{state: %{fd: file} = state, ctx: ctx} = inject_mock_fd(context) Membrane.File.CommonMock |> expect(:close!, fn ^file -> :ok end)