Skip to content

Commit

Permalink
make tests more stable
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszsamson committed Nov 19, 2023
1 parent 830be21 commit 4477847
Show file tree
Hide file tree
Showing 9 changed files with 502 additions and 414 deletions.
4 changes: 2 additions & 2 deletions apps/elixir_ls_debugger/lib/debugger/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ defmodule ElixirLS.Debugger.Server do

Output.debugger_important("Terminating #{__MODULE__}: #{message}")

unless Application.get_env(:elixir_ls_debugger, :test_mode) do
unless :persistent_term.get(:elixir_ls_debugger_test_mode, false) do
System.stop(1)
end
end
Expand Down Expand Up @@ -609,7 +609,7 @@ defmodule ElixirLS.Debugger.Server do
# die right after responding to the request
@impl GenServer
def handle_continue(:disconnect, state = %__MODULE__{}) do
unless Application.get_env(:elixir_ls_debugger, :test_mode) do
unless :persistent_term.get(:elixir_ls_debugger_test_mode, false) do
Output.debugger_console("Received disconnect request")
Process.sleep(200)
System.stop(0)
Expand Down
2 changes: 1 addition & 1 deletion apps/elixir_ls_debugger/test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Application.put_env(:elixir_ls_debugger, :test_mode, true)
:persistent_term.put(:elixir_ls_debugger_test_mode, true)
ExUnit.start(exclude: [pending: true])

if Version.match?(System.version(), ">= 1.15.0") do
Expand Down
7 changes: 6 additions & 1 deletion apps/language_server/lib/language_server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ defmodule ElixirLS.LanguageServer do
"ElixirLS has crashed. See Output panel."
)

System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn("Application stopping")
end
end

:ok
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,14 @@ defmodule ElixirLS.LanguageServer.Providers.WorkspaceSymbols do
end

def notify_build_complete(server \\ __MODULE__, override_test_mode \\ false) do
unless Application.get_env(:language_server, :test_mode) && not override_test_mode do
unless :persistent_term.get(:language_server_test_mode, false) && not override_test_mode do
GenServer.cast(server, :build_complete)
end
end

@spec notify_uris_modified([String.t()]) :: :ok | nil
def notify_uris_modified(uris, server \\ __MODULE__, override_test_mode \\ false) do
unless Application.get_env(:language_server, :test_mode) && not override_test_mode do
unless :persistent_term.get(:language_server_test_mode, false) && not override_test_mode do
GenServer.cast(server, {:uris_modified, uris})
end
end
Expand Down
167 changes: 119 additions & 48 deletions apps/language_server/lib/language_server/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,11 @@ defmodule ElixirLS.LanguageServer.Server do

Logger.error("Terminating #{__MODULE__}: #{message}")

unless Application.get_env(:language_server, :test_mode) do
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn("Terminating #{__MODULE__}: #{message}")
end
end

Expand Down Expand Up @@ -326,8 +328,13 @@ defmodule ElixirLS.LanguageServer.Server do

Logger.error(message)
JsonRpc.show_message(:error, message)
Process.sleep(2000)
System.halt(1)

unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn(message)
end
end

state =
Expand Down Expand Up @@ -545,10 +552,10 @@ defmodule ElixirLS.LanguageServer.Server do
defp handle_notification(notification("exit"), state = %__MODULE__{}) do
code = if state.received_shutdown?, do: 0, else: 1

unless Application.get_env(:language_server, :test_mode) do
unless :persistent_term.get(:language_server_test_mode, false) do
System.stop(code)
else
Process.exit(self(), {:exit_code, code})
Logger.info("Received exit with code #{code}")
end

state
Expand Down Expand Up @@ -1358,8 +1365,13 @@ defmodule ElixirLS.LanguageServer.Server do

Logger.error(message)
JsonRpc.show_message(:error, message)
Process.sleep(2000)
System.halt(1)

unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn(message)
end
end
end

Expand Down Expand Up @@ -1618,8 +1630,12 @@ defmodule ElixirLS.LanguageServer.Server do
"Invalid `dialyzerWarnOpts` in configuration. Expected list of strings or nil, got #{inspect(dialyzer_warn_opts)}."
)

Process.sleep(2000)
System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn("Invalid `dialyzerWarnOpts` #{inspect(dialyzer_warn_opts)}")
end
end

dialyzer_formats = [
Expand All @@ -1638,8 +1654,12 @@ defmodule ElixirLS.LanguageServer.Server do
"Invalid `dialyzerFormat` in configuration. Expected one of #{Enum.join(dialyzer_formats, ", ")} or nil, got #{inspect(dialyzer_format)}."
)

Process.sleep(2000)
System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn("Invalid `dialyzerFormat` #{inspect(dialyzer_format)}")
end
end
end

Expand Down Expand Up @@ -1711,8 +1731,12 @@ defmodule ElixirLS.LanguageServer.Server do
"Invalid `additionalWatchedExtensions` in configuration. Expected list of extensions starting with `.` or nil, got #{inspect(exts)}."
)

Process.sleep(2000)
System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn("Invalid `additionalWatchedExtensions`: #{inspect(exts)}")
end
end

case JsonRpc.register_capability_request(
Expand Down Expand Up @@ -1800,8 +1824,14 @@ defmodule ElixirLS.LanguageServer.Server do
"Invalid `envVariables` in configuration. Expected a map with string key value pairs, got #{inspect(env)}."
)

