guix-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

02/14: marionette: Add 'wait-for-tcp-port'.


From: Ludovic Courtès
Subject: 02/14: marionette: Add 'wait-for-tcp-port'.
Date: Fri, 1 Jun 2018 07:52:17 -0400 (EDT)

civodul pushed a commit to branch master
in repository guix.

commit 7a4e2eaab34f7fad6951f312203ac0d9dfa3d44a
Author: Ludovic Courtès <address@hidden>
Date:   Fri Jun 1 10:01:05 2018 +0200

    marionette: Add 'wait-for-tcp-port'.
    
    * gnu/build/marionette.scm (wait-for-tcp-port): New procedure.
    * gnu/tests/dict.scm (run-dicod-test)["connect inside"]: Use it instead
    of the inline loop.
---
 gnu/build/marionette.scm | 27 +++++++++++++++++++++++++++
 gnu/tests/dict.scm       | 19 ++-----------------
 2 files changed, 29 insertions(+), 17 deletions(-)

diff --git a/gnu/build/marionette.scm b/gnu/build/marionette.scm
index 173a67c..bb018fc 100644
--- a/gnu/build/marionette.scm
+++ b/gnu/build/marionette.scm
@@ -26,6 +26,7 @@
             make-marionette
             marionette-eval
             wait-for-file
+            wait-for-tcp-port
             marionette-control
             marionette-screen-text
             wait-for-screen-text
@@ -187,6 +188,32 @@ FILE has not shown up after TIMEOUT seconds, raise an 
error."
     ('failure
      (error "file didn't show up" file))))
 
+(define* (wait-for-tcp-port port marionette
+                            #:key (timeout 20))
+  "Wait for up to TIMEOUT seconds for PORT to accept connections in
+MARIONETTE.  Raise an error on failure."
+  ;; Note: The 'connect' loop has to run within the guest because, when we
+  ;; forward ports to the host, connecting to the host never raises
+  ;; ECONNREFUSED.
+  (match (marionette-eval
+          `(begin
+             (let ((sock (socket PF_INET SOCK_STREAM 0)))
+               (let loop ((i 0))
+                 (catch 'system-error
+                   (lambda ()
+                     (connect sock AF_INET INADDR_LOOPBACK ,port)
+                     'success)
+                   (lambda args
+                     (if (< i ,timeout)
+                         (begin
+                           (sleep 1)
+                           (loop (+ 1 i)))
+                         'failure))))))
+          marionette)
+    ('success #t)
+    ('failure
+     (error "nobody's listening on port" port))))
+
 (define (marionette-control command marionette)
   "Run COMMAND in the QEMU monitor of MARIONETTE.  COMMAND is a string such as
 \"sendkey ctrl-alt-f1\" or \"screendump foo.ppm\" (info \"(qemu-doc)
diff --git a/gnu/tests/dict.scm b/gnu/tests/dict.scm
index b9c741e..4431e37 100644
--- a/gnu/tests/dict.scm
+++ b/gnu/tests/dict.scm
@@ -1,5 +1,5 @@
 ;;; GNU Guix --- Functional package management for GNU
-;;; Copyright © 2017 Ludovic Courtès <address@hidden>
+;;; Copyright © 2017, 2018 Ludovic Courtès <address@hidden>
 ;;;
 ;;; This file is part of GNU Guix.
 ;;;
@@ -96,22 +96,7 @@
           ;; Wait until dicod is actually listening.
           ;; TODO: Use a PID file instead.
           (test-assert "connect inside"
-            (marionette-eval
-             '(begin
-                (use-modules (ice-9 rdelim))
-                (let ((sock (socket PF_INET SOCK_STREAM 0)))
-                  (let loop ((i 0))
-                    (pk 'try i)
-                    (catch 'system-error
-                      (lambda ()
-                        (connect sock AF_INET INADDR_LOOPBACK 2628))
-                      (lambda args
-                        (pk 'connection-error args)
-                        (when (< i 20)
-                          (sleep 1)
-                          (loop (+ 1 i))))))
-                  (read-line sock 'concat)))
-             marionette))
+            (wait-for-tcp-port 2628 marionette))
 
           (test-assert "connect"
             (let ((addr (make-socket-address AF_INET INADDR_LOOPBACK 8000)))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]