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.

Thursday, February 2, 2012

Testing: megaphone_receiver

Previously...
  • I finished coding up the megaphone module.
  • I came up with a plan for testing.
  • After some initial tests, I resolved to test the different components in turn.

I started by testing megaphone_receiver.  As with the megaphone module, I added some test start up code:

test() ->
    Port = 4567,
    spawn(megaphone_receiver, test_server, [Port]),
    receive after 100 -> ok end,
    spawn(megaphone_receiver, test_client, [Port]).

test_server(Port) ->
    { ok, LSocket } = gen_tcp:listen(Port, [binary, {packet, 0}, {active, false}]),
    io:format("~p is now listening on port ~p~n", [?MODULE, Port]),
    { ok, Socket } = gen_tcp:accept(LSocket),
    io:format("~p is now connected~n", [?MODULE]),
    start(Socket).

test_client(Port) ->
    {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {packet, 0}]),
    io:format("~p client is now connected to the server on port ~p~n", [?MODULE, Port]),
    ok = gen_tcp:send(Socket, "0000000053|00000000000000000024|<body>whatever</body>"),
    receive _ -> ok end.

I ran these tests in the erlang shell; with the addition of several io:format statements in various places to help me figure out what was going on.  This uncovered an embarrassing assortment of bugs, the most straight-forward of them was the way I was using the erlang case statement.  

While looking through the code of other developers, I noticed that they were using case statements in the same manner that I would use if statements in a language like Java.  I have found that it is often times simpler to use a statement like this:

case foo(bar) of
    value1 -> do something;
    _ -> do something else
end

Rather than 

Temp = foo(bar),
if 
    Temp == value1 -> do something;
    true -> do something else
end

I run into trouble when I confuse the two and put in a "true ->" element in a case statement.

Next time: more of megaphone_receiver.

No comments:

Post a Comment