Skip to content

Commit

Permalink
Initial commit together with the first test for creating a named pipe
Browse files Browse the repository at this point in the history
Also the rebar.config file
  • Loading branch information
Gianfranco Alongi committed Mar 21, 2012
1 parent e07dc11 commit 85cdf4b
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 0 deletions.
1 change: 1 addition & 0 deletions Three/Solution/rebar.config
@@ -0,0 +1 @@
{cover_enabled,true}.
3 changes: 3 additions & 0 deletions Three/Solution/src/message.hrl
@@ -0,0 +1,3 @@
-record(message,{body :: binary(),
byte_size :: non_neg_integer()
}).
12 changes: 12 additions & 0 deletions Three/Solution/src/pubsub.app.src
@@ -0,0 +1,12 @@
{application, pubsub,
[
{description, ""},
{vsn, "1"},
{registered, []},
{applications, [
kernel,
stdlib
]},
{mod, { pubsub_app, []}},
{env, []}
]}.
16 changes: 16 additions & 0 deletions Three/Solution/src/pubsub_app.erl
@@ -0,0 +1,16 @@
-module(pubsub_app).

-behaviour(application).

%% Application callbacks
-export([start/2, stop/1]).

%% ===================================================================
%% Application callbacks
%% ===================================================================

start(_StartType, _StartArgs) ->
pubsub_sup:start_link().

stop(_State) ->
ok.
64 changes: 64 additions & 0 deletions Three/Solution/src/pubsub_pipes.erl
@@ -0,0 +1,64 @@
%%%-------------------------------------------------------------------
%%% @author Gianfranco Alongi <zenon@zentop.local>
%%% @copyright (C) 2012, Gianfranco Alongi
%%% Created : 21 Mar 2012 by Gianfranco Alongi <zenon@zentop.local>
%%%-------------------------------------------------------------------
-module(pubsub_pipes).
-behaviour(gen_server).

-export([new_pipe/1,
get_pipes/0
]).
-export([start_link/0,
stop/0
]).

%% gen_server callbacks
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).

-record(state, {pipes :: atom() %% ets table
}).

%%%===================================================================
start_link() ->
gen_server:start_link(?MODULE, ?MODULE, [], []).

stop() ->
gen_server:call(?MODULE,stop).

-spec(new_pipe(string()) -> ok).
new_pipe(PipeName) ->
gen_server:call(?MODULE,{create_pipe,PipeName}).

-spec(get_pipes() -> [string()]).
get_pipes() ->
gen_server:call(?MODULE,get_pipes).

%%%===================================================================
init([]) ->
{ok, #state{pipes = ets:new(pipes,[bag])}}.

handle_call({create_pipe,PipeName}, _From, State) ->
ets:insert_new(State#state.pipes,{PipeName,[]}),
{reply, ok, State};

handle_call(get_pipes,_From,State) ->
Pipes = ets:foldl(fun({Key,_},Acc) -> [Key|Acc] end,
[],
State#state.pipes),
{reply,Pipes, State}.

handle_cast(_Msg, State) ->
{noreply, State}.

handle_info(_Info, State) ->
{noreply, State}.

terminate(_Reason, _State) ->
ok.

code_change(_OldVsn, State, _Extra) ->
{ok, State}.

%%%===================================================================
28 changes: 28 additions & 0 deletions Three/Solution/src/pubsub_sup.erl
@@ -0,0 +1,28 @@

-module(pubsub_sup).

-behaviour(supervisor).

%% API
-export([start_link/0]).

%% Supervisor callbacks
-export([init/1]).

%% Helper macro for declaring children of supervisor
-define(CHILD(I, Type), {I, {I, start_link, []}, permanent, 5000, Type, [I]}).

%% ===================================================================
%% API functions
%% ===================================================================

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

%% ===================================================================
%% Supervisor callbacks
%% ===================================================================

init([]) ->
{ok, { {one_for_one, 5, 10}, []} }.

20 changes: 20 additions & 0 deletions Three/Solution/test/pubsub_pipes_tests.erl
@@ -0,0 +1,20 @@
-module(pubsub_pipes_tests).
-include_lib("eunit/include/eunit.hrl").

create_pipe_test() ->
{setup,
fun setup/0,
fun cleanup/1,
fun() ->
PipeName = "A",
{ok,_} = pubsub_pipes:new_pipe(PipeName),
?assertEqual([PipeName],pubsub_pipes:get_pipes())
end}.

setup() ->
pubsub_pipes:start_link().

cleanup(_) ->
pubsub_pipes:stop().


0 comments on commit 85cdf4b

Please sign in to comment.