From f18f67022314eefced887df0a470a440f70f592c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ludovic=20Court=C3=A8s?= Date: Tue, 15 Mar 2022 14:38:40 +0100 Subject: [PATCH] tests: web-server: Wait until the server is listening. Fixes synchronization issues observed on slow or loaded machines, where client connection attempts would fail with ECONNREFUSED: https://issues.guix.gnu.org/54348 * test-suite/tests/web-server.test ("server is listening"): New test. --- test-suite/tests/web-server.test | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/test-suite/tests/web-server.test b/test-suite/tests/web-server.test index 486c99b58..f5081889f 100644 --- a/test-suite/tests/web-server.test +++ b/test-suite/tests/web-server.test @@ -1,6 +1,6 @@ ;;;; web-server.test --- HTTP server -*- mode: scheme; coding: utf-8; -*- ;;;; -;;;; Copyright (C) 2019, 2020 Free Software Foundation, Inc. +;;;; Copyright (C) 2019-2020, 2022 Free Software Foundation, Inc. ;;;; ;;;; This library is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU Lesser General Public @@ -76,6 +76,29 @@ (throw 'unresolved))) +(pass-if "server is listening" + ;; First, wait until the server is listening, up to a few seconds. + (let ((socket (socket AF_INET SOCK_STREAM 0))) + (let loop ((n 1)) + (define success? + (catch 'system-error + (lambda () + (format (current-error-port) + "connecting to the server, attempt #~a~%" n) + (connect socket AF_INET INADDR_LOOPBACK %port-number) + (close-port socket) + #t) + (lambda args + (if (and (= ECONNREFUSED (system-error-errno args)) + (<= n 15)) + #f + (apply throw args))))) + + (or success? + (begin + (sleep 1) + (loop (+ n 1))))))) + (pass-if-equal "GET /" "Hello, λ world!" (expect http-get "/" 200))