Skip to content

Commit

Permalink
Merge pull request #38 from gBillal/merge-and-close-file-on-end-of-st…
Browse files Browse the repository at this point in the history
…ream-event

Merge and close file on end of stream event
  • Loading branch information
DominikWolek authored May 18, 2023
2 parents b5735d3 + f1585a0 commit 9119a2d
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
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

0 comments on commit 9119a2d

Please sign in to comment.