]> git.ipfire.org Git - thirdparty/bind9.git/commitdiff
Bind to random port numbers in unit tests
authorMichał Kępień <michal@isc.org>
Wed, 8 Jan 2020 17:30:54 +0000 (18:30 +0100)
committerMichał Kępień <michal@isc.org>
Thu, 9 Jan 2020 09:44:28 +0000 (10:44 +0100)
Some unit tests need various managers to be created before they are run.
The interface manager spawned during libns tests listens on a fixed port
number, which causes intermittent issues when multiple tests using an
interface manager are run concurrently.  Make the interface manager
listen on a randomized port number to greatly reduce the risk of
multiple unit tests using the same port concurrently.

(cherry picked from commit ea7bddb4ca24a9ca4d12f8ecd8fa186699f0f5ab)

lib/ns/tests/listenlist_test.c
lib/ns/tests/nstest.c

index c79542b9b0a7e486c3289b4dd600754cba184839..5da470c8076363e581f1aace46e504ef3a744ab8 100644 (file)
@@ -28,6 +28,7 @@
 
 #include <isc/list.h>
 #include <isc/print.h>
+#include <isc/random.h>
 #include <isc/util.h>
 
 #include <dns/acl.h>
@@ -61,13 +62,14 @@ _teardown(void **state) {
 static void
 ns_listenlist_default_test(void **state) {
        isc_result_t result;
+       in_port_t port = 5300 + isc_random8();
        ns_listenlist_t *list = NULL;
        ns_listenelt_t *elt;
        int count;
 
        UNUSED(state);
 
-       result = ns_listenlist_default(mctx, 5300, -1, false, &list);
+       result = ns_listenlist_default(mctx, port, -1, false, &list);
        assert_int_equal(result, ISC_R_SUCCESS);
        assert_non_null(list);
 
@@ -94,7 +96,7 @@ ns_listenlist_default_test(void **state) {
 
        ns_listenlist_detach(&list);
 
-       result = ns_listenlist_default(mctx, 5300, -1, true, &list);
+       result = ns_listenlist_default(mctx, port, -1, true, &list);
        assert_int_equal(result, ISC_R_SUCCESS);
 
        assert_false(ISC_LIST_EMPTY(list->elts));
index b5e13c6ee12cc8310ab29f21008d371793cb7697..64804bef3edadc7c7d76cac8b14f518295552181 100644 (file)
@@ -175,6 +175,7 @@ scan_interfaces(isc_task_t *task, isc_event_t *event) {
 static isc_result_t
 create_managers(void) {
        isc_result_t result;
+       in_port_t port = 5300 + isc_random8();
        ns_listenlist_t *listenon = NULL;
        isc_event_t *event = NULL;
        ncpus = isc_os_ncpus();
@@ -199,7 +200,7 @@ create_managers(void) {
        CHECK(ns_clientmgr_create(mctx, sctx, taskmgr, timermgr,
                                  &clientmgr));
 
-       CHECK(ns_listenlist_default(mctx, 5300, -1, true, &listenon));
+       CHECK(ns_listenlist_default(mctx, port, -1, true, &listenon));
        ns_interfacemgr_setlistenon4(interfacemgr, listenon);
        ns_listenlist_detach(&listenon);