-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make code_reload act regardless of diagnostics (#1423)
- Loading branch information
Showing
5 changed files
with
188 additions
and
130 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
-module(els_code_reload). | ||
|
||
-include("els_lsp.hrl"). | ||
-include_lib("kernel/include/logger.hrl"). | ||
|
||
-export([ | ||
maybe_compile_and_load/1 | ||
]). | ||
|
||
-spec maybe_compile_and_load(uri()) -> ok. | ||
maybe_compile_and_load(Uri) -> | ||
Ext = filename:extension(Uri), | ||
case els_config:get(code_reload) of | ||
#{"node" := NodeStr} when Ext == <<".erl">> -> | ||
Node = els_utils:compose_node_name( | ||
NodeStr, | ||
els_config_runtime:get_name_type() | ||
), | ||
Module = els_uri:module(Uri), | ||
case rpc:call(Node, code, is_sticky, [Module]) of | ||
true -> ok; | ||
_ -> handle_rpc_result(rpc:call(Node, c, c, [Module]), Module) | ||
end; | ||
_ -> | ||
ok | ||
end. | ||
|
||
-spec handle_rpc_result(term() | {badrpc, term()}, atom()) -> ok. | ||
handle_rpc_result({ok, Module}, _) -> | ||
Msg = io_lib:format("code_reload success for: ~s", [Module]), | ||
els_server:send_notification( | ||
<<"window/showMessage">>, | ||
#{ | ||
type => ?MESSAGE_TYPE_INFO, | ||
message => els_utils:to_binary(Msg) | ||
} | ||
); | ||
handle_rpc_result(Err, Module) -> | ||
?LOG_INFO( | ||
"[code_reload] code_reload using c:c/1 crashed with: ~p", | ||
[Err] | ||
), | ||
Msg = io_lib:format( | ||
"code_reload swap crashed for: ~s with: ~w", | ||
[Module, Err] | ||
), | ||
els_server:send_notification( | ||
<<"window/showMessage">>, | ||
#{ | ||
type => ?MESSAGE_TYPE_ERROR, | ||
message => els_utils:to_binary(Msg) | ||
} | ||
). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
-module(els_code_reload_SUITE). | ||
|
||
%% CT Callbacks | ||
-export([ | ||
suite/0, | ||
init_per_suite/1, | ||
end_per_suite/1, | ||
init_per_testcase/2, | ||
end_per_testcase/2, | ||
all/0 | ||
]). | ||
|
||
%% Test cases | ||
-export([ | ||
code_reload/1, | ||
code_reload_sticky_mod/1 | ||
]). | ||
|
||
%%============================================================================== | ||
%% Includes | ||
%%============================================================================== | ||
-include_lib("common_test/include/ct.hrl"). | ||
-include_lib("stdlib/include/assert.hrl"). | ||
|
||
%%============================================================================== | ||
%% Types | ||
%%============================================================================== | ||
-type config() :: [{atom(), any()}]. | ||
|
||
%%============================================================================== | ||
%% CT Callbacks | ||
%%============================================================================== | ||
-spec suite() -> [tuple()]. | ||
suite() -> | ||
[{timetrap, {seconds, 30}}]. | ||
|
||
-spec all() -> [atom()]. | ||
all() -> | ||
els_test_utils:all(?MODULE). | ||
|
||
-spec init_per_suite(config()) -> config(). | ||
init_per_suite(Config) -> | ||
els_test_utils:init_per_suite(Config). | ||
|
||
-spec end_per_suite(config()) -> ok. | ||
end_per_suite(Config) -> | ||
els_test_utils:end_per_suite(Config). | ||
|
||
-spec init_per_testcase(atom(), config()) -> config(). | ||
init_per_testcase(TestCase, Config) -> | ||
mock_rpc(), | ||
mock_code_reload_enabled(), | ||
els_test_utils:init_per_testcase(TestCase, Config). | ||
|
||
-spec end_per_testcase(atom(), config()) -> ok. | ||
end_per_testcase(TestCase, Config) -> | ||
unmock_rpc(), | ||
unmock_code_reload_enabled(), | ||
els_test_utils:end_per_testcase(TestCase, Config). | ||
|
||
%%============================================================================== | ||
%% Testcases | ||
%%============================================================================== | ||
|
||
-spec code_reload(config()) -> ok. | ||
code_reload(Config) -> | ||
Uri = ?config(diagnostics_uri, Config), | ||
Module = els_uri:module(Uri), | ||
ok = els_code_reload:maybe_compile_and_load(Uri), | ||
{ok, HostName} = inet:gethostname(), | ||
NodeName = list_to_atom("fakenode@" ++ HostName), | ||
?assert(meck:called(rpc, call, [NodeName, c, c, [Module]])), | ||
ok. | ||
|
||
-spec code_reload_sticky_mod(config()) -> ok. | ||
code_reload_sticky_mod(Config) -> | ||
Uri = ?config(diagnostics_uri, Config), | ||
Module = els_uri:module(Uri), | ||
{ok, HostName} = inet:gethostname(), | ||
NodeName = list_to_atom("fakenode@" ++ HostName), | ||
meck:expect( | ||
rpc, | ||
call, | ||
fun | ||
(PNode, code, is_sticky, [_]) when PNode =:= NodeName -> | ||
true; | ||
(Node, Mod, Fun, Args) -> | ||
meck:passthrough([Node, Mod, Fun, Args]) | ||
end | ||
), | ||
ok = els_code_reload:maybe_compile_and_load(Uri), | ||
?assert(meck:called(rpc, call, [NodeName, code, is_sticky, [Module]])), | ||
?assertNot(meck:called(rpc, call, [NodeName, c, c, [Module]])), | ||
ok. | ||
|
||
%%============================================================================== | ||
%% Internal Functions | ||
%%============================================================================== | ||
|
||
mock_rpc() -> | ||
meck:new(rpc, [passthrough, no_link, unstick]), | ||
{ok, HostName} = inet:gethostname(), | ||
NodeName = list_to_atom("fakenode@" ++ HostName), | ||
meck:expect( | ||
rpc, | ||
call, | ||
fun | ||
(PNode, c, c, [Module]) when PNode =:= NodeName -> | ||
{ok, Module}; | ||
(Node, Mod, Fun, Args) -> | ||
meck:passthrough([Node, Mod, Fun, Args]) | ||
end | ||
). | ||
|
||
unmock_rpc() -> | ||
meck:unload(rpc). | ||
|
||
mock_code_reload_enabled() -> | ||
meck:new(els_config, [passthrough, no_link]), | ||
meck:expect( | ||
els_config, | ||
get, | ||
fun | ||
(code_reload) -> | ||
{ok, HostName} = inet:gethostname(), | ||
#{"node" => "fakenode@" ++ HostName}; | ||
(Key) -> | ||
meck:passthrough([Key]) | ||
end | ||
). | ||
|
||
unmock_code_reload_enabled() -> | ||
meck:unload(els_config). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters