Browse Source

Move listener initialization to cowboy_test

Loïc Hoguin 11 years ago
parent
commit
25a17a2590
4 changed files with 65 additions and 59 deletions
  1. 28 0
      test/cowboy_test.erl
  2. 28 42
      test/http_SUITE.erl
  3. 3 8
      test/spdy_SUITE.erl
  4. 6 9
      test/ws_SUITE.erl

+ 28 - 0
test/cowboy_test.erl

@@ -49,6 +49,34 @@ all(Suite) ->
 		string:substr(atom_to_list(F), 1, 3) =/= "do_"
 		string:substr(atom_to_list(F), 1, 3) =/= "do_"
 	]).
 	]).
 
 
+%% Listeners initialization.
+
+init_http(Ref, ProtoOpts, Config) ->
+	{ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [
+		{max_keepalive, 50},
+		{timeout, 500}
+		|ProtoOpts]),
+	Port = ranch:get_port(Ref),
+	[{type, tcp}, {port, Port}, {opts, []}|Config].
+
+init_https(Ref, ProtoOpts, Config) ->
+	{_, Cert, Key} = ct_helper:make_certs(),
+	Opts = [{cert, Cert}, {key, Key}],
+	{ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [
+		{max_keepalive, 50},
+		{timeout, 500}
+		|ProtoOpts]),
+	Port = ranch:get_port(Ref),
+	[{type, ssl}, {port, Port}, {opts, Opts}|Config].
+
+init_spdy(Ref, ProtoOpts, Config) ->
+	{_, Cert, Key} = ct_helper:make_certs(),
+	Opts = [{cert, Cert}, {key, Key}],
+	{ok, _} = cowboy:start_spdy(Ref, 100, Opts ++ [{port, 0}],
+		ProtoOpts),
+	Port = ranch:get_port(Ref),
+	[{type, ssl}, {port, Port}, {opts, Opts}|Config].
+
 %% Support functions for testing using Gun.
 %% Support functions for testing using Gun.
 
 
 gun_open(Config) ->
 gun_open(Config) ->

+ 28 - 42
test/http_SUITE.erl

@@ -79,39 +79,26 @@ init_per_suite(Config) ->
 end_per_suite(Config) ->
 end_per_suite(Config) ->
 	ct_helper:delete_static_dir(config(static_dir, Config)).
 	ct_helper:delete_static_dir(config(static_dir, Config)).
 
 
-init_tcp_group(Ref, ProtoOpts, Config) ->
-	Transport = ranch_tcp,
-	{ok, _} = cowboy:start_http(Ref, 100, [{port, 0}], [
+init_per_group(Name = http, Config) ->
+	cowboy_test:init_http(Name, [
+		{env, [{dispatch, init_dispatch(Config)}]}
+	], Config);
+init_per_group(Name = https, Config) ->
+	cowboy_test:init_https(Name, [
+		{env, [{dispatch, init_dispatch(Config)}]}
+	], Config);
+init_per_group(Name = http_compress, Config) ->
+	cowboy_test:init_http(Name, [
 		{env, [{dispatch, init_dispatch(Config)}]},
 		{env, [{dispatch, init_dispatch(Config)}]},
-		{max_keepalive, 50},
-		{timeout, 500}
-		|ProtoOpts]),
-	Port = ranch:get_port(Ref),
-	[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config].
-
-init_ssl_group(Ref, ProtoOpts, Config) ->
-	Transport = ranch_ssl,
-	{_, Cert, Key} = ct_helper:make_certs(),
-	Opts = [{cert, Cert}, {key, Key}],
-	{ok, _} = cowboy:start_https(Ref, 100, Opts ++ [{port, 0}], [
+		{compress, true}
+	], Config);
+init_per_group(Name = https_compress, Config) ->
+	cowboy_test:init_https(Name, [
 		{env, [{dispatch, init_dispatch(Config)}]},
 		{env, [{dispatch, init_dispatch(Config)}]},
-		{max_keepalive, 50},
-		{timeout, 500}
-		|ProtoOpts]),
-	Port = ranch:get_port(Ref),
-	[{type, ssl}, {port, Port}, {opts, Opts}, {transport, Transport}|Config].
-
-init_per_group(http, Config) ->
-	init_tcp_group(http, [], Config);
-init_per_group(https, Config) ->
-	init_ssl_group(https, [], Config);
-init_per_group(http_compress, Config) ->
-	init_tcp_group(http_compress, [{compress, true}], Config);
-init_per_group(https_compress, Config) ->
-	init_ssl_group(https_compress, [{compress, true}], Config);
+		{compress, true}
+	], Config);
 %% Most, if not all of these, should be in separate test suites.
 %% Most, if not all of these, should be in separate test suites.
 init_per_group(onrequest, Config) ->
 init_per_group(onrequest, Config) ->
