Skip to content

Commit

Permalink
Wrap server_test assertions in timeout helper function (#541)
Browse files Browse the repository at this point in the history
These tests were flakey on CI, hopefully this helps with that.
  • Loading branch information
mhanberg authored Jun 17, 2020
1 parent beb5326 commit bf59c62
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
26 changes: 26 additions & 0 deletions test/support/utils.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
defmodule Wallaby.TestSupport.Utils do
@moduledoc """
This module contains generic testing helpers.
"""

@doc """
Repeatedly execute a closure, with a timeout. Useful for assertions that are relying on asyncronous operations.
"""
def attempt_with_timeout(doer, timeout \\ 100),
do: attempt_with_timeout(doer, now_in_milliseconds(), timeout)

defp attempt_with_timeout(doer, start, timeout) do
doer.()
rescue
e ->
passed_timeout? = now_in_milliseconds() - start >= timeout

if passed_timeout? do
reraise e, __STACKTRACE__
else
attempt_with_timeout(doer, start, timeout)
end
end

defp now_in_milliseconds(), do: DateTime.utc_now() |> DateTime.to_unix(:millisecond)
end
22 changes: 14 additions & 8 deletions test/wallaby/chrome/chromedriver/server_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ defmodule Wallaby.Chrome.Chromedriver.ServerTest do
alias Wallaby.TestSupport.Chrome.ChromeTestScript
alias Wallaby.TestSupport.TestScriptUtils
alias Wallaby.TestSupport.TestWorkspace
alias Wallaby.TestSupport.Utils

alias Wallaby.Chrome
alias Wallaby.Chrome.Chromedriver.Server
Expand Down Expand Up @@ -102,8 +103,11 @@ defmodule Wallaby.Chrome.Chromedriver.ServerTest do
kill_os_process(wrapper_script_os_pid)

assert_receive {:EXIT, ^server, {:exit_status, _}}
refute os_process_running?(wrapper_script_os_pid)
refute os_process_running?(os_pid)

Utils.attempt_with_timeout(fn ->
refute os_process_running?(wrapper_script_os_pid)
refute os_process_running?(os_pid)
end)
end

test "crashes when chromedriver is killed" do
Expand All @@ -120,10 +124,10 @@ defmodule Wallaby.Chrome.Chromedriver.ServerTest do

assert_receive {:EXIT, ^server, {:exit_status, _}}

# Since the process isn't trapping exits, let things shut down async
Process.sleep(100)
refute os_process_running?(wrapper_script_os_pid)
refute os_process_running?(os_pid)
Utils.attempt_with_timeout(fn ->
refute os_process_running?(wrapper_script_os_pid)
refute os_process_running?(os_pid)
end)
end

test "shuts down wrapper and chromedriver when server is stopped" do
Expand All @@ -135,8 +139,10 @@ defmodule Wallaby.Chrome.Chromedriver.ServerTest do

Server.stop(server)

refute os_process_running?(wrapper_script_os_pid)
refute os_process_running?(os_pid)
Utils.attempt_with_timeout(fn ->
refute os_process_running?(wrapper_script_os_pid)
refute os_process_running?(os_pid)
end)
end

defp write_chrome_wrapper_script!(base_dir, opts \\ []) do
Expand Down
23 changes: 3 additions & 20 deletions test/wallaby/driver/process_workspace_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ defmodule Wallaby.Driver.ProcessWorkspaceTest do

alias Wallaby.Driver.ProcessWorkspace
alias Wallaby.Driver.TemporaryPath
alias Wallaby.TestSupport.Utils

defmodule TestServer do
use GenServer
Expand All @@ -23,7 +24,7 @@ defmodule Wallaby.Driver.ProcessWorkspaceTest do

TestServer.stop(test_server)

with_timeout(fn ->
Utils.attempt_with_timeout(fn ->
refute File.exists?(workspace_path)
end)
end
Expand All @@ -38,7 +39,7 @@ defmodule Wallaby.Driver.ProcessWorkspaceTest do

TestServer.stop(test_server)

with_timeout(fn ->
Utils.attempt_with_timeout(fn ->
refute File.exists?(workspace_path)
end)
end
Expand All @@ -63,22 +64,4 @@ defmodule Wallaby.Driver.ProcessWorkspaceTest do

TemporaryPath.generate(base_dir)
end

defp now_in_milliseconds(), do: DateTime.utc_now() |> DateTime.to_unix(:millisecond)

defp with_timeout(doer, timeout \\ 100),
do: with_timeout(doer, now_in_milliseconds(), timeout)

defp with_timeout(doer, start, timeout) do
doer.()
rescue
e ->
passed_timeout? = now_in_milliseconds() - start >= timeout

if passed_timeout? do
reraise e, __STACKTRACE__
else
with_timeout(doer, start, timeout)
end
end
end

0 comments on commit bf59c62

Please sign in to comment.