Process.sleep(2000)
System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn(
"Cannot set environment variables to #{inspect(env)}: #{Exception.message(e)}"
)
end
end
else
JsonRpc.show_message(
Expand Down Expand Up @@ -1849,8 +1879,12 @@ defmodule ElixirLS.LanguageServer.Server do
"Invalid `mixEnv` in configuration. Expected a string or nil, got #{inspect(env)}."
)

Process.sleep(2000)
System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn("Cannot set mix env to #{inspect(env)}: #{Exception.message(e)}")
end
end
else
JsonRpc.show_message(:warning, "Mix env change detected. ElixirLS will restart.")
Expand Down Expand Up @@ -1892,8 +1926,12 @@ defmodule ElixirLS.LanguageServer.Server do
"Invalid `mixTarget` in configuration. Expected a string or nil, got #{inspect(target)}."
)

Process.sleep(2000)
System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn("Cannot set mix target to #{inspect(target)}: #{Exception.message(e)}")
end
end
else
JsonRpc.show_message(:warning, "Mix target change detected. ElixirLS will restart")
Expand Down Expand Up @@ -1934,17 +1972,26 @@ defmodule ElixirLS.LanguageServer.Server do
"Invalid `projectDir` in configuration. Expected a string or nil, got #{inspect(project_dir_config)}."
)

Process.sleep(2000)
System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn("Invalid `projectDir`: #{inspect(project_dir_config)}")
end
end
end

cond do
not File.dir?(project_dir) ->
Logger.error("Project directory #{project_dir} does not exist")
JsonRpc.show_message(:error, "Project directory #{project_dir} does not exist")
Process.sleep(2000)
System.halt(1)

unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn("Project directory #{project_dir} does not exist")
end

is_nil(prev_project_dir) ->
with :ok <- File.cd(project_dir),
Expand All @@ -1964,8 +2011,12 @@ defmodule ElixirLS.LanguageServer.Server do
"Please make sure the directory exists and you have necessary permissions"
)

Process.sleep(2000)
System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn("Unable to change directory into #{project_dir}: #{inspect(reason)}")
end
end

prev_project_dir != project_dir ->
Expand Down Expand Up @@ -2015,8 +2066,12 @@ defmodule ElixirLS.LanguageServer.Server do
"Cannot create .elixir_ls/.gitignore"
)

Process.sleep(2000)
System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn("Cannot create .elixir_ls/.gitignore, cause: #{Atom.to_string(err)}")
end
end
end

Expand Down Expand Up @@ -2062,8 +2117,12 @@ defmodule ElixirLS.LanguageServer.Server do
message
)

Process.sleep(2000)
System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn(message)
end
end

case File.cwd() do
Expand Down Expand Up @@ -2092,8 +2151,12 @@ defmodule ElixirLS.LanguageServer.Server do
message
)

Process.sleep(2000)
System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn(message)
end
end
else
message =
Expand All @@ -2106,8 +2169,12 @@ defmodule ElixirLS.LanguageServer.Server do
message
)

Process.sleep(2000)
System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn(message)
end
end

{:error, reason} ->
Expand All @@ -2119,8 +2186,12 @@ defmodule ElixirLS.LanguageServer.Server do
message
)

Process.sleep(2000)
System.halt(1)
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn(message)
end
end
end
end
Expand Down Expand Up @@ -2255,25 +2326,25 @@ defmodule ElixirLS.LanguageServer.Server do

defp do_sanity_check(_state) do
try do
unless function_exported?(ElixirSense, :module_info, 1) and
:persistent_term.get(:language_server_lib_dir) ==
ElixirLS.LanguageServer.module_info(:compile)[:source] do
raise "sanity check failed"
end
unless :persistent_term.get(:language_server_test_mode, false) do
unless function_exported?(ElixirSense, :module_info, 1) and
:persistent_term.get(:language_server_lib_dir) ==
ElixirLS.LanguageServer.module_info(:compile)[:source] do
raise "sanity check failed"
end

unless function_exported?(ElixirLS.LanguageServer, :module_info, 1) and
:persistent_term.get(:language_server_elixir_sense_lib_dir) ==
ElixirSense.module_info(:compile)[:source] do
raise "sanity check failed"
unless function_exported?(ElixirLS.LanguageServer, :module_info, 1) and
:persistent_term.get(:language_server_elixir_sense_lib_dir) ==
ElixirSense.module_info(:compile)[:source] do
raise "sanity check failed"
end
end
rescue
_ ->
Logger.error("Sanity check failed. ElixirLS needs to restart.")

unless Application.get_env(:language_server, :test_mode) do
Process.sleep(2000)
System.halt(1)
end
Process.sleep(2000)
System.halt(1)
end
end
end
8 changes: 6 additions & 2 deletions apps/language_server/lib/language_server/tracer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ defmodule ElixirLS.LanguageServer.Tracer do

Logger.error("Terminating #{__MODULE__}: #{message}")

unless Application.get_env(:language_server, :test_mode) do
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn("Terminating #{__MODULE__}: #{message}")
end
end
end
Expand Down Expand Up @@ -243,9 +245,11 @@ defmodule ElixirLS.LanguageServer.Tracer do
%{}
)

unless Application.get_env(:language_server, :test_mode) do
unless :persistent_term.get(:language_server_test_mode, false) do
Process.sleep(2000)
System.halt(1)
else
IO.warn("Unable to init tracer table #{table} in directory #{project_dir}: #{error_msg}")
end
end

Expand Down
Loading

0 comments on commit 4477847

Please sign in to comment.