Created
March 10, 2011 23:22
-
-
Save leandrosilva/865165 to your computer and use it in GitHub Desktop.
Sample on how I use websocket_client to test Erlang WebSocket-based applications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is a quick sample on how I have used websocket_client to test Erlang WebSocket-based applications. To | |
taste it you will need to have a copy of websocket_client from my fork, available at: | |
https://github.com/leandrosilva/erlang_websocket/blob/master/src/websocket_client.erl | |
The websocket_client module was made by Dave Bryson on erlang_websocket project: | |
https://github.com/davebryson/erlang_websocket | |
Enjoy! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% @author Leandro Silva <[email protected]> | |
%% @copyright 2011 Leandro Silva. | |
%% @doc Functional test module for an imaginary websocket application. | |
-module(imaginary_websocket_app_test). | |
-author('Leandro Silva <[email protected]>'). | |
-include_lib("eunit/include/eunit.hrl"). | |
% --- scenery description ------------------------------------------------------------------------- | |
describe_test_() -> | |
{"imaginary_websocket_app", | |
{"when is running", | |
[ | |
{"should accept connections on /rooms/{RoomId} and exchange messages", | |
fun should_accept_connections_and_exchange_messages/0} | |
]}}. | |
% --- scenery implementation ---------------------------------------------------------------------- | |
should_accept_connections_and_exchange_messages() -> | |
started = websocket_client_impl:start("127.0.0.1", 8008, "/rooms/123"), | |
{sent, "message 1"} = websocket_client_impl:send("message 1"), | |
wait_one_seconds(), | |
{sent, "message 2"} = websocket_client_impl:send("message 2"), | |
wait_one_seconds(), | |
closed = websocket_client_impl:close(), | |
wait_one_seconds(), | |
?assertMatch([{send, "message 1"}, | |
{onopen, ok}, | |
{onmessage, "received 'message 1'"}, | |
{send, "message 2"}, | |
{onmessage, "received 'message 2'"}, | |
{close, ok}, | |
{onclose, ok}], websocket_client_impl:get_log()). | |
% --- helper functions ---------------------------------------------------------------------------- | |
wait_one_seconds() -> | |
timer:sleep(1000). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% @author Leandro Silva <[email protected]> | |
%% @copyright 2011 Leandro Silva. | |
%% @doc Helper module to test Erlang WebSocket-based application. | |
%% Thanks to Dave Bryson - https://github.com/davebryson/erlang_websocket | |
%% | |
%% This code assumes you're using my fork of websocket_client module, because of the start function: | |
%% https://github.com/leandrosilva/erlang_websocket/blob/master/src/websocket_client.erl | |
-module(websocket_client_impl). | |
-behaviour(websocket_client). | |
-export([start/3]). | |
-export([onmessage/1, onopen/0, onclose/0, close/0, send/1]). | |
-export([logger/1, get_log/0]). | |
start(Ip, Port, Path) -> | |
websocket_client:start(Ip, Port, Path, ?MODULE), | |
register(websocket_client_logger, spawn(?MODULE, logger, [[]])), | |
started. | |
send(Data) -> | |
websocket_client:write(Data), | |
add_log(send, Data), | |
{sent, Data}. | |
onmessage(Data) -> | |
add_log(onmessage, Data). | |
onclose() -> | |
add_log(onclose). | |
onopen() -> | |
add_log(onopen). | |
close() -> | |
websocket_client:close(), | |
add_log(close), | |
closed. | |
% --- websocket_client_logger process ------------------------------------------------------------- | |
logger(State) -> | |
receive | |
{add, {What, Data}} -> | |
NewState = [{What, Data} | State], | |
logger(NewState); | |
{get, Sender} -> | |
Sender ! {log, lists:reverse(State)}, | |
logger(State) | |
end. | |
add_log(What) -> | |
add_log(What, ok). | |
add_log(What, Data) -> | |
websocket_client_logger ! {add, {What, Data}}, | |
ok. | |
get_log() -> | |
websocket_client_logger ! {get, self()}, | |
receive | |
{log, State} -> | |
State; | |
_ -> | |
error | |
end. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment