Skip to content

Commit

Permalink
fix!: use name option to support multiple lsp servers in one test
Browse files Browse the repository at this point in the history
  • Loading branch information
sineed committed Jul 14, 2023
1 parent a0a9d0c commit 71a8d47
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 6 deletions.
11 changes: 8 additions & 3 deletions lib/gen_lsp/buffer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand All @@ -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
Expand Down
22 changes: 19 additions & 3 deletions lib/gen_lsp/test.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions test/gen_lsp_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 71a8d47

Please sign in to comment.