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 receive)
27 #:use-module (eightsync agenda)
28 #:use-module (tests utils))
30 (test-begin "test-agenda")
39 (lambda* (#:optional message)
40 (if message (set! messages (append messages (list message))))
47 (define test-q (list->q '(1 2 3)))
48 (test-equal (deq! test-q) 1)
49 (test-equal (deq! test-q) 2)
50 (test-equal (deq! test-q) 3)
51 (test-assert (q-empty? test-q))
53 (define test-q (make-q* 'apple 'banana 'carrot))
54 (test-equal (deq! test-q) 'apple)
55 (test-equal (deq! test-q) 'banana)
56 (test-equal (deq! test-q) 'carrot)
57 (test-assert (q-empty? test-q))
64 (test-assert (time= '(1 . 1) '(1 . 1)))
65 (test-assert (not (time= '(1 . 1) '(1 . 0))))
66 (test-assert (not (time= '(0 . 1) '(1 . 1))))
68 (test-assert (time< '(1 . 1) '(1 . 2)))
69 (test-assert (time< '(7 . 2) '(8 . 2)))
70 (test-assert (not (time< '(7 . 2) '(7 . 2))))
71 (test-assert (not (time< '(7 . 8) '(7 . 2))))
72 (test-assert (not (time< '(8 . 2) '(7 . 2))))
74 (let ((tdelta (make-time-delta 8)))
75 (test-assert (time-delta? tdelta))
76 (test-eqv (time-delta-sec tdelta) 8)
77 (test-eqv (time-delta-usec tdelta) 0)
79 (time-delta+ '(2 . 3) tdelta)
82 (let ((tdelta (make-time-delta '(10 . 1))))
83 (test-assert (time-delta? tdelta))
84 (test-eqv (time-delta-sec tdelta) 10)
85 (test-eqv (time-delta-usec tdelta) 1)
87 (time-delta+ '(2 . 3) tdelta)
90 (test-equal (time-minus '(100 . 100) '(50 . 66))
92 (test-equal (time-minus '(2 . 0) '(0 . 1))
95 (test-equal (time-plus '(50 . 34) '(50 . 66))
97 (test-equal (time-plus '(1 . 999999) '(1 . 2))
106 (define (assert-times-expected time-segments expected-times)
107 (test-equal (map time-segment-time time-segments)
110 (define a-proc (const 'a))
111 (define b-proc (const 'b))
112 (define c-proc (const 'c))
113 (define d-proc (const 'd))
114 (define e-proc (const 'e))
115 (define f-proc (const 'f))
117 (define sched (make-schedule))
118 (test-assert (schedule-empty? sched))
120 ;; Add a segment at (10 . 0)
121 (schedule-add! sched 10 a-proc)
122 (test-assert (not (schedule-empty? sched)))
123 (test-equal (length (schedule-segments sched)) 1)
124 (test-equal (time-segment-time (car (schedule-segments sched)))
126 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
128 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
130 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
132 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
134 (assert-times-expected (schedule-segments sched)
137 ;; Add another segment at (10 . 0)
138 (schedule-add! sched '(10 . 0) b-proc)
139 (test-assert (not (schedule-empty? sched)))
140 (test-equal (length (schedule-segments sched)) 1)
141 (test-equal (time-segment-time (car (schedule-segments sched)))
143 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
145 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
147 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
149 (assert-times-expected (schedule-segments sched)
152 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
153 (schedule-add! sched 11 c-proc)
154 (schedule-add! sched '(8 . 1) d-proc)
155 (schedule-add! sched '(10 . 10) e-proc)
156 (test-assert (not (schedule-empty? sched)))
157 (test-equal (length (schedule-segments sched)) 4)
158 (assert-times-expected (schedule-segments sched)
159 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
162 (define (test-split-at schedule time expected-before expected-after)
163 (receive (segments-before segments-after)
164 (schedule-segments-split schedule time)
165 (assert-times-expected segments-before expected-before)
166 (assert-times-expected segments-after expected-after)))
168 (test-split-at sched 0
170 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
171 (test-split-at sched '(8 . 0)
173 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
174 (test-split-at sched '(8 . 1)
176 '((10 . 0) (10 . 10) (11 . 0)))
177 (test-split-at sched 9
179 '((10 . 0) (10 . 10) (11 . 0)))
180 (test-split-at sched 10
182 '((10 . 10) (11 . 0)))
183 (test-split-at sched 9000
184 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
186 (test-split-at sched '(9000 . 1) ; over nine thousaaaaaaand
187 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
190 ;; Break off half of those and do some tests on them
191 (define some-extracted
192 (schedule-extract-until! sched 10))
193 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
194 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
195 (define first-extracted-queue
196 (time-segment-queue (car some-extracted)))
197 (define second-extracted-queue
198 (time-segment-queue (cadr some-extracted)))
199 (test-assert (not (q-empty? first-extracted-queue)))
200 (test-equal ((deq! first-extracted-queue)) 'd)
201 (test-assert (q-empty? first-extracted-queue))
203 (test-assert (not (q-empty? second-extracted-queue)))
204 (test-equal ((deq! second-extracted-queue)) 'a)
205 (test-equal ((deq! second-extracted-queue)) 'b)
206 (test-assert (q-empty? second-extracted-queue))
208 ;; Add one more and test flattening to a queue
209 (test-assert (not (schedule-empty? sched)))
210 (schedule-add! sched '(10 . 10) f-proc)
211 (define remaining-segments
212 (schedule-extract-until! sched '(9000 . 1)))
213 (test-assert (schedule-empty? sched))
214 (define some-queue (make-q))
215 (enq! some-queue (const 'ho-ho))
216 (enq! some-queue (const 'ha-ha))
217 (add-segments-contents-to-queue! remaining-segments some-queue)
218 (test-assert (not (q-empty? some-queue)))
219 (test-equal 'ho-ho ((deq! some-queue)))
220 (test-equal 'ha-ha ((deq! some-queue)))
221 (test-equal 'e ((deq! some-queue)))
222 (test-equal 'f ((deq! some-queue)))
223 (test-equal 'c ((deq! some-queue)))
224 (test-assert (q-empty? some-queue))
228 ;;; Run/wrap request stuff
229 ;;; ======================
231 (let ((wrapped (wrap (+ 1 2))))
232 (test-assert (procedure? wrapped))
233 (test-equal (wrapped) 3))
235 (let ((run-two-squared (run-it (lambda () (* 2 2)))))
236 (test-assert (run-request? run-two-squared))
237 (test-assert (procedure? (run-request-proc run-two-squared)))
238 (test-equal ((run-request-proc run-two-squared)) 4)
239 (test-eq (run-request-when run-two-squared) #f))
241 (let ((run-two-squared (run-it (lambda () (* 2 2)) '(88 . 0))))
242 (test-assert (run-request? run-two-squared))
243 (test-assert (procedure? (run-request-proc run-two-squared)))
244 (test-equal ((run-request-proc run-two-squared)) 4)
245 (test-equal (run-request-when run-two-squared) '(88 . 0)))
247 (let ((run-two-squared (run (* 2 2))))
248 (test-assert (run-request? run-two-squared))
249 (test-assert (procedure? (run-request-proc run-two-squared)))
250 (test-equal ((run-request-proc run-two-squared)) 4)
251 (test-eq (run-request-when run-two-squared) #f))
253 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
254 (test-assert (run-request? run-two-squared))
255 (test-assert (procedure? (run-request-proc run-two-squared)))
256 (test-equal ((run-request-proc run-two-squared)) 4)
257 (test-equal (run-request-when run-two-squared) '(88 . 0)))
260 ;;; %run, %8sync and friends tests
261 ;;; ==============================
263 (define (test-%run-and-friends async-request expected-when)
264 (let* ((fake-kont (speak-it))
265 (run-request ((@@ (eightsync agenda) setup-async-request)
266 fake-kont async-request)))
267 (test-equal (car async-request) '*async-request*)
268 (test-equal (run-request-when run-request) expected-when)
269 ;; we're using speaker as a fake continuation ;p
270 ((run-request-proc run-request))
271 (test-equal (fake-kont)
274 (test-%run-and-friends (%run (string-concatenate '("apple" "sauce")))
277 (test-%run-and-friends (%run-at (string-concatenate '("apple" "sauce"))
281 (test-%run-and-friends (%run-delay (string-concatenate '("apple" "sauce"))
283 ;; whoa, I'm surprised equal? can
284 ;; compare records like this
287 ;; TODO: test %port-request
288 ;; TODO: test %8sync and friends!
296 (define (true-after-n-times n)
299 (set! count (+ count 1))
300 (if (>= count n) #t #f))))
304 (define speaker (speak-it))
307 (speaker "I'm a dummy\n"))
310 (speaker "I bet I can make you say you're a dummy!\n")
314 (set! speaker (speak-it)) ; reset the speaker
316 (start-agenda (make-agenda #:queue q)
317 #:stop-condition (true-after-n-times 2))
318 (test-equal (speaker)
319 '("I bet I can make you say you're a dummy!\n"
322 ;; should only do the first one after one round though
324 (set! speaker (speak-it)) ; reset the speaker
326 (start-agenda (make-agenda #:queue q)
327 #:stop-condition (true-after-n-times 1))
328 (test-equal (speaker)
329 '("I bet I can make you say you're a dummy!\n")))
331 ;; delimited continuation tests
333 (define (return-monkey)
334 (speaker "(Hint, it's a monkey...)\n")
337 (define (talk-about-the-zoo)
338 (speaker "Today I went to the zoo and I saw...\n")
341 `("A " ,(symbol->string (%8sync (%run (return-monkey)))) "!\n"))))
344 (set! speaker (speak-it))
345 (enq! q talk-about-the-zoo)
346 ;; (enq! q talk-about-the-zoo-but-wait)
347 (start-agenda (make-agenda #:queue q)
348 #:stop-condition (true-after-n-times 10))
349 (test-equal (speaker)
350 '("Today I went to the zoo and I saw...\n"
351 "(Hint, it's a monkey...)\n"
355 ;; Error handling tests
356 ;; --------------------
358 (define (remote-func-breaks)
359 (speaker "Here we go...\n")
361 (speaker "SHOULD NOT HAPPEN\n"))
363 (define (indirection-remote-func-breaks)
365 (%8sync (%run (remote-func-breaks)))
368 (define* (local-func-gets-break #:key with-indirection)
369 (speaker "Time for exception fun!\n")
370 (let ((caught-exception #f))
372 (%8sync (%run (if with-indirection
373 (indirection-remote-func-breaks)
374 (remote-func-breaks))))
376 (lambda (orig-stacks . orig-args)
377 (set! caught-exception #t)
378 (speaker "in here now!\n")
379 (test-equal orig-args '("/" "Numerical overflow" #f #f))
380 (test-assert (list? orig-stacks))
381 (test-equal (length orig-stacks)
382 (if with-indirection 2 1))
385 (test-assert (stack? x)))
387 (test-assert caught-exception))
388 (speaker "Well that was fun :)\n"))
392 (set! speaker (speak-it))
393 (enq! q local-func-gets-break)
394 (start-agenda (make-agenda #:queue q)
395 #:stop-condition (true-after-n-times 10))
396 (test-assert (speaker)
397 '("Time for exception fun!\n"
400 "Well that was fun :)\n")))
403 (set! speaker (speak-it))
404 (enq! q (wrap (local-func-gets-break #:with-indirection #t)))
405 (start-agenda (make-agenda #:queue q)
406 #:stop-condition (true-after-n-times 10))
407 (test-assert (speaker)
408 '("Time for exception fun!\n"
412 "Well that was fun :)\n")))
414 ;; Make sure catching tools work
416 (let ((speaker (speak-it))
421 (throw '8sync-caught-error
422 'my-orig-key '(apple orange banana) '(*fake-stack* *fake-stack* *fake-stack*))
423 (speaker "no goodbyes"))
425 (lambda (stacks . rest)
426 (speaker "should not happen")))
428 (lambda (stacks fruit1 fruit2 fruit3)
432 (fruit3 ,fruit3))))))
433 (test-equal (speaker) '("hello"))
434 (test-equal catch-result '((fruit1 apple)
440 (test-end "test-agenda")
442 ;; @@: A better way to handle this at the repl?