[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnunet-scheme] 176/324: tests/utils: New utilities for tests.
From: |
gnunet |
Subject: |
[gnunet-scheme] 176/324: tests/utils: New utilities for tests. |
Date: |
Tue, 21 Sep 2021 13:23:36 +0200 |
This is an automated email from the git hooks/post-receive script.
maxime-devos pushed a commit to branch master
in repository gnunet-scheme.
commit aeb4d31b176543ac46a219bb560d5344c1a98848
Author: Maxime Devos <maximedevos@telenet.be>
AuthorDate: Fri Aug 13 16:39:15 2021 +0200
tests/utils: New utilities for tests.
* tests/util.scm
(call-with-temporary-directory)
(call-with-services)
(call-with-services/fibers): New procedure.
---
tests/utils.scm | 83 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 82 insertions(+), 1 deletion(-)
diff --git a/tests/utils.scm b/tests/utils.scm
index 0d91a9a..365fc40 100644
--- a/tests/utils.scm
+++ b/tests/utils.scm
@@ -17,8 +17,16 @@
;; SPDX-License-Identifier: AGPL3.0-or-later
(define-module (tests utils)
#:use-module (srfi srfi-8)
+ #:use-module (ice-9 match)
+ #:use-module ((rnrs hashtables) #:prefix #{rnrs:}#)
+ #:use-module ((rnrs arithmetic bitwise)
+ #:select (bitwise-ior))
#:use-module ((rnrs base) #:select (assert))
- #:export (conservative-gc? calls-in-tail-position?))
+ #:use-module ((fibers) #:prefix #{fibers:}#)
+ #:autoload (gnu gnunet config db) (hash->configuration)
+ #:export (conservative-gc? calls-in-tail-position?
+ call-with-services
+ call-with-services/fibers))
;; Current versions of guile (at least 3.0.5) use a conservative
;; garbage collector, so some tests concerning garbage collection
@@ -54,3 +62,76 @@ times."
(assert (not (calls-in-tail-position? (lambda (thunk) (+ 1 (thunk))))))
#;
(assert (not (calls-in-tail-position? (lambda (thunk) (for-each thunk '("bla"
"bla"))))))
+
+(define (call-with-temporary-directory proc)
+ (let ((file (mkdtemp (in-vicinity (or (getenv "TMPDIR") "/tmp")
+ "test-XXXXXX"))))
+ (with-exception-handler
+ (lambda (e)
+ (system* "rm" "-r" file)
+ (raise-exception e))
+ (lambda ()
+ (call-with-values
+ (lambda () (proc file))
+ (lambda the-values
+ (system* "rm" "-r" file)
+ (apply values the-values)))))))
+
+(define (call-with-services service-alist proc)
+ "Call the procedure @var{proc} with a configuration database
+and a procedure behaving like @code{spawn-fiber}, in an environment
+where the services listed in @var{service-alist} can
+be connected to. The heads in @var{service-alist} are the names of
+the services and each tails is a list of a procedure accepting ports
+(connected to the client) and the procedure behaving like @code{spawn-fiber}."
+ (define %thread-table (make-hash-table))
+ (define (wrapped-spawn-fiber thunk)
+ (define o (list))
+ (hashq-set! %thread-table o 'running)
+ (fibers:spawn-fiber
+ (lambda ()
+ (with-exception-handler
+ (lambda (e)
+ (hashq-set! %thread-table o (cons 'exception e))
+ (raise-exception e))
+ thunk)))
+ (values))
+ ;; The hash function isn't very efficient but is sufficient.
+ (define config-hash (rnrs:make-hashtable (const 0) equal?))
+ (call-with-temporary-directory
+ (lambda (dir)
+ (define (start-service key+value)
+ (define where (in-vicinity dir (string-append (car key+value) ".sock")))
+ (rnrs:hashtable-set! config-hash (cons (car key+value) "UNIXPATH")
+ where)
+ (wrapped-spawn-fiber
+ (lambda ()
+ (define sock (socket AF_UNIX SOCK_STREAM 0))
+ (bind sock AF_UNIX where)
+ (listen sock 40)
+ (fcntl sock F_SETFL
+ (bitwise-ior (fcntl sock F_GETFL) O_NONBLOCK))
+ (let loop ()
+ (define client-sock
+ (car (accept sock (logior SOCK_NONBLOCK
+ SOCK_CLOEXEC))))
+ (wrapped-spawn-fiber
+ (lambda ()
+ ((cdr key+value) client-sock wrapped-spawn-fiber)))
+ (loop)))))
+ (for-each start-service service-alist)
+ (define config (hash->configuration config-hash))
+ (call-with-values
+ (lambda () (proc config wrapped-spawn-fiber))
+ (lambda results
+ ;; Make sure exceptions are visible
+ (hash-for-each (lambda (key value)
+ (match value
+ (('exception . e)
+ (raise-exception e))
+ ('running (values))))
+ %thread-table)
+ (apply values results))))))
+
+(define (call-with-services/fibers service-alist proc)
+ (fibers:run-fibers (lambda () (call-with-services service-alist proc))))
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [gnunet-scheme] 187/324: doc: Document the ‘network size estimation’ API., (continued)
- [gnunet-scheme] 187/324: doc: Document the ‘network size estimation’ API., gnunet, 2021/09/21
- [gnunet-scheme] 197/324: mq-impl/stream: Stop all fibers when EOF is reached (part 2)., gnunet, 2021/09/21
- [gnunet-scheme] 165/324: mq-impl/stream: Implement connecting to unix sockets., gnunet, 2021/09/21
- [gnunet-scheme] 171/324: util/struct: Define /time-absolute., gnunet, 2021/09/21
- [gnunet-scheme] 168/324: README: Remove paragraph about avoiding callbacks., gnunet, 2021/09/21
- [gnunet-scheme] 175/324: mq/handler: Handle the case where no handler exists., gnunet, 2021/09/21
- [gnunet-scheme] 178/324: Makefile.am: Compile with more optimisations., gnunet, 2021/09/21
- [gnunet-scheme] 174/324: nse/struct: Add missing imports., gnunet, 2021/09/21
- [gnunet-scheme] 186/324: nse: Allow 'updated' to be absent., gnunet, 2021/09/21
- [gnunet-scheme] 166/324: guix: Use fixed version of guile., gnunet, 2021/09/21
- [gnunet-scheme] 176/324: tests/utils: New utilities for tests.,
gnunet <=
- [gnunet-scheme] 172/324: crypto/struct: Define /ecc-signature-purpose., gnunet, 2021/09/21
- [gnunet-scheme] 179/324: nse/struct: Document 'timestamp' field of estimates., gnunet, 2021/09/21
- [gnunet-scheme] 192/324: tests/mq-stream: Recognise the 'input:regular-end-of-file' error., gnunet, 2021/09/21
- [gnunet-scheme] 206/324: mq-impl/stream: Flush the output port regularily., gnunet, 2021/09/21
- [gnunet-scheme] 198/324: doc: Document dependencies and how to get the source code., gnunet, 2021/09/21
- [gnunet-scheme] 202/324: mq-impl/stream: Allow the write fiber to stop even if blocking., gnunet, 2021/09/21
- [gnunet-scheme] 205/324: tests/mq-stream: Unbreak SIGPIPE signal handler., gnunet, 2021/09/21
- [gnunet-scheme] 203/324: tests/mq-stream: Make tests less fragile., gnunet, 2021/09/21
- [gnunet-scheme] 193/324: mq-impl/stream: Eliminate condition variable., gnunet, 2021/09/21
- [gnunet-scheme] 207/324: hat-let: Allow (dotted) variable lists with <--., gnunet, 2021/09/21