test: pick random port number for gcp pubsub mock server

This commit is contained in:
Zaiming (Stone) Shi 2023-04-18 15:07:57 +02:00
parent d0a7e7c406
commit 97b8f00508
2 changed files with 42 additions and 19 deletions

View File

@ -29,7 +29,14 @@ start_link(Port, Path) ->
start_link(Port, Path, false). start_link(Port, Path, false).
start_link(Port, Path, SSLOpts) -> start_link(Port, Path, SSLOpts) ->
supervisor:start_link({local, ?MODULE}, ?MODULE, [Port, Path, SSLOpts]). case Port of
random ->
PickedPort = pick_port_number(56000),
{ok, Pid} = supervisor:start_link({local, ?MODULE}, ?MODULE, [PickedPort, Path, SSLOpts]),
{ok, {PickedPort, Pid}};
_ ->
supervisor:start_link({local, ?MODULE}, ?MODULE, [Port, Path, SSLOpts])
end.
stop() -> stop() ->
try try
@ -103,3 +110,20 @@ default_handler(Req0, State) ->
Req0 Req0
), ),
{ok, Req, State}. {ok, Req, State}.
pick_port_number(Port) ->
case is_port_in_use(Port) of
true ->
pick_port_number(Port + 1);
false ->
Port
end.
is_port_in_use(Port) ->
case gen_tcp:listen(Port, [{reuseaddr, true}, {active, false}]) of
{ok, ListenSocket} ->
gen_tcp:close(ListenSocket),
false;
{error, eaddrinuse} ->
true
end.

View File

@ -70,22 +70,13 @@ init_per_suite(Config) ->
ok = emqx_connector_test_helpers:start_apps([emqx_resource, emqx_bridge, emqx_rule_engine]), ok = emqx_connector_test_helpers:start_apps([emqx_resource, emqx_bridge, emqx_rule_engine]),
{ok, _} = application:ensure_all_started(emqx_connector), {ok, _} = application:ensure_all_started(emqx_connector),
emqx_mgmt_api_test_util:init_suite(), emqx_mgmt_api_test_util:init_suite(),
HTTPHost = "localhost", Config.
HTTPPort = 56000,
HostPort = HTTPHost ++ ":" ++ integer_to_list(HTTPPort),
true = os:putenv("PUBSUB_EMULATOR_HOST", HostPort),
[
{http_host, HTTPHost},
{http_port, HTTPPort}
| Config
].
end_per_suite(_Config) -> end_per_suite(_Config) ->
emqx_mgmt_api_test_util:end_suite(), emqx_mgmt_api_test_util:end_suite(),
ok = emqx_common_test_helpers:stop_apps([emqx_conf]), ok = emqx_common_test_helpers:stop_apps([emqx_conf]),
ok = emqx_connector_test_helpers:stop_apps([emqx_bridge, emqx_resource, emqx_rule_engine]), ok = emqx_connector_test_helpers:stop_apps([emqx_bridge, emqx_resource, emqx_rule_engine]),
_ = application:stop(emqx_connector), _ = application:stop(emqx_connector),
os:unsetenv("PUBSUB_EMULATOR_HOST"),
ok. ok.
init_per_group(sync_query, Config) -> init_per_group(sync_query, Config) ->
@ -113,26 +104,26 @@ init_per_testcase(TestCase, Config0) when
1 -> 1 ->
[{skip_due_to_no_batching, true}]; [{skip_due_to_no_batching, true}];
_ -> _ ->
{ok, _} = start_echo_http_server(),
delete_all_bridges(), delete_all_bridges(),
Tid = install_telemetry_handler(TestCase), Tid = install_telemetry_handler(TestCase),
Config = generate_config(Config0), Config = generate_config(Config0),
put(telemetry_table, Tid), put(telemetry_table, Tid),
[{telemetry_table, Tid} | Config] {ok, HttpServer} = start_echo_http_server(),
[{telemetry_table, Tid}, {http_server, HttpServer} | Config]
end; end;
init_per_testcase(TestCase, Config0) -> init_per_testcase(TestCase, Config0) ->
ct:timetrap({seconds, 30}), ct:timetrap({seconds, 30}),
{ok, _} = start_echo_http_server(), {ok, HttpServer} = start_echo_http_server(),
delete_all_bridges(), delete_all_bridges(),
Tid = install_telemetry_handler(TestCase), Tid = install_telemetry_handler(TestCase),
Config = generate_config(Config0), Config = generate_config(Config0),
put(telemetry_table, Tid), put(telemetry_table, Tid),
[{telemetry_table, Tid} | Config]. [{telemetry_table, Tid}, {http_server, HttpServer} | Config].
end_per_testcase(_TestCase, _Config) -> end_per_testcase(_TestCase, _Config) ->
ok = snabbkaffe:stop(), ok = snabbkaffe:stop(),
delete_all_bridges(), delete_all_bridges(),
ok = emqx_connector_web_hook_server:stop(), ok = stop_echo_http_server(),
emqx_common_test_helpers:call_janitor(), emqx_common_test_helpers:call_janitor(),
ok. ok.
@ -242,7 +233,6 @@ success_http_handler() ->
start_echo_http_server() -> start_echo_http_server() ->
HTTPHost = "localhost", HTTPHost = "localhost",
HTTPPort = 56000,
HTTPPath = <<"/v1/projects/myproject/topics/mytopic:publish">>, HTTPPath = <<"/v1/projects/myproject/topics/mytopic:publish">>,
ServerSSLOpts = ServerSSLOpts =
[ [
@ -250,14 +240,23 @@ start_echo_http_server() ->
{versions, ['tlsv1.2', 'tlsv1.3']}, {versions, ['tlsv1.2', 'tlsv1.3']},
{ciphers, ["ECDHE-RSA-AES256-GCM-SHA384", "TLS_CHACHA20_POLY1305_SHA256"]} {ciphers, ["ECDHE-RSA-AES256-GCM-SHA384", "TLS_CHACHA20_POLY1305_SHA256"]}
] ++ certs(), ] ++ certs(),
{ok, _} = emqx_connector_web_hook_server:start_link(HTTPPort, HTTPPath, ServerSSLOpts), {ok, {HTTPPort, _Pid}} = emqx_connector_web_hook_server:start_link(
random, HTTPPath, ServerSSLOpts
),
ok = emqx_connector_web_hook_server:set_handler(success_http_handler()), ok = emqx_connector_web_hook_server:set_handler(success_http_handler()),
HTTPHost = "localhost",
HostPort = HTTPHost ++ ":" ++ integer_to_list(HTTPPort),
true = os:putenv("PUBSUB_EMULATOR_HOST", HostPort),
{ok, #{ {ok, #{
host_port => HTTPHost ++ ":" ++ integer_to_list(HTTPPort), host_port => HostPort,
host => HTTPHost, host => HTTPHost,
port => HTTPPort port => HTTPPort
}}. }}.
stop_echo_http_server() ->
os:unsetenv("PUBSUB_EMULATOR_HOST"),
ok = emqx_connector_web_hook_server:stop().
certs() -> certs() ->
CertsPath = emqx_common_test_helpers:deps_path(emqx, "etc/certs"), CertsPath = emqx_common_test_helpers:deps_path(emqx, "etc/certs"),
[ [