Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
akash-akya committed Sep 25, 2024
1 parent 1e12072 commit 2858eb0
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
16 changes: 8 additions & 8 deletions lib/ex_cmd/process.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ defmodule ExCmd.Process do
```
# sleep command does not watch for stdin or stdout, so closing the
# pipe does not terminate the sleep command.
iex> {:ok, p} = Process.start_link(~w(sleep 100000000)) # sleep indefinitely
iex> Process.await_exit(p, 500) # ensure `await_exit` finish within `500ms`. By default it waits for 5s
iex> {:ok, p} = Process.start_link(~w(sleep 100000000), log: :stderr) # sleep indefinitely
iex> Process.await_exit(p, 1000) # ensure `await_exit` finish within `1000ms`. By default it waits for 5s
{:error, :killed} # command exit due to SIGTERM
```
Expand All @@ -214,16 +214,16 @@ defmodule ExCmd.Process do
```
# bc is a calculator, which reads from stdin and writes output to stdout
iex> {:ok, p} = Process.start_link(~w(bc))
iex> Process.write(p, "1 + 1\n") # there must be new-line to indicate the end of the input line
iex> {:ok, p} = Process.start_link(~w(cat))
iex> Process.write(p, "hello\n") # there must be new-line to indicate the end of the input line
:ok
iex> Process.read(p)
{:ok, "2\n"}
iex> Process.write(p, "2 * 10 + 1\n")
{:ok, "hello\n"}
iex> Process.write(p, "world\n")
:ok
iex> Process.read(p)
{:ok, "21\n"}
# We must close stdin to signal the `bc` command that we are done.
{:ok, "world\n"}
# We must close stdin to signal the command that we are done.
# since `await_exit` implicitly closes the pipes, in this case we don't have to
iex> Process.await_exit(p)
{:ok, 0}
Expand Down
16 changes: 8 additions & 8 deletions test/ex_cmd/process_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ defmodule ExCmd.ProcessTest do
end

test "if await_exit kills the program" do
{:ok, s} = Process.start_link(~w(sleep 10000))
assert_killed(Process.await_exit(s, 500))
{:ok, s} = Process.start_link(~w(sleep 10000), log: :stderr)
assert_killed(Process.await_exit(s, 1000))
end

test "if external program terminates on process exit" do
Expand Down Expand Up @@ -259,8 +259,8 @@ defmodule ExCmd.ProcessTest do

assert {:ok, "ignored signals\n" <> _} = Process.read(s)

# attempt to kill the process after 200ms
assert_killed(Process.await_exit(s, 200))
# attempt to kill the process after 1000ms
assert_killed(Process.await_exit(s, 1000))

refute os_process_alive?(os_pid)
refute Elixir.Process.alive?(s.pid)
Expand Down Expand Up @@ -345,7 +345,7 @@ defmodule ExCmd.ProcessTest do
test "if ex_cmd process is *NOT* terminated on owner exit, if any pipe owner is alive" do
parent = self()

{:ok, s} = Process.start_link(~w(cat), log: :stderr)
{:ok, s} = Process.start_link(~w(cat))

io_proc =
spawn_link(fn ->
Expand All @@ -361,7 +361,7 @@ defmodule ExCmd.ProcessTest do
end

# external process will be killed with SIGTERM (143)
assert_killed(Process.await_exit(s, 200))
assert_killed(Process.await_exit(s, 1000))

# wait for messages to propagate, if there are any
:timer.sleep(100)
Expand Down Expand Up @@ -407,7 +407,7 @@ defmodule ExCmd.ProcessTest do
:owner_changed -> :ok
end

assert_killed(Process.await_exit(s, 200))
_ = Process.await_exit(s, 1000)

refute os_process_alive?(os_pid)
assert {:error, :epipe} == Task.await(task)
Expand Down Expand Up @@ -701,7 +701,7 @@ defmodule ExCmd.ProcessTest do
receive do
{^sender, term} -> term
after
1000 ->
3000 ->
raise "recv timeout"
end
end
Expand Down

0 comments on commit 2858eb0

Please sign in to comment.