What is Megaphone?

What is Megaphone?
The Megaphone project is about enhancing open source chat software. Specifically, the goal is to allow ejabberd to support 1,000,000 simultaneous users. See The Plan page for more details on how I plan to solve this problem. See the About this Blog page for more details on why I created this blog.

Sunday, February 5, 2012

Testing: megaphone


Previously...
  • I started testing the megaphone_receiver component.
  • I finished testing the megaphone_receiver component.
  • I finished testing the megaphone_sender component.
So...back to the megaphone module...

I added some more test code.  Rather than trying to figure out what changed here is what I used:


start_test() ->
    spawn(megaphone, test_start_server, [4567]).
    
test_start_server(Port) ->
    {ok, LSocket} = gen_tcp:listen(Port, [binary, {packet, 0}, {active, false}]),
    io:format("megaphone is now listening on port ~p~n", [Port]),
    {ok, Sock} = gen_tcp:accept(LSocket),
    io:format("megaphone now has a connection on port ~p, starting...~n", [Port]),
    ?MODULE:start({gen_tcp, Sock}, undefined),
    io:format("megaphone is now running~n").


test_receiver() ->
    Port = 4567,
    test_receiver(Port).
    
test_receiver(Port) ->
    io:format("will try to send packet to port ~p~n", [Port]),
    {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {packet, 0}]),
    receive after 1000 -> ok end,
    gen_tcp:send(Socket, "0000000053|00000000000000000024|<body>whatever</body>"),
    io:format("Sent test packet to ~p~n", [Port]).
   
After many trials and tribulations, the test code eventually worked (go me times three!).  The biggest gotcha I ran into was the following:

When using gen_tcp:listen it is important to use the correct option re: binary mode.  At first I used the following:

{ok, LSocket} = gen_tcp:listen(Port, [{packet, 0}, {active, false}])

Note the missing binary option in the options to gen_tcp:listen.  Because of this, the calls to things like byte_size were failing because gen_tcp:recv was returning strings (lists) rather than binaries.  

Next time: status and (hopefully) the sender.

No comments:

Post a Comment