1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
4 ;;; This library is free software; you can redistribute it and/or
5 ;;; modify it under the terms of the GNU Lesser General Public
6 ;;; License as published by the Free Software Foundation; either
7 ;;; version 3 of the License, or (at your option) any later version.
9 ;;; This library is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;;; Lesser General Public License for more details.
14 ;;; You should have received a copy of the GNU Lesser General Public
15 ;;; License along with this library; if not, write to the Free Software
16 ;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 (define-module (tests test-agenda)
20 #:use-module (srfi srfi-64)
21 #:use-module (ice-9 q)
22 #:use-module (ice-9 match)
23 #:use-module (ice-9 receive)
24 #:use-module (8sync agenda)
25 #:use-module (tests utils))
27 (test-begin "test-agenda")
36 (lambda* (#:optional message)
37 (if message (set! messages (append messages (list message))))
44 (define test-q (list->q '(1 2 3)))
45 (test-equal (deq! test-q) 1)
46 (test-equal (deq! test-q) 2)
47 (test-equal (deq! test-q) 3)
48 (test-assert (q-empty? test-q))
50 (define test-q (make-q* 'apple 'banana 'carrot))
51 (test-equal (deq! test-q) 'apple)
52 (test-equal (deq! test-q) 'banana)
53 (test-equal (deq! test-q) 'carrot)
54 (test-assert (q-empty? test-q))
61 (test-assert (time= '(1 . 1) '(1 . 1)))
62 (test-assert (not (time= '(1 . 1) '(1 . 0))))
63 (test-assert (not (time= '(0 . 1) '(1 . 1))))
65 (test-assert (time< '(1 . 1) '(1 . 2)))
66 (test-assert (time< '(7 . 2) '(8 . 2)))
67 (test-assert (not (time< '(7 . 2) '(7 . 2))))
68 (test-assert (not (time< '(7 . 8) '(7 . 2))))
69 (test-assert (not (time< '(8 . 2) '(7 . 2))))
71 (let ((tdelta (make-time-delta 8)))
72 (test-assert (time-delta? tdelta))
73 (test-eqv (time-delta-sec tdelta) 8)
74 (test-eqv (time-delta-usec tdelta) 0)
76 (time-delta+ '(2 . 3) tdelta)
79 (let ((tdelta (make-time-delta '(10 . 1))))
80 (test-assert (time-delta? tdelta))
81 (test-eqv (time-delta-sec tdelta) 10)
82 (test-eqv (time-delta-usec tdelta) 1)
84 (time-delta+ '(2 . 3) tdelta)
87 (test-equal (time-minus '(100 . 100) '(50 . 66))
89 (test-equal (time-minus '(2 . 0) '(0 . 1))
92 (test-equal (time-plus '(50 . 34) '(50 . 66))
94 (test-equal (time-plus '(1 . 999999) '(1 . 2))
103 (define (assert-times-expected time-segments expected-times)
104 (test-equal (map time-segment-time time-segments)
107 (define a-proc (const 'a))
108 (define b-proc (const 'b))
109 (define c-proc (const 'c))
110 (define d-proc (const 'd))
111 (define e-proc (const 'e))
112 (define f-proc (const 'f))
114 (define sched (make-schedule))
115 (test-assert (schedule-empty? sched))
117 ;; Add a segment at (10 . 0)
118 (schedule-add! sched 10 a-proc)
119 (test-assert (not (schedule-empty? sched)))
120 (test-equal (length (schedule-segments sched)) 1)
121 (test-equal (time-segment-time (car (schedule-segments sched)))
123 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
125 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
127 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
129 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
131 (assert-times-expected (schedule-segments sched)
134 ;; Add another segment at (10 . 0)
135 (schedule-add! sched '(10 . 0) b-proc)
136 (test-assert (not (schedule-empty? sched)))
137 (test-equal (length (schedule-segments sched)) 1)
138 (test-equal (time-segment-time (car (schedule-segments sched)))
140 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
142 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
144 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
146 (assert-times-expected (schedule-segments sched)
149 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
150 (schedule-add! sched 11 c-proc)
151 (schedule-add! sched '(8 . 1) d-proc)
152 (schedule-add! sched '(10 . 10) e-proc)
153 (test-assert (not (schedule-empty? sched)))
154 (test-equal (length (schedule-segments sched)) 4)
155 (assert-times-expected (schedule-segments sched)
156 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
159 (define (test-split-at schedule time expected-before expected-after)
160 (receive (segments-before segments-after)
161 (schedule-segments-split schedule time)
162 (assert-times-expected segments-before expected-before)
163 (assert-times-expected segments-after expected-after)))
165 (test-split-at sched 0
167 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
168 (test-split-at sched '(8 . 0)
170 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
171 (test-split-at sched '(8 . 1)
173 '((10 . 0) (10 . 10) (11 . 0)))
174 (test-split-at sched 9
176 '((10 . 0) (10 . 10) (11 . 0)))
177 (test-split-at sched 10
179 '((10 . 10) (11 . 0)))
180 (test-split-at sched 9000
181 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
183 (test-split-at sched '(9000 . 1) ; over nine thousaaaaaaand
184 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
187 ;; Break off half of those and do some tests on them
188 (define some-extracted
189 (schedule-extract-until! sched 10))
190 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
191 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
192 (define first-extracted-queue
193 (time-segment-queue (car some-extracted)))
194 (define second-extracted-queue
195 (time-segment-queue (cadr some-extracted)))
196 (test-assert (not (q-empty? first-extracted-queue)))
197 (test-equal ((deq! first-extracted-queue)) 'd)
198 (test-assert (q-empty? first-extracted-queue))
200 (test-assert (not (q-empty? second-extracted-queue)))
201 (test-equal ((deq! second-extracted-queue)) 'a)
202 (test-equal ((deq! second-extracted-queue)) 'b)
203 (test-assert (q-empty? second-extracted-queue))
205 ;; Add one more and test flattening to a queue
206 (test-assert (not (schedule-empty? sched)))
207 (schedule-add! sched '(10 . 10) f-proc)
208 (define remaining-segments
209 (schedule-extract-until! sched '(9000 . 1)))
210 (test-assert (schedule-empty? sched))
211 (define some-queue (make-q))
212 (enq! some-queue (const 'ho-ho))
213 (enq! some-queue (const 'ha-ha))
214 (add-segments-contents-to-queue! remaining-segments some-queue)
215 (test-assert (not (q-empty? some-queue)))
216 (test-equal 'ho-ho ((deq! some-queue)))
217 (test-equal 'ha-ha ((deq! some-queue)))
218 (test-equal 'e ((deq! some-queue)))
219 (test-equal 'f ((deq! some-queue)))
220 (test-equal 'c ((deq! some-queue)))
221 (test-assert (q-empty? some-queue))
225 ;;; Run/wrap request stuff
226 ;;; ======================
228 (let ((wrapped (wrap (+ 1 2))))
229 (test-assert (procedure? wrapped))
230 (test-equal (wrapped) 3))
232 (let ((run-two-squared (run-it (lambda () (* 2 2)))))
233 (test-assert (run-request? run-two-squared))
234 (test-assert (procedure? (run-request-proc run-two-squared)))
235 (test-equal ((run-request-proc run-two-squared)) 4)
236 (test-eq (run-request-when run-two-squared) #f))
238 (let ((run-two-squared (run-it (lambda () (* 2 2)) '(88 . 0))))
239 (test-assert (run-request? run-two-squared))
240 (test-assert (procedure? (run-request-proc run-two-squared)))
241 (test-equal ((run-request-proc run-two-squared)) 4)
242 (test-equal (run-request-when run-two-squared) '(88 . 0)))
244 (let ((run-two-squared (run (* 2 2))))
245 (test-assert (run-request? run-two-squared))
246 (test-assert (procedure? (run-request-proc run-two-squared)))
247 (test-equal ((run-request-proc run-two-squared)) 4)
248 (test-eq (run-request-when run-two-squared) #f))
250 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
251 (test-assert (run-request? run-two-squared))
252 (test-assert (procedure? (run-request-proc run-two-squared)))
253 (test-equal ((run-request-proc run-two-squared)) 4)
254 (test-equal (run-request-when run-two-squared) '(88 . 0)))
257 ;;; %run, 8sync and friends tests
258 ;;; ==============================
260 (define-syntax-rule (run-in-fake-agenda
262 (let ((agenda (make-agenda)))
263 (parameterize ((%current-agenda agenda))
265 (agenda-prompt-tag agenda)
267 (list '*normal-result* code-to-run))
268 (lambda (kont async-request)
271 ((@@ (8sync agenda) setup-async-request)
272 kont async-request)))))))
274 (define (test-%run-and-friends run-result expected-when)
276 (('*caught-kont* kont async-request setup-request)
277 (let* ((fake-kont (speak-it))
278 (run-request ((@@ (8sync agenda) setup-async-request)
279 fake-kont async-request)))
280 (test-equal (car async-request) '*async-request*)
281 (test-equal (run-request-when run-request) expected-when)
282 ;; we're using speaker as a fake continuation ;p
283 ((run-request-proc run-request))
284 (test-equal (fake-kont)
287 (test-%run-and-friends (run-in-fake-agenda
288 (8sync (string-concatenate '("apple" "sauce"))))
291 (test-%run-and-friends (run-in-fake-agenda
292 (8sync (string-concatenate '("apple" "sauce"))
296 (test-%run-and-friends (run-in-fake-agenda
297 (8sync-delay (string-concatenate '("apple" "sauce"))
299 ;; whoa, I'm surprised equal? can
300 ;; compare records like this
303 ;; TODO: test %port-request
304 ;; TODO: test 8sync and friends!
312 (define (true-after-n-times n)
315 (set! count (+ count 1))
316 (if (>= count n) #t #f))))
320 (define speaker (speak-it))
323 (speaker "I'm a dummy\n"))
326 (speaker "I bet I can make you say you're a dummy!\n")
330 (set! speaker (speak-it)) ; reset the speaker
331 (start-agenda (make-agenda #:queue (make-q* run-dummy))
332 #:stop-condition (true-after-n-times 2))
333 (test-equal (speaker)
334 '("I bet I can make you say you're a dummy!\n"
337 ;; should only do the first one after one round though
339 (set! speaker (speak-it)) ; reset the speaker
340 (start-agenda (make-agenda #:queue (make-q* run-dummy))
341 #:stop-condition (true-after-n-times 1))
342 (test-equal (speaker)
343 '("I bet I can make you say you're a dummy!\n")))
345 ;; delimited continuation tests
347 (define (return-monkey)
348 (speaker "(Hint, it's a monkey...)\n")
351 (define (talk-about-the-zoo)
352 (speaker "Today I went to the zoo and I saw...\n")
355 `("A " ,(symbol->string (8sync (return-monkey))) "!\n"))))
358 (set! speaker (speak-it))
359 ;; (enq! q talk-about-the-zoo-but-wait)
360 (start-agenda (make-agenda #:queue (make-q* talk-about-the-zoo))
361 #:stop-condition (true-after-n-times 10))
362 (test-equal (speaker)
363 '("Today I went to the zoo and I saw...\n"
364 "(Hint, it's a monkey...)\n"
368 ;; Error handling tests
369 ;; --------------------
371 (define (remote-func-breaks)
372 (speaker "Here we go...\n")
374 (speaker "SHOULD NOT HAPPEN\n"))
376 (define (indirection-remote-func-breaks)
378 (8sync (remote-func-breaks))
381 (define* (local-func-gets-break #:key with-indirection)
382 (speaker "Time for exception fun!\n")
383 (let ((caught-exception #f))
385 (8sync-run (if with-indirection
386 (indirection-remote-func-breaks)
387 (remote-func-breaks)))
389 (lambda (orig-stacks . orig-args)
390 (set! caught-exception #t)
391 (speaker "in here now!\n")
392 (test-equal orig-args '("/" "Numerical overflow" #f #f))
393 (test-assert (list? orig-stacks))
394 (test-equal (length orig-stacks)
395 (if with-indirection 2 1))
398 (test-assert (stack? x)))
400 (test-assert caught-exception))
401 (speaker "Well that was fun :)\n"))
405 (set! speaker (speak-it))
406 (start-agenda (make-agenda #:queue (make-q* local-func-gets-break))
407 #:stop-condition (true-after-n-times 10))
408 (test-equal (speaker)
409 '("Time for exception fun!\n"
412 "Well that was fun :)\n")))
415 (set! speaker (speak-it))
416 (start-agenda (make-agenda
417 #:queue (make-q* (wrap (local-func-gets-break
418 #:with-indirection #t))))
419 #:stop-condition (true-after-n-times 10))
420 (test-equal (speaker)
421 '("Time for exception fun!\n"
425 "Well that was fun :)\n")))
427 ;; Make sure catching tools work
429 (let ((speaker (speak-it))
434 (throw '8sync-caught-error
435 'my-orig-key '(apple orange banana) '(*fake-stack* *fake-stack* *fake-stack*))
436 (speaker "no goodbyes"))
438 (lambda (stacks . rest)
439 (speaker "should not happen")))
441 (lambda (stacks fruit1 fruit2 fruit3)
445 (fruit3 ,fruit3))))))
446 (test-equal (speaker) '("hello"))
447 (test-equal catch-result '((fruit1 apple)
453 (test-end "test-agenda")
455 ;; @@: A better way to handle this at the repl?