Await file deletion without sleeping in tests

This commit is contained in:
Jonatan Kłosko 2023-05-30 23:17:33 +02:00
parent 4145121ba9
commit 0b73752803
2 changed files with 24 additions and 12 deletions

View file

@ -587,7 +587,7 @@ defmodule Livebook.SessionTest do
File.write!(source_path, "content")
{:ok, old_file_ref} = Session.register_file(session.pid, source_path, "key")
runtime = connected_noop_runtime()
runtime = connected_noop_runtime(self())
Session.set_runtime(session.pid, runtime)
send(session.pid, {:runtime_file_lookup, self(), old_file_ref})
assert_receive {:runtime_file_lookup_reply, {:ok, old_path}}
@ -599,7 +599,8 @@ defmodule Livebook.SessionTest do
send(session.pid, {:runtime_file_lookup, self(), new_file_ref})
assert_receive {:runtime_file_lookup_reply, {:ok, new_path}}
Process.sleep(50)
{:file, file_id} = old_file_ref
assert_receive {:runtime_trace, :revoke_file, [^file_id]}
refute File.exists?(old_path)
assert File.exists?(new_path)
@ -620,14 +621,15 @@ defmodule Livebook.SessionTest do
{:ok, file_ref} =
Session.register_file(session.pid, source_path, "key", linked_client_id: client_id)
runtime = connected_noop_runtime()
runtime = connected_noop_runtime(self())
Session.set_runtime(session.pid, runtime)
send(session.pid, {:runtime_file_lookup, self(), file_ref})
assert_receive {:runtime_file_lookup_reply, {:ok, path}}
send(client_pid, :stop)
Process.sleep(50)
{:file, file_id} = file_ref
assert_receive {:runtime_trace, :revoke_file, [^file_id]}
refute File.exists?(path)
end
@ -660,14 +662,15 @@ defmodule Livebook.SessionTest do
client_name: "data.txt"
})
runtime = connected_noop_runtime()
runtime = connected_noop_runtime(self())
Session.set_runtime(session.pid, runtime)
send(session.pid, {:runtime_file_lookup, self(), file_ref})
assert_receive {:runtime_file_lookup_reply, {:ok, path}}
Session.erase_outputs(session.pid)
Process.sleep(50)
{:file, file_id} = file_ref
assert_receive {:runtime_trace, :revoke_file, [^file_id]}
refute File.exists?(path)
end
@ -1314,8 +1317,8 @@ defmodule Livebook.SessionTest do
{section_id, cell_id}
end
defp connected_noop_runtime() do
{:ok, runtime} = Livebook.Runtime.NoopRuntime.new() |> Livebook.Runtime.connect()
defp connected_noop_runtime(trace_to \\ nil) do
{:ok, runtime} = Livebook.Runtime.NoopRuntime.new(trace_to) |> Livebook.Runtime.connect()
runtime
end

View file

@ -4,10 +4,10 @@ defmodule Livebook.Runtime.NoopRuntime do
# A runtime that doesn't do any actual evaluation,
# thus not requiring any underlying resources.
defstruct [:started]
defstruct [:started, :trace_to]
def new() do
%__MODULE__{started: false}
def new(trace_to \\ nil) do
%__MODULE__{started: false, trace_to: trace_to}
end
defimpl Livebook.Runtime do
@ -38,7 +38,10 @@ defmodule Livebook.Runtime.NoopRuntime do
:ok
end
def revoke_file(_, _), do: :ok
def revoke_file(runtime, file_id) do
trace(runtime, :revoke_file, [file_id])
:ok
end
def start_smart_cell(_, _, _, _, _), do: :ok
def set_smart_cell_parent_locators(_, _, _), do: :ok
@ -60,5 +63,11 @@ defmodule Livebook.Runtime.NoopRuntime do
def put_system_envs(_, _), do: :ok
def delete_system_envs(_, _), do: :ok
defp trace(runtime, fun, args) do
if runtime.trace_to do
send(runtime.trace_to, {:runtime_trace, fun, args})
end
end
end
end