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.

Saturday, February 4, 2012

Testing megaphone_sender: Times Two!

Previously...
  • After some initial tests, I resolved to test the different components in turn.
  • I started testing the megaphone_receiver component.
  • I finished testing the megaphone_receiver component.

I finished testing the megaphone_sender module.  A proud person might point out that this module is rather, erm, modest.  Fortunately, I am not proud: Go Me!

At any rate, I had to create some test harness stuff for the sender module:

test() ->
    %% 
    %% Create a connection for the process to talk to.
    %%
    Port = 4567,
    test_listener(Port),

    %%
    %% give the listener a little time to start up
    %%
    receive after 100 -> ok end,
    test_sender(Port).

test_listener(Port) ->
    { ok, LSocket } = gen_tcp:listen(Port, [binary, {packet, 0}, {active, false}]),
    spawn(?MODULE, test_listener_start, [LSocket]),
    io:format("tester is now listening on port ~p~n", [Port]).

test_listener_start(LSocket) ->
    {ok, Socket} = gen_tcp:accept(LSocket),
    io:format("tester is now connected.~n"),
    listen_loop(Socket).

listen_loop(Socket) ->
    Data = gen_tcp:recv(Socket, 0),
    io:format("tester has received ~p~n", [Data]),
    listen_loop(Socket).

test_sender(Port) ->
    io:format("test_sender is trying to connect to port ~p~n", [Port]),
    {ok, Socket} = gen_tcp:connect("localhost", Port, [binary, {packet, 0}]),
    io:format("test_sender is now connected to port ~p~n", [Port]),
    spawn(?MODULE, start, [Socket, undefined]),

    %%
    %% give the system a bit of time to start up
    %%
    receive after 100 -> ok end,

    %%
    %% test sending a packet through the system
    %%
    io:format("trying to send message to sender~n"),
    megaphone_sender ! { 16, "<body>some response</body>" },
    io:format("sent messsage~n").

After a few typos, misformats and other assorted bugs, the sender code seems to be working correctly.  

Note that all during testing I have had to make various changes to the underlying code, hence the original stuff that I posted will not work correctly.  Rather than reposting the updated modules here, I think it would be more useful to just post the tested stuff up on github.  

Besides posting the revised version of the code would be a cheap out and I absolutely resolve not to do that.  Absolutely.  Will not.  At least until I feel lazy.

Next time: back to the megaphone module.

No comments:

Post a Comment