aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMaxime Devos <maximedevos@telenet.be>2021-08-13 16:39:15 +0200
committerMaxime Devos <maximedevos@telenet.be>2021-09-21 12:15:26 +0200
commitaeb4d31b176543ac46a219bb560d5344c1a98848 (patch)
treefe8e03dd2d0bd4fa73fa639c01bd2cedbd5346f2 /tests
parent9ef0449481dc02aaa853b09b5af1a7d940c08996 (diff)
downloadgnunet-scheme-aeb4d31b176543ac46a219bb560d5344c1a98848.tar.gz
gnunet-scheme-aeb4d31b176543ac46a219bb560d5344c1a98848.zip
tests/utils: New utilities for tests.
* tests/util.scm (call-with-temporary-directory) (call-with-services) (call-with-services/fibers): New procedure.
Diffstat (limited to 'tests')
-rw-r--r--tests/utils.scm83
1 files changed, 82 insertions, 1 deletions
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 @@
17;; SPDX-License-Identifier: AGPL3.0-or-later 17;; SPDX-License-Identifier: AGPL3.0-or-later
18(define-module (tests utils) 18(define-module (tests utils)
19 #:use-module (srfi srfi-8) 19 #:use-module (srfi srfi-8)
20 #:use-module (ice-9 match)
21 #:use-module ((rnrs hashtables) #:prefix #{rnrs:}#)
22 #:use-module ((rnrs arithmetic bitwise)
23 #:select (bitwise-ior))
20 #:use-module ((rnrs base) #:select (assert)) 24 #:use-module ((rnrs base) #:select (assert))
21 #:export (conservative-gc? calls-in-tail-position?)) 25 #:use-module ((fibers) #:prefix #{fibers:}#)
26 #:autoload (gnu gnunet config db) (hash->configuration)
27 #:export (conservative-gc? calls-in-tail-position?
28 call-with-services
29 call-with-services/fibers))
22 30
23;; Current versions of guile (at least 3.0.5) use a conservative 31;; Current versions of guile (at least 3.0.5) use a conservative
24;; garbage collector, so some tests concerning garbage collection 32;; garbage collector, so some tests concerning garbage collection
@@ -54,3 +62,76 @@ times."
54(assert (not (calls-in-tail-position? (lambda (thunk) (+ 1 (thunk)))))) 62(assert (not (calls-in-tail-position? (lambda (thunk) (+ 1 (thunk))))))
55#; 63#;
56(assert (not (calls-in-tail-position? (lambda (thunk) (for-each thunk '("bla" "bla")))))) 64(assert (not (calls-in-tail-position? (lambda (thunk) (for-each thunk '("bla" "bla"))))))
65
66(define (call-with-temporary-directory proc)
67 (let ((file (mkdtemp (in-vicinity (or (getenv "TMPDIR") "/tmp")
68 "test-XXXXXX"))))
69 (with-exception-handler
70 (lambda (e)
71 (system* "rm" "-r" file)
72 (raise-exception e))
73 (lambda ()
74 (call-with-values
75 (lambda () (proc file))
76 (lambda the-values
77 (system* "rm" "-r" file)
78 (apply values the-values)))))))
79
80(define (call-with-services service-alist proc)
81 "Call the procedure @var{proc} with a configuration database
82and a procedure behaving like @code{spawn-fiber}, in an environment
83where the services listed in @var{service-alist} can
84be connected to. The heads in @var{service-alist} are the names of
85the services and each tails is a list of a procedure accepting ports
86(connected to the client) and the procedure behaving like @code{spawn-fiber}."
87 (define %thread-table (make-hash-table))
88 (define (wrapped-spawn-fiber thunk)
89 (define o (list))
90 (hashq-set! %thread-table o 'running)
91 (fibers:spawn-fiber
92 (lambda ()
93 (with-exception-handler
94 (lambda (e)
95 (hashq-set! %thread-table o (cons 'exception e))
96 (raise-exception e))
97 thunk)))
98 (values))
99 ;; The hash function isn't very efficient but is sufficient.
100 (define config-hash (rnrs:make-hashtable (const 0) equal?))
101 (call-with-temporary-directory
102 (lambda (dir)
103 (define (start-service key+value)
104 (define where (in-vicinity dir (string-append (car key+value) ".sock")))
105 (rnrs:hashtable-set! config-hash (cons (car key+value) "UNIXPATH")
106 where)
107 (wrapped-spawn-fiber
108 (lambda ()
109 (define sock (socket AF_UNIX SOCK_STREAM 0))
110 (bind sock AF_UNIX where)
111 (listen sock 40)
112 (fcntl sock F_SETFL
113 (bitwise-ior (fcntl sock F_GETFL) O_NONBLOCK))
114 (let loop ()
115 (define client-sock
116 (car (accept sock (logior SOCK_NONBLOCK
117 SOCK_CLOEXEC))))
118 (wrapped-spawn-fiber
119 (lambda ()
120 ((cdr key+value) client-sock wrapped-spawn-fiber)))
121 (loop)))))
122 (for-each start-service service-alist)
123 (define config (hash->configuration config-hash))
124 (call-with-values
125 (lambda () (proc config wrapped-spawn-fiber))
126 (lambda results
127 ;; Make sure exceptions are visible
128 (hash-for-each (lambda (key value)
129 (match value
130 (('exception . e)
131 (raise-exception e))
132 ('running (values))))
133 %thread-table)
134 (apply values results))))))
135
136(define (call-with-services/fibers service-alist proc)
137 (fibers:run-fibers (lambda () (call-with-services service-alist proc))))