Skip to content

Commit

Permalink
Raise if the pubsub server name is not configured at compile time (ph…
Browse files Browse the repository at this point in the history
…oenixframework#3068)

If the pubsub server is configured at runtime (e.g. in the init callback) then
everything will appear to work as normal, but the pubsub server will have the
name of `Elixir.Supervisor` due to having nil as a name.

This closes phoenixframework#2992
  • Loading branch information
Gazler authored and chrismccord committed Sep 28, 2018
1 parent 49c42b6 commit cbadd89
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/phoenix/endpoint.ex
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,8 @@ defmodule Phoenix.Endpoint do
server
else
raise ArgumentError, """
no :pubsub server configured, please setup :pubsub in your config.
no :pubsub server was configured at compile time, please setup :pubsub
in your config.
By default this looks like:
Expand Down
2 changes: 1 addition & 1 deletion lib/phoenix/endpoint/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ defmodule Phoenix.Endpoint.Supervisor do

if adapter = pub_conf[:adapter] do
pub_conf = [fastlane: Phoenix.Channel.Server] ++ pub_conf
[supervisor(adapter, [mod.__pubsub_server__(), pub_conf])]
[supervisor(adapter, [Phoenix.Endpoint.__pubsub_server__!(mod), pub_conf])]
else
[]
end
Expand Down
17 changes: 17 additions & 0 deletions test/phoenix/endpoint/endpoint_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ defmodule Phoenix.Endpoint.EndpointTest do
pubsub: [adapter: Phoenix.PubSub.PG2, name: :endpoint_pub]]
Application.put_env(:phoenix, __MODULE__.Endpoint, @config)

Application.put_env(:phoenix, __MODULE__.NoPubSubEndpoint, Keyword.delete(@config, :pubsub))

defmodule Endpoint do
use Phoenix.Endpoint, otp_app: :phoenix

Expand All @@ -21,6 +23,15 @@ defmodule Phoenix.Endpoint.EndpointTest do
assert code_reloading? == false
end

defmodule NoPubSubEndpoint do
use Phoenix.Endpoint, otp_app: :phoenix

def init(_, config) do
pubsub = [adapter: Phoenix.PubSub.PG2, name: :named_at_runtime_endpoint_pub]
{:ok, Keyword.put(config, :pubsub, pubsub)}
end
end

setup_all do
Endpoint.start_link()
on_exit fn -> Application.delete_env(:phoenix, :serve_endpoints) end
Expand All @@ -35,6 +46,12 @@ defmodule Phoenix.Endpoint.EndpointTest do
}
end

test "errors if pubsub is not set at runtime and not compile time" do
Process.flag(:trap_exit, true)
{:error, {%ArgumentError{message: message}, _}} = NoPubSubEndpoint.start_link()
assert message =~ "no :pubsub server was configured at compile time"
end

test "has reloadable configuration" do
assert Endpoint.config(:url) == [host: {:system, "ENDPOINT_TEST_HOST"}, path: "/api"]
assert Endpoint.config(:static_url) == [host: "static.example.com"]
Expand Down

0 comments on commit cbadd89

Please sign in to comment.