Created
August 11, 2008 07:07
-
-
Save cooldaemon/4820 to your computer and use it in GitHub Desktop.
This files is test codes for the code_change function of gen_X modules.
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
-module(call_code_change). | |
-export([call_code_change/2, call_code_change/4]). | |
call_code_change(Name, OldVer) -> | |
call_code_change(Name, Name, OldVer, {}). | |
call_code_change(Pid, Module, OldVer, Extra) when is_pid(Pid) -> | |
sys:suspend(Pid), | |
sys:change_code(Pid, Module, OldVer, Extra), | |
sys:resume(Pid), | |
ok; | |
call_code_change(Name, Module, OldVer, Extra) -> | |
case whereis(Name) of | |
Pid when is_pid(Pid) -> | |
call_code_change(Pid, Module, OldVer, Extra); | |
_Other -> | |
not_started | |
end. |
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
% version 1 | |
-module(code_change_test). | |
-behaviour(gen_server). | |
-export([start/0, stop/0]). | |
-export([state/0]). | |
-export([ | |
init/1, | |
handle_call/3, handle_cast/2, handle_info/2, | |
terminate/2, code_change/3 | |
]). | |
start() -> gen_server:start({local, ?MODULE}, ?MODULE, [], []). | |
stop() -> gen_server:cast(?MODULE, stop). | |
state() -> gen_server:call(?MODULE, state). | |
init(_Args) -> | |
{ok, ver1}. | |
handle_call(state, _From, State) -> | |
io:fwrite("State=~p~n", [State]), | |
{reply, State, State}; | |
handle_call(_Message, _From, State) -> | |
{reply, ok, State}. | |
handle_cast(stop, State) -> | |
{stop, normal, State}; | |
handle_cast(_Message, State) -> | |
{noreply, State}. | |
handle_info(_Info, State) -> {noreply, State}. | |
terminate(normal, _State) -> ok; | |
terminate(_, _) -> ok. | |
code_change(_OldVsn, State, _Extra) -> | |
{ok, State}. |
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
% version 2 | |
-module(code_change_test). | |
-behaviour(gen_server). | |
-export([start/0, stop/0]). | |
-export([state/0]). | |
-export([ | |
init/1, | |
handle_call/3, handle_cast/2, handle_info/2, | |
terminate/2, code_change/3 | |
]). | |
start() -> gen_server:start({local, ?MODULE}, ?MODULE, [], []). | |
stop() -> gen_server:cast(?MODULE, stop). | |
state() -> gen_server:call(?MODULE, state). | |
init(_Args) -> | |
{ok, {ver2, [foo, bar]}}. | |
handle_call(state, _From, {ver2, Args}=State) -> | |
io:fwrite("State=~p~n", [Args]), | |
{reply, Args, State}; | |
handle_call(_Message, _From, State) -> | |
{reply, ok, State}. | |
handle_cast(stop, State) -> | |
{stop, normal, State}; | |
handle_cast(_Message, State) -> | |
{noreply, State}. | |
handle_info(_Info, State) -> {noreply, State}. | |
terminate(normal, _State) -> ok; | |
terminate(_, _) -> ok. | |
code_change(OldVsn, State, Extra) -> | |
io:fwrite( | |
lists:foldl( | |
fun (Name, Format) -> Format ++ Name ++ "=~p~n" end, | |
"code_change~n", ["OldVsn", "State", "Extra"] | |
), | |
[OldVsn, State, Extra] | |
), | |
{ok, {ver2, [foo, bar]}}. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment