1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 20:00:19 +02:00

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.
This commit is contained in:
Ludovic Courtès 2022-03-15 14:38:40 +01:00
parent f047133e7b
commit f18f670223

View file

@ -1,6 +1,6 @@
;;;; web-server.test --- HTTP server -*- mode: scheme; coding: utf-8; -*- ;;;; 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 ;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public ;;;; modify it under the terms of the GNU Lesser General Public
@ -76,6 +76,29 @@
(throw 'unresolved))) (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 /" (pass-if-equal "GET /"
"Hello, λ world!" "Hello, λ world!"
(expect http-get "/" 200)) (expect http-get "/" 200))