-	Transport = ranch_tcp,
 	{ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [
 	{ok, _} = cowboy:start_http(onrequest, 100, [{port, 0}], [
 		{env, [{dispatch, init_dispatch(Config)}]},
 		{env, [{dispatch, init_dispatch(Config)}]},
 		{max_keepalive, 50},
 		{max_keepalive, 50},
@@ -119,9 +106,8 @@ init_per_group(onrequest, Config) ->
 		{timeout, 500}
 		{timeout, 500}
 	]),
 	]),
 	Port = ranch:get_port(onrequest),
 	Port = ranch:get_port(onrequest),
-	[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
+	[{type, tcp}, {port, Port}, {opts, []}|Config];
 init_per_group(onresponse, Config) ->
 init_per_group(onresponse, Config) ->
-	Transport = ranch_tcp,
 	{ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
 	{ok, _} = cowboy:start_http(onresponse, 100, [{port, 0}], [
 		{env, [{dispatch, init_dispatch(Config)}]},
 		{env, [{dispatch, init_dispatch(Config)}]},
 		{max_keepalive, 50},
 		{max_keepalive, 50},
@@ -129,9 +115,8 @@ init_per_group(onresponse, Config) ->
 		{timeout, 500}
 		{timeout, 500}
 	]),
 	]),
 	Port = ranch:get_port(onresponse),
 	Port = ranch:get_port(onresponse),
-	[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
+	[{type, tcp}, {port, Port}, {opts, []}|Config];
 init_per_group(onresponse_capitalize, Config) ->
 init_per_group(onresponse_capitalize, Config) ->
-	Transport = ranch_tcp,
 	{ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
 	{ok, _} = cowboy:start_http(onresponse_capitalize, 100, [{port, 0}], [
 		{env, [{dispatch, init_dispatch(Config)}]},
 		{env, [{dispatch, init_dispatch(Config)}]},
 		{max_keepalive, 50},
 		{max_keepalive, 50},
@@ -139,9 +124,8 @@ init_per_group(onresponse_capitalize, Config) ->
 		{timeout, 500}
 		{timeout, 500}
 	]),
 	]),
 	Port = ranch:get_port(onresponse_capitalize),
 	Port = ranch:get_port(onresponse_capitalize),
