Created
December 19, 2018 15:33
-
-
Save arjan/7482d1f0ecb9492bae1234760ee5d694 to your computer and use it in GitHub Desktop.
Test the maximum timeout value for a genserver, using a binary search.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule TimeoutChecker do | |
@moduledoc """ | |
test the maximum timeout value for a genserver, using a binary search. | |
TimeoutChecker.run(): | |
16:31:16.907 [warn] Testing the maximum genserver timeout value… | |
16:31:17.064 [warn] The maximum timeout value is: 4294967295 | |
""" | |
require Logger | |
defmodule Tester do | |
use GenServer | |
require Logger | |
def start(t) do | |
GenServer.start(__MODULE__, t) | |
end | |
def init(t) do | |
Process.send_after(self(), :stop, 10) | |
{:ok, t, t} | |
end | |
def handle_info(:stop, _) do | |
{:stop, :normal, nil} | |
end | |
end | |
def test_one(t) do | |
{:ok, pid} = Tester.start(t) | |
Process.monitor(pid) | |
receive do | |
{:DOWN, _, _, _, r} -> r | |
end == :normal | |
end | |
def run() do | |
Logger.warn("Testing the maximum genserver timeout value…") | |
t = test(5_000_000_000_000) | |
Logger.warn("The maximum timeout value is: #{t}") | |
end | |
def test(t, t_prev \\ 0) | |
def test(t, t) do | |
t | |
end | |
def test(t, t_prev) do | |
half = trunc((t - t_prev) / 2) | |
case test_one(t) do | |
true -> | |
test(t + trunc((t - t_prev) / 2), t) | |
false -> | |
test(t_prev + trunc((t - t_prev) / 2), t_prev) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment