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
23 (define-module (tests test-agenda)
24 #:use-module (srfi srfi-64)
25 #:use-module (ice-9 q)
26 #:use-module (ice-9 match)
27 #:use-module (ice-9 receive)
28 #:use-module (8sync agenda)
29 #:use-module (tests utils))
31 (test-begin "test-agenda")
40 (lambda* (#:optional message)
41 (if message (set! messages (append messages (list message))))
48 (define test-q (list->q '(1 2 3)))
49 (test-equal (deq! test-q) 1)
50 (test-equal (deq! test-q) 2)
51 (test-equal (deq! test-q) 3)
52 (test-assert (q-empty? test-q))
54 (define test-q (make-q* 'apple 'banana 'carrot))
55 (test-equal (deq! test-q) 'apple)
56 (test-equal (deq! test-q) 'banana)
57 (test-equal (deq! test-q) 'carrot)
58 (test-assert (q-empty? test-q))
65 (test-assert (time= '(1 . 1) '(1 . 1)))
66 (test-assert (not (time= '(1 . 1) '(1 . 0))))
67 (test-assert (not (time= '(0 . 1) '(1 . 1))))
69 (test-assert (time< '(1 . 1) '(1 . 2)))
70 (test-assert (time< '(7 . 2) '(8 . 2)))
71 (test-assert (not (time< '(7 . 2) '(7 . 2))))
72 (test-assert (not (time< '(7 . 8) '(7 . 2))))
73 (test-assert (not (time< '(8 . 2) '(7 . 2))))
75 (let ((tdelta (make-time-delta 8)))
76 (test-assert (time-delta? tdelta))
77 (test-eqv (time-delta-sec tdelta) 8)
78 (test-eqv (time-delta-usec tdelta) 0)
80 (time-delta+ '(2 . 3) tdelta)
83 (let ((tdelta (make-time-delta '(10 . 1))))
84 (test-assert (time-delta? tdelta))
85 (test-eqv (time-delta-sec tdelta) 10)
86 (test-eqv (time-delta-usec tdelta) 1)
88 (time-delta+ '(2 . 3) tdelta)
91 (test-equal (time-minus '(100 . 100) '(50 . 66))
93 (test-equal (time-minus '(2 . 0) '(0 . 1))
96 (test-equal (time-plus '(50 . 34) '(50 . 66))
98 (test-equal (time-plus '(1 . 999999) '(1 . 2))
107 (define (assert-times-expected time-segments expected-times)
108 (test-equal (map time-segment-time time-segments)
111 (define a-proc (const 'a))
112 (define b-proc (const 'b))
113 (define c-proc (const 'c))
114 (define d-proc (const 'd))
115 (define e-proc (const 'e))
116 (define f-proc (const 'f))
118 (define sched (make-schedule))
119 (test-assert (schedule-empty? sched))
121 ;; Add a segment at (10 . 0)
122 (schedule-add! sched 10 a-proc)
123 (test-assert (not (schedule-empty? sched)))
124 (test-equal (length (schedule-segments sched)) 1)
125 (test-equal (time-segment-time (car (schedule-segments sched)))
127 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
129 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
131 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
133 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
135 (assert-times-expected (schedule-segments sched)
138 ;; Add another segment at (10 . 0)
139 (schedule-add! sched '(10 . 0) b-proc)
140 (test-assert (not (schedule-empty? sched)))
141 (test-equal (length (schedule-segments sched)) 1)
142 (test-equal (time-segment-time (car (schedule-segments sched)))
144 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
146 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
148 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
150 (assert-times-expected (schedule-segments sched)
153 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
154 (schedule-add! sched 11 c-proc)
155 (schedule-add! sched '(8 . 1) d-proc)
156 (schedule-add! sched '(10 . 10) e-proc)
157 (test-assert (not (schedule-empty? sched)))
158 (test-equal (length (schedule-segments sched)) 4)
159 (assert-times-expected (schedule-segments sched)
160 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
163 (define (test-split-at schedule time expected-before expected-after)
164 (receive (segments-before segments-after)
165 (schedule-segments-split schedule time)
166 (assert-times-expected segments-before expected-before)
167 (assert-times-expected segments-after expected-after)))
169 (test-split-at sched 0
171 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
172 (test-split-at sched '(8 . 0)
174 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
175 (test-split-at sched '(8 . 1)
177 '((10 . 0) (10 . 10) (11 . 0)))
178 (test-split-at sched 9
180 '((10 . 0) (10 . 10) (11 . 0)))
181 (test-split-at sched 10
183 '((10 . 10) (11 . 0)))
184 (test-split-at sched 9000
185 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
187 (test-split-at sched '(9000 . 1) ; over nine thousaaaaaaand
188 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
191 ;; Break off half of those and do some tests on them
192 (define some-extracted
193 (schedule-extract-until! sched 10))
194 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
195 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
196 (define first-extracted-queue
197 (time-segment-queue (car some-extracted)))
198 (define second-extracted-queue
199 (time-segment-queue (cadr some-extracted)))
200 (test-assert (not (q-empty? first-extracted-queue)))
201 (test-equal ((deq! first-extracted-queue)) 'd)
202 (test-assert (q-empty? first-extracted-queue))
204 (test-assert (not (q-empty? second-extracted-queue)))
205 (test-equal ((deq! second-extracted-queue)) 'a)
206 (test-equal ((deq! second-extracted-queue)) 'b)
207 (test-assert (q-empty? second-extracted-queue))
209 ;; Add one more and test flattening to a queue
210 (test-assert (not (schedule-empty? sched)))
211 (schedule-add! sched '(10 . 10) f-proc)
212 (define remaining-segments
213 (schedule-extract-until! sched '(9000 . 1)))
214 (test-assert (schedule-empty? sched))
215 (define some-queue (make-q))
216 (enq! some-queue (const 'ho-ho))
217 (enq! some-queue (const 'ha-ha))
218 (add-segments-contents-to-queue! remaining-segments some-queue)
219 (test-assert (not (q-empty? some-queue)))
220 (test-equal 'ho-ho ((deq! some-queue)))
221 (test-equal 'ha-ha ((deq! some-queue)))
222 (test-equal 'e ((deq! some-queue)))
223 (test-equal 'f ((deq! some-queue)))
224 (test-equal 'c ((deq! some-queue)))
225 (test-assert (q-empty? some-queue))
229 ;;; Run/wrap request stuff
230 ;;; ======================
232 (let ((wrapped (wrap (+ 1 2))))
233 (test-assert (procedure? wrapped))
234 (test-equal (wrapped) 3))
236 (let ((run-two-squared (run-it (lambda () (* 2 2)))))
237 (test-assert (run-request? run-two-squared))
238 (test-assert (procedure? (run-request-proc run-two-squared)))
239 (test-equal ((run-request-proc run-two-squared)) 4)
240 (test-eq (run-request-when run-two-squared) #f))
242 (let ((run-two-squared (run-it (lambda () (* 2 2)) '(88 . 0))))
243 (test-assert (run-request? run-two-squared))
244 (test-assert (procedure? (run-request-proc run-two-squared)))
245 (test-equal ((run-request-proc run-two-squared)) 4)
246 (test-equal (run-request-when run-two-squared) '(88 . 0)))
248 (let ((run-two-squared (run (* 2 2))))
249 (test-assert (run-request? run-two-squared))
250 (test-assert (procedure? (run-request-proc run-two-squared)))
251 (test-equal ((run-request-proc run-two-squared)) 4)
252 (test-eq (run-request-when run-two-squared) #f))
254 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
255 (test-assert (run-request? run-two-squared))
256 (test-assert (procedure? (run-request-proc run-two-squared)))
257 (test-equal ((run-request-proc run-two-squared)) 4)
258 (test-equal (run-request-when run-two-squared) '(88 . 0)))
261 ;;; %run, 8sync and friends tests
262 ;;; ==============================
264 (define-syntax-rule (run-in-fake-agenda
266 (let ((agenda (make-agenda)))
267 (parameterize ((%current-agenda agenda))
269 (agenda-prompt-tag agenda)
271 (list '*normal-result* code-to-run))
272 (lambda (kont async-request)
275 ((@@ (8sync agenda) setup-async-request)
276 kont async-request)))))))
278 (define (test-%run-and-friends run-result expected-when)
280 (('*caught-kont* kont async-request setup-request)
281 (let* ((fake-kont (speak-it))
282 (run-request ((@@ (8sync agenda) setup-async-request)
283 fake-kont async-request)))
284 (test-equal (car async-request) '*async-request*)
285 (test-equal (run-request-when run-request) expected-when)
286 ;; we're using speaker as a fake continuation ;p
287 ((run-request-proc run-request))
288 (test-equal (fake-kont)
291 (test-%run-and-friends (run-in-fake-agenda
292 (8sync (string-concatenate '("apple" "sauce"))))
295 (test-%run-and-friends (run-in-fake-agenda
296 (8sync (string-concatenate '("apple" "sauce"))
300 (test-%run-and-friends (run-in-fake-agenda
301 (8sync-delay (string-concatenate '("apple" "sauce"))
303 ;; whoa, I'm surprised equal? can
304 ;; compare records like this
307 ;; TODO: test %port-request
308 ;; TODO: test 8sync and friends!
316 (define (true-after-n-times n)
319 (set! count (+ count 1))
320 (if (>= count n) #t #f))))
324 (define speaker (speak-it))
327 (speaker "I'm a dummy\n"))
330 (speaker "I bet I can make you say you're a dummy!\n")
334 (set! speaker (speak-it)) ; reset the speaker
335 (start-agenda (make-agenda #:queue (make-q* run-dummy))
336 #:stop-condition (true-after-n-times 2))
337 (test-equal (speaker)
338 '("I bet I can make you say you're a dummy!\n"
341 ;; should only do the first one after one round though
343 (set! speaker (speak-it)) ; reset the speaker
344 (start-agenda (make-agenda #:queue (make-q* run-dummy))
345 #:stop-condition (true-after-n-times 1))
346 (test-equal (speaker)
347 '("I bet I can make you say you're a dummy!\n")))
349 ;; delimited continuation tests
351 (define (return-monkey)
352 (speaker "(Hint, it's a monkey...)\n")
355 (define (talk-about-the-zoo)
356 (speaker "Today I went to the zoo and I saw...\n")
359 `("A " ,(symbol->string (8sync (return-monkey))) "!\n"))))
362 (set! speaker (speak-it))
363 ;; (enq! q talk-about-the-zoo-but-wait)
364 (start-agenda (make-agenda #:queue (make-q* talk-about-the-zoo))
365 #:stop-condition (true-after-n-times 10))
366 (test-equal (speaker)
367 '("Today I went to the zoo and I saw...\n"
368 "(Hint, it's a monkey...)\n"
372 ;; Error handling tests
373 ;; --------------------
375 (define (remote-func-breaks)
376 (speaker "Here we go...\n")
378 (speaker "SHOULD NOT HAPPEN\n"))
380 (define (indirection-remote-func-breaks)
382 (8sync (remote-func-breaks))
385 (define* (local-func-gets-break #:key with-indirection)
386 (speaker "Time for exception fun!\n")
387 (let ((caught-exception #f))
389 (8sync-run (if with-indirection
390 (indirection-remote-func-breaks)
391 (remote-func-breaks)))
393 (lambda (orig-stacks . orig-args)
394 (set! caught-exception #t)
395 (speaker "in here now!\n")
396 (test-equal orig-args '("/" "Numerical overflow" #f #f))
397 (test-assert (list? orig-stacks))
398 (test-equal (length orig-stacks)
399 (if with-indirection 2 1))
402 (test-assert (stack? x)))
404 (test-assert caught-exception))
405 (speaker "Well that was fun :)\n"))
409 (set! speaker (speak-it))
410 (start-agenda (make-agenda #:queue (make-q* local-func-gets-break))
411 #:stop-condition (true-after-n-times 10))
412 (test-equal (speaker)
413 '("Time for exception fun!\n"
416 "Well that was fun :)\n")))
419 (set! speaker (speak-it))
420 (start-agenda (make-agenda
421 #:queue (make-q* (wrap (local-func-gets-break
422 #:with-indirection #t))))
423 #:stop-condition (true-after-n-times 10))
424 (test-equal (speaker)
425 '("Time for exception fun!\n"
429 "Well that was fun :)\n")))
431 ;; Make sure catching tools work
433 (let ((speaker (speak-it))
438 (throw '8sync-caught-error
439 'my-orig-key '(apple orange banana) '(*fake-stack* *fake-stack* *fake-stack*))
440 (speaker "no goodbyes"))
442 (lambda (stacks . rest)
443 (speaker "should not happen")))
445 (lambda (stacks fruit1 fruit2 fruit3)
449 (fruit3 ,fruit3))))))
450 (test-equal (speaker) '("hello"))
451 (test-equal catch-result '((fruit1 apple)
457 (test-end "test-agenda")
459 ;; @@: A better way to handle this at the repl?