Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge and close file on end of stream event #38

Merged
merged 1 commit into from
May 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion lib/membrane_file/sink.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
17 changes: 17 additions & 0 deletions test/membrane_file/sink_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions test/support/membrane_file/test_case_template.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down