Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added first test cases and code for the port opening

  • Loading branch information...
commit c107b71803146553de1370d3aae2b0129e994ed0 1 parent fc84668
Gianfranco Alongi authored
View
5 Two/Solution/Makefile
@@ -0,0 +1,5 @@
+all:
+ erlc -o ebin/ src/*.erl test/*.erl
+
+eunit:
+ erl -pa ebin/ -eval 'eunit:test(port,[verbose]),init:stop().'
View
21 Two/Solution/src/port.erl
@@ -0,0 +1,21 @@
+-module(port).
+-export([open/2,
+ close/1
+ ]).
+-record(port,{type :: echo,
+ socket :: gen_tcp:socket(),
+ number :: integer()
+ }).
+
+-spec(open(integer(),atom() | string()) -> {ok,gen_tcp:socket()}).
+open(Port,echo) ->
+ {ok,Sock} = gen_tcp:listen(Port,[{active,false}]),
+ {ok,#port{ type = echo,
+ socket = Sock,
+ number = Port}}.
+
+-spec(close(#port{}) -> ok).
+close(Port) ->
+ gen_tcp:close(Port#port.socket).
+
+
View
24 Two/Solution/test/port_tests.erl
@@ -0,0 +1,24 @@
+-module(port_tests).
+-include_lib("eunit/include/eunit.hrl").
+
+echo_port_open_close_test() ->
+ Port = 50001,
+ Type = echo,
+ {ok,Openport} = port:open(Port,Type),
+ assert_port_open(Port),
+ port:close(Openport),
+ assert_port_closed(Port).
+
+%% ------------------------------------------------------------
+assert_port_open(Port) ->
+ {ok,Sock} = gen_tcp:connect("localhost",Port,[]),
+ gen_tcp:close(Sock).
+
+assert_port_closed(Port) ->
+ {error,econnrefused} = gen_tcp:connect("localhost",Port,[]).
+
+
+
+
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.