elli-lib / elli

Simple, robust and performant Erlang web server

Home Page:https://github.com/elli-lib/elli/blob/develop/doc/README.md

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add ability to request the port elli's running on?

opened this issue Β· comments

πŸ‘‹

Something like

-spec port(Server :: atom | pid) -> {ok, inet:port_number()} | {error, any}.
port(Server) ->
  gen_server:call(Server, port).
  
%% @hidden
handle_call(port, _From, State) ->
  {reply, elli_tcp:port(State#state.socket), State}.

and in elli_tcp:

port({plain, Socket}) -> inet:port(Socket).
port({ssl, Socket}) -> {error, not_supported}. % don't care about ssl

I mostly would need it to run tests concurrently in elixir:

  setup_all do
    {:ok, elli_pid} = Plug.Adapters.Elli.http(__MODULE__, [], port: 0)
    {:ok, port} = :elli.port(elli_pid)
    on_exit(fn ->
      :ok = :elli.shutdown(elli_pid)
    end)

    {:ok, port: port}
  end

  # ...

  test "does that thing", %{port: port} do
    resp = TestClient.get("http://localhost:#{port}/hello")
    assert resp.status_code == 200
    assert resp.body == "hello"
  end

Or maybe there are other ways for elli to bind to a random port such that I'd know about the port elli did bind to?

Instead, couldn't you generate a list of ports and spin up multiple instance of elli from that, keeping track of their ports?

What if the ports are already taken by other processes running on the system? I'd need to handle eaddrinuse errors then ...

I'm not opposed to reviewing a PR for this.

Closing this due to inactivity and ghost status.