Skip to content

Commit

Permalink
improve eunit coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
jamespharaoh committed Apr 7, 2012
1 parent b09b7a6 commit 62d8f26
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 42 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -23,7 +23,7 @@ run: compile
-pa ebin deps/*/ebin deps/*/include \
-name notes@127.0.0.1 \
-config notes.config \
-eval "application:start (notes)."
-eval "notes_start:run ()."

erl: compile
erl \
Expand Down
6 changes: 6 additions & 0 deletions include/notes_global.hrl
Expand Up @@ -3,3 +3,9 @@
standard_error,
"TRACE ~w:~w~n",
[ ?MODULE, ?LINE ])).

-define (TRACE (Arg),
io:format (
standard_error,
"TRACE ~w:~w: ~p~n",
[ ?MODULE, ?LINE, Arg ])).
11 changes: 11 additions & 0 deletions src/delegate/notes_delegate_supervisor.erl
@@ -0,0 +1,11 @@
-module (notes_delegate_supervisor).

-ifndef (TEST).

-export ([
start_link/3 ]).

start_link (Name, Module, Args) ->
supervisor:start_link (Name, Module, Args).

-endif.
41 changes: 41 additions & 0 deletions src/misc/notes_start.erl
@@ -0,0 +1,41 @@
-module (notes_start).

-ifndef (TEST).

-export ([
run/0 ]).

run () ->

% start system

ok = application:start (compiler),
ok = application:start (syntax_tools),
ok = application:start (crypto),
ok = application:start (inets),
ok = application:start (public_key),
ok = application:start (ssl),
ok = application:start (xmerl),

% start mochiweb

ok = application:start (mochiweb),

% start openid

ok = application:start (ibrowse),
ok = application:start (openid),

% start nitrogen

ok = application:start (nprocreg),
ok = application:start (sasl),
ok = application:start (nitrogen_core),

% start notes

ok = application:start (notes),

ok.

-endif.
57 changes: 17 additions & 40 deletions src/misc/notes_supervisor.erl
Expand Up @@ -7,58 +7,35 @@
init/1,
loop/1 ]).

%% =============================================================== api functions
-include ("notes_global.hrl").

% api functions

start_link () ->

supervisor:start_link (
notes_delegate_supervisor:start_link (
{ local, ?MODULE },
?MODULE,
[ ]).

%% ======================================================== supervisor callbacks
% supervisor callbacks

init ([]) ->

% start process registry

application:start (nprocreg),

% start mochiweb

application:load (mochiweb),
% mochiweb

{ ok, BindAddress } =
application:get_env (bind_address),
notes_config:get (bind_address),

{ ok, Port } =
application:get_env (port),
notes_config:get (port),

{ ok, ServerName } =
application:get_env (server_name),

Options = [
{ name, ServerName },
MochiOptions = [
{ ip, BindAddress },
{ port, Port },
{ loop, fun ?MODULE:loop/1 }
],

{ ok, _MochiPid } = mochiweb_http:start (Options),

% start openid

ok = application:start (crypto),
ok = application:start (ibrowse),
ok = application:start (openid),
ok = application:start (public_key),
ok = application:start (ssl),

% start nitrogen

ok = application:start (sasl),
ok = application:start (nitrogen_core),

% and return

MaxRestart = 5,
Expand All @@ -67,19 +44,19 @@ init ([]) ->

ChildSpecs = [

%{ data,
% { data, start_link, [] },
% permanent,
% 10000,
% worker,
% [ data ] },
{ mochiweb,
{ mochiweb_http, start_link, [ MochiOptions ] },
permanent,
10000,
worker,
[ mochiweb_http ] },

{ openid,
{ openid_srv, start_link, [ openid ] },
permanent,
10000,
worker,
[ openid ] }
[ openid_srv ] }
],

SupervisorOptions = { RestartPolicy, ChildSpecs },
Expand All @@ -89,7 +66,7 @@ init ([]) ->
loop (Req) ->

{ ok, DocRoot } =
application:get_env (document_root),
notes_config:get (document_root),

RequestBridge =
simple_bridge:make_request (
Expand Down
1 change: 0 additions & 1 deletion src/notes.app.src
Expand Up @@ -16,7 +16,6 @@
{ env, [
{ bind_address, "0.0.0.0" },
{ port, 8000 },
{ server_name, notes },
{ document_root, "./static" },
{ base_url, "http://localhost:8000/" }
] },
Expand Down
112 changes: 112 additions & 0 deletions test/misc/notes_supervisor_test.erl
@@ -0,0 +1,112 @@
-module (notes_supervisor_test).

-include ("notes_global.hrl").
-include ("notes_test.hrl").

% macros

-define (MOCK_MODULES, [
notes_config,
notes_delegate_supervisor,
simple_bridge ]).

-define (TARGET, notes_supervisor).

% tests

start_link_test () ->

Name = { local, ?TARGET },

?EXPECT,

em:strict (Em, notes_delegate_supervisor, start_link,
[ Name, ?TARGET, [] ],
{ return, ok }),

?REPLAY,

?assertEqual (
ok,
?TARGET:start_link ()),

?VERIFY.

init_test () ->

MochiOptions = [
{ ip, "1.2.3.4" },
{ port, 5678 },
{ loop, fun ?TARGET:loop/1 }
],

?EXPECT,

em:strict (Em, notes_config, get,
[ bind_address ],
{ return, { ok, "1.2.3.4" } }),

em:strict (Em, notes_config, get,
[ port ],
{ return, { ok, 5678 } }),

?REPLAY,

?assertEqual (

{ ok,

{ { one_for_one, 5, 100 },

[ { mochiweb,
{ mochiweb_http, start_link, [ MochiOptions ] },
permanent,
10000,
worker,
[ mochiweb_http ] },

{ openid,
{ openid_srv, start_link, [ openid ] },
permanent,
10000,
worker,
[ openid_srv ] }
]
}
},

?TARGET:init ([ ])),

?VERIFY.

loop_test () ->

?EXPECT,

em:strict (Em, notes_config, get,
[ document_root ],
{ return, { ok, "document root" } }),

em:strict (Em, simple_bridge, make_request,
[ mochiweb_request_bridge, { "request", "document root" } ],
{ return, "request bridge" }),

em:strict (Em, simple_bridge, make_response,
[ mochiweb_response_bridge, { "request", "document root" } ],
{ return, "response bridge" }),

em:strict (Em, nitrogen, init_request,
[ "request bridge", "response bridge" ],
{ return, "response bridge" }),

em:strict (Em, nitrogen, run,
[ ],
{ return, ok }),

?REPLAY,

?assertEqual (
ok,
?TARGET:loop ("request")),

?VERIFY.

0 comments on commit 62d8f26

Please sign in to comment.