diff --git a/lib/gen_lsp/buffer.ex b/lib/gen_lsp/buffer.ex index b47d806..668657c 100644 --- a/lib/gen_lsp/buffer.ex +++ b/lib/gen_lsp/buffer.ex @@ -9,10 +9,15 @@ defmodule GenLSP.Buffer do @options_schema NimbleOptions.new!( communication: [ type: :mod_arg, - default: {GenLSP.Communication.Stdio, []}, doc: "A `{module, args}` tuple, where `module` implements the `GenLSP.Communication.Adapter` behaviour." + ], + name: [ + type: :atom, + doc: + "Used for name registration as described in the \"Name registration\" section in the documentation for `GenServer`." ] + ) @doc """ @@ -23,8 +28,8 @@ defmodule GenLSP.Buffer do #{NimbleOptions.docs(@options_schema)} """ def start_link(opts) do - opts = NimbleOptions.validate!(opts, @options_schema) - GenServer.start_link(__MODULE__, opts, name: Keyword.get(opts, :name, __MODULE__)) + init_opts = NimbleOptions.validate!(opts, @options_schema) + GenServer.start_link(__MODULE__, init_opts, name: Keyword.get(opts, :name, __MODULE__)) end @doc false diff --git a/lib/gen_lsp/test.ex b/lib/gen_lsp/test.ex index c395268..0dd21cf 100644 --- a/lib/gen_lsp/test.ex +++ b/lib/gen_lsp/test.ex @@ -30,12 +30,28 @@ defmodule GenLSP.Test do """ @spec server(mod :: atom()) :: server() def server(mod, opts \\ []) do - buffer = - start_supervised!({GenLSP.Buffer, communication: {GenLSP.Communication.TCP, [port: 0]}}) + buffer_id = String.to_atom("#{opts[:name]}buffer") + lsp_id = String.to_atom("#{opts[:name]}lsp") + + buffer = start_supervised!( + Supervisor.child_spec( + { + GenLSP.Buffer, + communication: {GenLSP.Communication.TCP, [port: 0]}, + name: buffer_id + }, + id: buffer_id + ) + ) {:ok, port} = :inet.port(GenLSP.Buffer.comm_state(buffer).lsocket) - lsp = start_supervised!({mod, Keyword.merge([buffer: buffer], opts)}) + lsp = start_supervised!( + Supervisor.child_spec( + {mod, Keyword.merge([buffer: buffer], opts)}, + id: lsp_id + ) + ) %{lsp: lsp, buffer: buffer, port: port} end diff --git a/test/gen_lsp_test.exs b/test/gen_lsp_test.exs index 18678d2..394d31f 100644 --- a/test/gen_lsp_test.exs +++ b/test/gen_lsp_test.exs @@ -146,4 +146,12 @@ defmodule GenLSPTest do "message" => "Method Not Found" }) end + + test "can start multiple servers in one test/2" do + assert %{} = server(GenLSPTest.ExampleServer, name: :example_server_2) + + assert_raise RuntimeError, ~r/more than one child specification has the id: :example_server_2buffer/, fn -> + server(GenLSPTest.ExampleServer, name: :example_server_2) + end + end end