Skip to content

Commit

Permalink
Merge pull request #91 from civodul/test-with-os-allocated-port
Browse files Browse the repository at this point in the history
tests: Use an OS-allocated TCP port number.
  • Loading branch information
aconchillo authored Jul 19, 2023
2 parents 581319f + c01ce47 commit 7a49bc8
Showing 1 changed file with 11 additions and 25 deletions.
36 changes: 11 additions & 25 deletions tests/ports.scm
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
;; Fibers: cooperative, event-driven user-space threads.

;;;; Copyright (C) 2022 Ludovic Courtès <[email protected]>
;;;; Copyright (C) 2022, 2023 Ludovic Courtès <[email protected]>
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
Expand All @@ -25,28 +25,9 @@
#:use-module ((ice-9 ports internal)
#:select (port-read-wait-fd)))

(define* (bind* sock address)
;; Like 'bind', but retry upon EADDRINUSE.
(let loop ((n 5))
(define result
(catch 'system-error
(lambda ()
(bind sock address))
(lambda args
(if (and (= EADDRINUSE (system-error-errno args))
(> n 0))
'in-use
(apply throw args)))))

(when (eq? result 'in-use)
(sleep 1)
(loop (- n 1)))))


(run-fibers
(lambda ()
(let* ((address (make-socket-address AF_INET INADDR_LOOPBACK 5556))
(notification (make-channel)))
(let ((notification (make-channel)))
(spawn-fiber
(lambda ()
;; The server.
Expand All @@ -60,13 +41,17 @@
0)))
(pk 'listening-socket sock)
(setsockopt sock SOL_SOCKET SO_REUSEADDR 1)
(bind* sock address)

;; Let the OS pick an unused port.
(bind sock (make-socket-address AF_INET INADDR_LOOPBACK 0))

(listen sock 1)

(if (zero? n)
(begin
;; Let's go.
(put-message notification 'ready!)
(put-message notification
`(ready! ,(sockaddr:port (getsockname sock))))
(match (pk 'accepted-connection (accept sock SOCK_NONBLOCK))
((connection . _)
(display (pk 'received (read-line connection)) connection)
Expand Down Expand Up @@ -95,10 +80,11 @@

;; Wait for the server to be ready.
(match (get-message notification)
('ready!
(('ready! port)
;; Connect, send a message, and receive its echo.
(let ((sock (socket AF_INET (logior SOCK_NONBLOCK SOCK_STREAM)
0)))
0))
(address (make-socket-address AF_INET INADDR_LOOPBACK port)))
(connect sock address)
(pk 'connected address)
(display "hello!\n" sock)
Expand Down

0 comments on commit 7a49bc8

Please sign in to comment.