From patchwork Thu Jan 20 13:08:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: M X-Patchwork-Id: 36651 Return-Path: X-Original-To: patchwork@mira.cbaines.net Delivered-To: patchwork@mira.cbaines.net Received: by mira.cbaines.net (Postfix, from userid 113) id DC0BE27BBEA; Thu, 20 Jan 2022 19:45:05 +0000 (GMT) X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on mira.cbaines.net X-Spam-Level: X-Spam-Status: No, score=-2.7 required=5.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,URIBL_BLOCKED autolearn=unavailable autolearn_force=no version=3.4.6 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mira.cbaines.net (Postfix) with ESMTPS id 2F87E27BBE9 for ; Thu, 20 Jan 2022 19:45:05 +0000 (GMT) Received: from localhost ([::1]:55912 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nAdMk-0001Wb-LX for patchwork@mira.cbaines.net; Thu, 20 Jan 2022 14:45:02 -0500 Received: from eggs.gnu.org ([209.51.188.92]:50804) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nAXCX-0004EI-8F for guix-patches@gnu.org; Thu, 20 Jan 2022 08:10:06 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:35573) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nAXCV-0006Sf-MZ for guix-patches@gnu.org; Thu, 20 Jan 2022 08:10:04 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nAXCV-0004tA-IH for guix-patches@gnu.org; Thu, 20 Jan 2022 08:10:03 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#53389] [PATCH 1/9] tests: Support arbitrary HTTP request handlers. References: <6b1c1d98514b2547907a81a04c1241d9b865d6fa.camel@telenet.be> In-Reply-To: <6b1c1d98514b2547907a81a04c1241d9b865d6fa.camel@telenet.be> Resent-From: Maxime Devos Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Thu, 20 Jan 2022 13:10:03 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 53389 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 53389@debbugs.gnu.org Cc: ludo@gnu.org, Maxime Devos Received: via spool by 53389-submit@debbugs.gnu.org id=B53389.164268415818687 (code B ref 53389); Thu, 20 Jan 2022 13:10:03 +0000 Received: (at 53389) by debbugs.gnu.org; 20 Jan 2022 13:09:18 +0000 Received: from localhost ([127.0.0.1]:56700 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nAXBm-0004rF-1p for submit@debbugs.gnu.org; Thu, 20 Jan 2022 08:09:18 -0500 Received: from baptiste.telenet-ops.be ([195.130.132.51]:41976) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nAXBM-0004op-08 for 53389@debbugs.gnu.org; Thu, 20 Jan 2022 08:08:54 -0500 Received: from localhost.localdomain ([IPv6:2a02:1811:8c09:9d00:3c5f:2eff:feb0:ba5a]) by baptiste.telenet-ops.be with bizsmtp id l18q2600H4UW6Th0118qN2; Thu, 20 Jan 2022 14:08:50 +0100 From: Maxime Devos Date: Thu, 20 Jan 2022 13:08:41 +0000 Message-Id: <20220120130849.292178-1-maximedevos@telenet.be> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telenet.be; s=r22; t=1642684130; bh=+gPR/bwIo+N5qZniBsvVIBPU7dairx51qt2DzoeadNg=; h=From:To:Cc:Subject:Date; b=PZEIpQUAjd9W5GonwE4N8TbjIf6B2UeNQN9I2btDdTT0Q8IwjjxtLIHmXLn39PQH6 2DqHy3CKUQMCxUpL7o3ynfKI01+nk+Kg7AhTv4zydWEtJ5TPoO7HYf2uQgSjnQt5Si 0Pi5F+PVNYNzmvJzdF2b5+RoBp7Fg8rj3o9OJtEeMsUctRHtrX2lOXjGrDs1qXVsm4 elv4RafZ5f5bETmk4JHpX9ypHDE1ZWeT6e7lNPeBW6AJwOtvzv7+RB4NRP3AwQQ1Ev ZktAJ1Mdp+fGLzsyQlxPUF3zSMShkoBgswe1xfHz2pi8dy6c+ZnIBiEutx0EFMW8+W R2nVsYvVKD9HQ== X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+patchwork=mira.cbaines.net@gnu.org Sender: "Guix-patches" X-getmail-retrieved-from-mailbox: Patches An incompatible change to with-http-server has been made: it now also exits when the thunk exits. This change allows implementing with-http-server*. It also keeps threads from lingering if the thunk doesn't access all of RESPONSES+DATA. Usually, this change is fine, but it does not interact nicely with monads in tests/challenge, so a variant with-http-server/lingering preserving the old behaviour has been defined. * guix/tests/http.scm (call-with-http-server): Extract most functionality to ... (call-with-http-server*): ... this new procedure. Also stop the server thread after 'thunk' returns instead of when the last response has been sent unless requested not to. (with-http-server/keep-lingering): New macro. * tests/challenge.scm (call-mismatch-test): Use the 'keep-lingering' variant of 'with-http-server'. --- guix/tests/http.scm | 96 +++++++++++++++++++++++++++++++-------------- tests/challenge.scm | 24 ++++++------ 2 files changed, 80 insertions(+), 40 deletions(-) base-commit: 1bd250783d7118c3101dd2a6e090f3d6904b24a0 diff --git a/guix/tests/http.scm b/guix/tests/http.scm index 8f50eaefca..c42b4b8176 100644 --- a/guix/tests/http.scm +++ b/guix/tests/http.scm @@ -1,6 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2014, 2015, 2016, 2017, 2019 Ludovic Courtès -;;; Copyright © 2021 Maxime Devos +;;; Copyright © 2021, 2022 Maxime Devos ;;; ;;; This file is part of GNU Guix. ;;; @@ -26,7 +26,10 @@ #:use-module (srfi srfi-39) #:use-module (ice-9 match) #:export (with-http-server + with-http-server/keep-lingering + with-http-server* call-with-http-server + call-with-http-server* %http-server-port %local-url)) @@ -68,23 +71,15 @@ actually listened at (in case %http-server-port was 0)." (string-append "http://localhost:" (number->string port) "/foo/bar")) -(define* (call-with-http-server responses+data thunk) - "Call THUNK with an HTTP server running and returning RESPONSES+DATA on HTTP -requests. Each element of RESPONSES+DATA must be a tuple containing a -response and a string, or an HTTP response code and a string. +(define* (call-with-http-server* handle thunk #:key (keep-lingering? #false) + (last-response? (const #false))) + "Call THUNK with an HTTP server running and responding to HTTP requests +with HANDLE (see (guile)Web Server). %http-server-port will be set to the port listened at -The port listened at will be set for the dynamic extent of THUNK." - (define responses - (map (match-lambda - (((? response? response) data) - (list response data)) - (((? integer? code) data) - (list (build-response #:code code - #:reason-phrase "Such is life") - data))) - responses+data)) - +The port listened at will be set for the dynamic extent of THUNK. +The server will quit after THUNK returns, unless KEEP-LINGERING? is true. +It will also quit if LAST-RESPONSE? returns true." (define (http-write server client response body) "Write RESPONSE." (let* ((response (write-response response client)) @@ -94,8 +89,8 @@ The port listened at will be set for the dynamic extent of THUNK." (else (write-response-body response body))) (close-port port) - (when (null? responses) - (quit #t)) ;exit the server thread + (when (last-response?) + (throw 'quit)) (values))) ;; Mutex and condition variable to synchronize with the HTTP server. @@ -118,18 +113,15 @@ The port listened at will be set for the dynamic extent of THUNK." (@@ (web server http) http-close)) (define (server-body) - (define (handle request body) - (match responses - (((response data) rest ...) - (set! responses rest) - (values response data)))) - (let-values (((socket port) (open-http-server-socket))) (set! %http-real-server-port port) (catch 'quit (lambda () - (run-server handle stub-http-server - `(#:socket ,socket))) + ;; HANDLE might want to include the port in its responses, + ;; so set %http-server-port here as well. + (parameterize ((%http-server-port port)) + (run-server handle stub-http-server + `(#:socket ,socket)))) (lambda _ (close-port socket))))) @@ -137,12 +129,58 @@ The port listened at will be set for the dynamic extent of THUNK." (let ((server (make-thread server-body))) (wait-condition-variable %http-server-ready %http-server-lock) ;; Normally SERVER exits automatically once it has received a request. - (parameterize ((%http-server-port %http-real-server-port)) - (thunk))))) + (let-values ((results + (parameterize ((%http-server-port %http-real-server-port)) + (thunk)))) + (unless keep-lingering? + ;; exit the server thread + (system-async-mark (lambda () (throw 'quit)) server)) + (apply values results))))) + + +(define* (call-with-http-server responses+data thunk #:key (keep-lingering? #false)) + "Call THUNK with an HTTP server running and returning RESPONSES+DATA +on HTTP requests. Each element of RESPONSES+DATA must be a tuple containing a +response and a string, or an HTTP response code and a string. + +The argument RESPONSES+DATA is thunked. As such, RESPONSES+DATA can use +%http-server-port. %http-server-port will be set to the port listened at. +It will be set for the dynamic extent of THUNK and RESPONSES+DATA. + +The server will exit after the last response. When KEEP-LINGERING? is false, +the server will also exit after THUNK returns." + (define (responses) + (map (match-lambda + (((? response? response) data) + (list response data)) + (((? integer? code) data) + (list (build-response #:code code + #:reason-phrase "Such is life") + data))) + (responses+data))) + (define (handle request body) + (match (responses) + (((response data) rest ...) + (set! responses (const rest)) + (values response data)))) + (call-with-http-server* handle thunk #:keep-lingering? keep-lingering? + #:last-response? + (lambda () (null? (responses))))) (define-syntax with-http-server (syntax-rules () ((_ responses+data body ...) - (call-with-http-server responses+data (lambda () body ...))))) + (call-with-http-server (lambda () responses+data) (lambda () body ...))))) + +(define-syntax with-http-server/keep-lingering + (syntax-rules () + ((_ responses+data body ...) + (call-with-http-server (lambda () responses+data) (lambda () body ...) + #:keep-lingering? #true)))) + +(define-syntax with-http-server* + (syntax-rules () + ((_ handle body ...) + (call-with-http-server* handle (lambda () body ...))))) ;;; http.scm ends here diff --git a/tests/challenge.scm b/tests/challenge.scm index fdd5fd238e..c9de33ed34 100644 --- a/tests/challenge.scm +++ b/tests/challenge.scm @@ -1,5 +1,6 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2015, 2017, 2019, 2020 Ludovic Courtès +;;; Copyright © 2022 Maxime Devos ;;; ;;; This file is part of GNU Guix. ;;; @@ -198,17 +199,18 @@ value." (lambda (port) (write-file out2 port))))) (parameterize ((%http-server-port 9000)) - (with-http-server `((200 ,(make-narinfo item size1 hash1)) - (200 ,nar1)) - (parameterize ((%http-server-port 9001)) - (with-http-server `((200 ,(make-narinfo item size2 hash2)) - (200 ,nar2)) - (mlet* %store-monad ((urls -> (list (%local-url 9000) - (%local-url 9001))) - (reports (compare-contents (list item) - urls))) - (pk 'report reports) - (return (proc (car reports)))))))))))) + (with-http-server/keep-lingering + `((200 ,(make-narinfo item size1 hash1)) + (200 ,nar1)) + (parameterize ((%http-server-port 9001)) + (with-http-server/keep-lingering + `((200 ,(make-narinfo item size2 hash2)) + (200 ,nar2)) + (mlet* %store-monad ((urls -> (list (%local-url 9000) + (%local-url 9001))) + (reports (compare-contents (list item) + urls))) + (return (proc (car reports)))))))))))) (test-assertm "differing-files" (call-mismatch-test