Commits

Anonymous committed ed8622c

Make handle_message send whole message() objects instead of just payloads and test that handle_call, handle_cast, and handle_info are properly passed through to the test_gb module.

Comments (0)

Files changed (2)

src/gen_bunny.erl

     end.
 
 handle_info({#'basic.deliver'{},
-            {content, _ClassId, _Props, _PropertiesBin, [Payload]}},
-            State=#state{mod=Module, modstate=ModState}) ->
-    %% TODO: figure out what fields we want to expose from the 'P_basic' record
-    %% TODO: decode_properties is failing for me - do we even need to do this?
-    %%#'P_basic'{} = rabbit_framing:decode_properties(ClassId, PropertiesBin),
-    case Module:handle_message(Payload, ModState) of
+             Message},
+            State=#state{mod=Module, modstate=ModState})
+  when ?is_message(Message) ->
+    case Module:handle_message(Message, ModState) of
         {noreply, NewModState} ->
             {noreply, State#state{modstate=NewModState}};
         {noreply, NewModState, A} when A =:= hibernate orelse is_number(A) ->
 
 test_gb_stop(Pid) ->
     gen_bunny:stop(Pid),
-    timer:sleep(500), %% I hate this.
+    timer:sleep(100), %% I hate this.
     mock:verify_and_stop(lib_amqp),
     ok.
 
              ?_test(
                 [begin
                      ?assertEqual(c:pid(0,0,0), gen_bunny:get_connection(Pid)),
-                     ?assertEqual(c:pid(0,0,1), gen_bunny:get_channel(Pid))
+                     ?assertEqual(c:pid(0,0,1), gen_bunny:get_channel(Pid)),
+                     ?assertEqual(<<"bunny.consumer">>,
+                                  gen_bunny:get_consumer_tag(Pid))
                  end])
      end}.
+
+
+test_gb_handle_message_test_() ->
+    {setup, fun test_gb_setup/0, fun test_gb_stop/1,
+     fun(Pid) ->
+             ?_test(
+                [begin
+                     ExpectedMessage = bunny_util:new_message(<<"Testing">>),
+                     Pid ! {#'basic.deliver'{}, ExpectedMessage},
+                     ?assertEqual([ExpectedMessage],
+                                  test_gb:get_messages(Pid))
+                 end])
+     end}.
+
+
+test_gb_call_passthrough_test_() ->
+    {setup, fun test_gb_setup/0, fun test_gb_stop/1,
+     fun(Pid) ->
+             ?_test(
+                [begin
+                     ok = gen_bunny:call(Pid, test),
+                     ?assertEqual([test], test_gb:get_calls(Pid))
+                 end])
+     end}.
+
+
+test_gb_cast_passthrough_test_() ->
+    {setup, fun test_gb_setup/0, fun test_gb_stop/1,
+     fun(Pid) ->
+             ?_test(
+                [begin
+                     gen_bunny:cast(Pid, cast_test),
+                     timer:sleep(100),
+                     ?assertEqual([cast_test], test_gb:get_casts(Pid))
+                 end])
+     end}.
+
+
+test_gb_info_passthrough_test_() ->
+    {setup, fun test_gb_setup/0, fun test_gb_stop/1,
+     fun(Pid) ->
+             ?_test(
+                [begin
+                     Pid ! info_test,
+                     ?assertEqual([info_test], test_gb:get_infos(Pid))
+                 end])
+     end}.
          handle_info/2,
          terminate/2]).
 
--export([get_messages/1]).
+-export([get_messages/1, get_calls/1, get_casts/1, get_infos/1]).
 
 -include_lib("gen_bunny.hrl").
 
--record(state, {messages=[]}).
+-record(state, {messages=[], calls=[], infos=[], casts=[]}).
 
 start_link(Opts) ->
     gen_bunny:start_link(?MODULE, direct, <<"bunny.test">>, Opts).
 get_messages(Pid) ->
     gen_bunny:call(Pid, get_messages).
 
+get_calls(Pid) ->
+    gen_bunny:call(Pid, get_calls).
+
+get_casts(Pid) ->
+    gen_bunny:call(Pid, get_casts).
+
+get_infos(Pid) ->
+    gen_bunny:call(Pid, get_infos).
+
 handle_message(Message, State=#state{messages=Messages})
   when ?is_message(Message) ->
     NewMessages = [Message|Messages],
     {noreply, State#state{messages=NewMessages}}.
 
 handle_call(get_messages, _From, State=#state{messages=Messages}) ->
-    {reply, Messages, State}.
+    {reply, Messages, State};
+handle_call(get_calls, _From, State=#state{calls=Calls}) ->
+    {reply, Calls, State};
+handle_call(get_casts, _From, State=#state{casts=Casts}) ->
+    {reply, Casts, State};
+handle_call(get_infos, _From, State=#state{infos=Infos}) ->
+    {reply, Infos, State};
+handle_call(Msg, _From, State=#state{calls=Calls}) ->
+    {reply, ok, State#state{calls=[Msg|Calls]}}.
 
-handle_cast(_Msg, State) -> {noreply, State}.
+handle_cast(Msg, State=#state{casts=Casts}) ->
+    {noreply, State#state{casts=[Msg|Casts]}}.
 
-handle_info(_Info, State) -> {noreply, State}.
+handle_info(Info, State=#state{infos=Infos}) ->
+    {noreply, State#state{infos=[Info|Infos]}}.
 
 terminate(Reason, _State) ->
     io:format("~p terminating with reason ~p~n", [?MODULE, Reason]),