Skip to content

Instantly share code, notes, and snippets.

@RickCarlino
Last active March 31, 2020 15:57
Show Gist options
  • Save RickCarlino/8f541f1e3316e94b9fbf9035a5cb5f80 to your computer and use it in GitHub Desktop.
Save RickCarlino/8f541f1e3316e94b9fbf9035a5cb5f80 to your computer and use it in GitHub Desktop.
wait_for_pid - Do I even need this as a test helper? I'm not sure...
@wait_time 25
# Base case: We have a pid
def wait_for(pid) when is_pid(pid), do: continue_waiting(pid)
# Failure case: We failed to find a pid for a module.
def wait_for(nil), do: raise("Attempted to wait on bad module/pid")
# Edge case: We have a module and need to try finding its pid.
def wait_for(mod), do: wait_for(Process.whereis(mod))
defp continue_waiting(pid) do
wait(pid, Process.info(pid, :message_queue_len))
end
defp wait(_pid, {:message_queue_len, 0}), do: Process.sleep(@wait_time)
defp wait(pid, {:message_queue_len, _n}) do
Process.sleep(@wait_time)
continue_waiting(pid)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment