diff options
author | Paul Jones <paulj@lshift.net> | 2009-08-11 15:03:07 +0100 |
---|---|---|
committer | Paul Jones <paulj@lshift.net> | 2009-08-11 15:03:07 +0100 |
commit | fa814810f38241d83e572762a85c8cf0c668a84a (patch) | |
tree | 25075fa13f3ff1132722d62c23aff438795d8fb8 | |
parent | 5c553b6541e198faa6d04d7df037321e3f3617c4 (diff) | |
download | rabbitmq-server-fa814810f38241d83e572762a85c8cf0c668a84a.tar.gz |
Changed hook function to be a {M,F,A} set instead
-rw-r--r-- | src/rabbit_hooks.erl | 8 | ||||
-rw-r--r-- | src/rabbit_tests.erl | 49 |
2 files changed, 31 insertions, 26 deletions
diff --git a/src/rabbit_hooks.erl b/src/rabbit_hooks.erl index 454171ca..05db6630 100644 --- a/src/rabbit_hooks.erl +++ b/src/rabbit_hooks.erl @@ -38,10 +38,8 @@ -ifdef(use_specs). --type(hookfun() :: fun((list()) -> 'ok')). - -spec(start/0 :: () -> 'ok'). --spec(subscribe/3 :: (atom(), atom(), hookfun()) -> 'ok'). +-spec(subscribe/3 :: (atom(), atom(), {atom(), atom(), list()}) -> 'ok'). -spec(unsubscribe/2 :: (atom(), atom()) -> 'ok'). -spec(trigger/2 :: (atom(), list()) -> 'ok'). @@ -61,10 +59,10 @@ unsubscribe(Hook, HandlerName) -> trigger(Hook, Args) -> Hooks = ets:lookup(?TableName, Hook), - [case catch H(Args) of + [case catch apply(M, F, [Hook, Name, Args | A]) of {'EXIT', Reason} -> rabbit_log:warning("Failed to execute handler ~p for hook ~p: ~p", [Name, Hook, Reason]); _ -> ok - end || {_, Name, H} <- Hooks], + end || {_, Name, {M, F, A}} <- Hooks], ok. diff --git a/src/rabbit_tests.erl b/src/rabbit_tests.erl index aef8c9d8..71d529a3 100644 --- a/src/rabbit_tests.erl +++ b/src/rabbit_tests.erl @@ -33,6 +33,9 @@ -export([all_tests/0, test_parsing/0]). +%% Exported so the hook mechanism can call back +-export([handle_hook/3, bad_handle_hook/3, extra_arg_hook/5]). + -import(lists). -include("rabbit.hrl"). @@ -604,36 +607,32 @@ test_server_status() -> test_hooks() -> %% Firing of hooks calls all hooks in an isolated manner - rabbit_hooks:subscribe(test_hook, test, - fun(Args) -> - put(fired_testhook_handler, Args) - end), - rabbit_hooks:subscribe(test_hook, test2, - fun(Args) -> - put(fired_testhook_handler2, Args) - end), - rabbit_hooks:subscribe(test_hook2, test2, - fun(Args) -> - put(fired_testhook2_handler, Args) - end), + rabbit_hooks:subscribe(test_hook, test, {rabbit_tests, handle_hook, []}), + rabbit_hooks:subscribe(test_hook, test2, {rabbit_tests, handle_hook, []}), + rabbit_hooks:subscribe(test_hook2, test2, {rabbit_tests, handle_hook, []}), rabbit_hooks:trigger(test_hook, [arg1, arg2]), - [arg1, arg2] = get(fired_testhook_handler), - [arg1, arg2] = get(fired_testhook_handler2), - undefined = get(fired_testhook2_handler), + [arg1, arg2] = get(test_hook_test_fired), + [arg1, arg2] = get(test_hook_test2_fired), + undefined = get(test_hook2_test2_fired), %% Hook Deletion works - put(fired_testhook_handler, undefined), - put(fired_testhook_handler2, undefined), + put(test_hook_test_fired, undefined), + put(test_hook_test2_fired, undefined), rabbit_hooks:unsubscribe(test_hook, test), rabbit_hooks:trigger(test_hook, [arg3, arg4]), - undefined = get(fired_testhook_handler), - [arg3, arg4] = get(fired_testhook_handler2), - undefined = get(fired_testhook2_handler), + undefined = get(test_hook_test_fired), + [arg3, arg4] = get(test_hook_test2_fired), + undefined = get(test_hook2_test2_fired), %% Catches exceptions from bad hooks - rabbit_hooks:subscribe(test_hook3, test, fun(Args) -> bad:bad() end), + rabbit_hooks:subscribe(test_hook3, test, {rabbit_tests, bad_handle_hook, []}), ok = rabbit_hooks:trigger(test_hook3, []), + %% Passing extra arguments to hooks + rabbit_hooks:subscribe(arg_hook, test, {rabbit_tests, extra_arg_hook, [1, 3]}), + rabbit_hooks:trigger(arg_hook, [arg1, arg2]), + {[arg1, arg2], 1, 3} = get(arg_hook_test_fired), + passed. %--------------------------------------------------------------------- @@ -719,3 +718,11 @@ delete_log_handlers(Handlers) -> [[] = error_logger:delete_report_handler(Handler) || Handler <- Handlers], ok. + +handle_hook(HookName, Handler, Args) -> + A = atom_to_list(HookName) ++ "_" ++ atom_to_list(Handler) ++ "_fired", + put(list_to_atom(A), Args). +bad_handle_hook(_, _, _) -> + bad:bad(). +extra_arg_hook(Hookname, Handler, Args, Extra1, Extra2) -> + handle_hook(Hookname, Handler, {Args, Extra1, Extra2}). |