Page MenuHomePhorge

No OneTemporary

Size
1 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/majic/once.ex b/lib/majic/once.ex
index 9bf91c0..2460b1c 100644
--- a/lib/majic/once.ex
+++ b/lib/majic/once.ex
@@ -1,30 +1,32 @@
defmodule Majic.Once do
@moduledoc """
Contains convenience functions for one-off use.
"""
alias Majic.Server
@process_timeout Majic.Config.default_process_timeout()
@spec perform(Majic.target(), [Server.start_option()], timeout()) :: Majic.result()
@doc """
Runs a one-shot process without supervision.
Useful in tests, but not recommended for actual applications.
## Example
iex(1)> {:ok, result} = Majic.Once.perform(".")
iex(2)> result
%Majic.Result{content: "directory", encoding: "binary", mime_type: "inode/directory"}
"""
def perform(path, options \\ [], timeout \\ @process_timeout) do
- with {:ok, pid} <- Server.start_link(options),
- {:ok, result} <- Server.perform(pid, path, timeout),
- :ok <- Server.stop(pid) do
- {:ok, result}
+ {:ok, pid} = Server.start_link(options)
+
+ try do
+ Server.perform(pid, path, timeout)
+ after
+ if Process.alive?(pid), do: Server.stop(pid)
end
end
end
diff --git a/test/majic/helpers_test.exs b/test/majic/helpers_test.exs
index ebe1736..e645baf 100644
--- a/test/majic/helpers_test.exs
+++ b/test/majic/helpers_test.exs
@@ -1,14 +1,22 @@
defmodule Majic.OnceTest do
use Majic.MagicCase
doctest Majic.Once
test "perform" do
path = absolute_path("Makefile")
assert {:ok, %{mime_type: "text/x-makefile"}} = Majic.Once.perform(path)
end
test "Majic.perform" do
path = absolute_path("Makefile")
assert {:ok, %{mime_type: "text/x-makefile"}} = Majic.perform(path, once: true)
end
+
+ test "perform does not leak server on error" do
+ before = Process.list()
+ assert {:error, :enoent} = Majic.Once.perform(missing_filename())
+ after_call = Process.list()
+ leaked = after_call -- before
+ assert leaked == [], "Leaked processes: #{inspect(leaked)}"
+ end
end

File Metadata

Mime Type
text/x-diff
Expires
Sun, Aug 9, 9:55 AM (1 d, 17 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1722852
Default Alt Text
(1 KB)

Event Timeline