-
-
Save charlenopires/865392 to your computer and use it in GitHub Desktop.
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_() -> | |
{"websocket_app", | |
{"when in WebSocket mode", | |
[ | |
{"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()). |
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