-	[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
+	[{type, tcp}, {port, Port}, {opts, []}|Config];
 init_per_group(parse_host, Config) ->
 init_per_group(parse_host, Config) ->
-	Transport = ranch_tcp,
 	Dispatch = cowboy_router:compile([
 	Dispatch = cowboy_router:compile([
 		{'_', [
 		{'_', [
 			{"/req_attr", http_req_attr, []}
 			{"/req_attr", http_req_attr, []}
@@ -153,20 +137,18 @@ init_per_group(parse_host, Config) ->
 		{timeout, 500}
 		{timeout, 500}
 	]),
 	]),
 	Port = ranch:get_port(http),
 	Port = ranch:get_port(http),
-	[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config];
+	[{type, tcp}, {port, Port}, {opts, []}|Config];
 init_per_group(set_env, Config) ->
 init_per_group(set_env, Config) ->
-	Transport = ranch_tcp,
 	{ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
 	{ok, _} = cowboy:start_http(set_env, 100, [{port, 0}], [
 		{env, [{dispatch, []}]},
 		{env, [{dispatch, []}]},
 		{max_keepalive, 50},
 		{max_keepalive, 50},
 		{timeout, 500}
 		{timeout, 500}
 	]),
 	]),
 	Port = ranch:get_port(set_env),
 	Port = ranch:get_port(set_env),
-	[{type, tcp}, {port, Port}, {opts, []}, {transport, Transport}|Config].
+	[{type, tcp}, {port, Port}, {opts, []}|Config].
 
 
 end_per_group(Name, _) ->
 end_per_group(Name, _) ->
-	cowboy:stop_listener(Name),
-	ok.
+	cowboy:stop_listener(Name).
 
 
 %% Dispatch configuration.
 %% Dispatch configuration.
 
 
@@ -436,8 +418,12 @@ http10_chunkless(Config) ->
 http10_hostless(Config) ->
 http10_hostless(Config) ->
 	Port10 = config(port, Config) + 10,
 	Port10 = config(port, Config) + 10,
 	Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
 	Name = list_to_atom("http10_hostless_" ++ integer_to_list(Port10)),
-	ranch:start_listener(Name, 5,
-		config(transport, Config), config(opts, Config) ++ [{port, Port10}],
+	Transport = case config(type, Config) of
+		tcp -> ranch_tcp;
+		ssl -> ranch_ssl
+	end,
+	ranch:start_listener(Name, 5, Transport,
+		config(opts, Config) ++ [{port, Port10}],
 		cowboy_protocol, [
 		cowboy_protocol, [
 			{env, [{dispatch, cowboy_router:compile([
 			{env, [{dispatch, cowboy_router:compile([
 				{'_', [{"/http1.0/hostless", http_handler, []}]}])}]},
 				{'_', [{"/http1.0/hostless", http_handler, []}]}])}]},

+ 3 - 8
test/spdy_SUITE.erl

@@ -40,17 +40,12 @@ end_per_suite(Config) ->
 	ct_helper:delete_static_dir(config(static_dir, Config)).
 	ct_helper:delete_static_dir(config(static_dir, Config)).
 
 
 init_per_group(Name, Config) ->
 init_per_group(Name, Config) ->
-	{_, Cert, Key} = ct_helper:make_certs(),
-	Opts = [{cert, Cert}, {key, Key}],
-	{ok, _} = cowboy:start_spdy(Name, 100, Opts ++ [{port, 0}], [
+	cowboy_test:init_spdy(Name, [
 		{env, [{dispatch, init_dispatch(Config)}]}
 		{env, [{dispatch, init_dispatch(Config)}]}
-	]),
-	Port = ranch:get_port(Name),
-	[{port, Port}, {type, ssl}|Config].
+	], Config).
 
 
 end_per_group(Name, _) ->
 end_per_group(Name, _) ->
-	cowboy:stop_listener(Name),
-	ok.
+	cowboy:stop_listener(Name).
 
 
 %% Dispatch configuration.
 %% Dispatch configuration.
 
 

+ 6 - 9
test/ws_SUITE.erl

@@ -29,7 +29,7 @@ groups() ->
 init_per_suite(Config) ->
 init_per_suite(Config) ->
 	Config.
 	Config.
 
 
-init_per_group(autobahn, Config) ->
+init_per_group(Name = autobahn, Config) ->
 	%% Some systems have it named pip2.
 	%% Some systems have it named pip2.
 	Out = os:cmd("pip show autobahntestsuite ; pip2 show autobahntestsuite"),
 	Out = os:cmd("pip show autobahntestsuite ; pip2 show autobahntestsuite"),
 	case string:str(Out, "autobahntestsuite") of
 	case string:str(Out, "autobahntestsuite") of
@@ -40,21 +40,18 @@ init_per_group(autobahn, Config) ->
 				"http://autobahn.ws/testsuite/installation.html"),
 				"http://autobahn.ws/testsuite/installation.html"),
 			{skip, "Autobahn Test Suite not installed."};
 			{skip, "Autobahn Test Suite not installed."};
 		_ ->
 		_ ->
-			{ok, _} = cowboy:start_http(autobahn, 100, [{port, 33080}], [
+			{ok, _} = cowboy:start_http(Name, 100, [{port, 33080}], [
 				{env, [{dispatch, init_dispatch()}]}]),
 				{env, [{dispatch, init_dispatch()}]}]),
 			Config
 			Config
 	end;
 	end;
-init_per_group(ws, Config) ->
-	cowboy:start_http(ws, 100, [{port, 0}], [
+init_per_group(Name = ws, Config) ->
+	cowboy_test:init_http(Name, [
 		{env, [{dispatch, init_dispatch()}]},
 		{env, [{dispatch, init_dispatch()}]},
 		{compress, true}
 		{compress, true}
-	]),
-	Port = ranch:get_port(ws),
-	[{port, Port}|Config].
+	], Config).
 
 
 end_per_group(Listener, _Config) ->
 end_per_group(Listener, _Config) ->
-	cowboy:stop_listener(Listener),
-	ok.
+	cowboy:stop_listener(Listener).
 
 
 %% Dispatch configuration.
 %% Dispatch configuration.