1 ;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;; This library is free software; you can redistribute it and/or
4 ;; modify it under the terms of the GNU Lesser General Public
5 ;; License as published by the Free Software Foundation; either
6 ;; version 3 of the License, or (at your option) any later version.
8 ;; This library is distributed in the hope that it will be useful,
9 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 ;; Lesser General Public License for more details.
13 ;; You should have received a copy of the GNU Lesser General Public
14 ;; License along with this library; if not, write to the Free Software
15 ;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 (define-module (tests test-agenda)
23 #:use-module (srfi srfi-64)
24 #:use-module (ice-9 q)
25 #:use-module (ice-9 receive)
26 #:use-module (eightsync agenda)
27 #:use-module (tests utils))
29 (test-begin "test-agenda")
38 (lambda* (#:optional message)
39 (if message (set! messages (append messages (list message))))
46 (define test-q (list->q '(1 2 3)))
47 (test-equal (deq! test-q) 1)
48 (test-equal (deq! test-q) 2)
49 (test-equal (deq! test-q) 3)
50 (test-assert (q-empty? test-q))
52 (define test-q (make-q* 'apple 'banana 'carrot))
53 (test-equal (deq! test-q) 'apple)
54 (test-equal (deq! test-q) 'banana)
55 (test-equal (deq! test-q) 'carrot)
56 (test-assert (q-empty? test-q))
63 (test-assert (time= '(1 . 1) '(1 . 1)))
64 (test-assert (not (time= '(1 . 1) '(1 . 0))))
65 (test-assert (not (time= '(0 . 1) '(1 . 1))))
67 (test-assert (time< '(1 . 1) '(1 . 2)))
68 (test-assert (time< '(7 . 2) '(8 . 2)))
69 (test-assert (not (time< '(7 . 2) '(7 . 2))))
70 (test-assert (not (time< '(7 . 8) '(7 . 2))))
71 (test-assert (not (time< '(8 . 2) '(7 . 2))))
73 (let ((tdelta (make-time-delta 8)))
74 (test-assert (time-delta? tdelta))
75 (test-eqv (time-delta-sec tdelta) 8)
76 (test-eqv (time-delta-usec tdelta) 0)
78 (time-delta+ '(2 . 3) tdelta)
81 (let ((tdelta (make-time-delta '(10 . 1))))
82 (test-assert (time-delta? tdelta))
83 (test-eqv (time-delta-sec tdelta) 10)
84 (test-eqv (time-delta-usec tdelta) 1)
86 (time-delta+ '(2 . 3) tdelta)
89 (test-equal (time-minus '(100 . 100) '(50 . 66))
91 (test-equal (time-minus '(2 . 0) '(0 . 1))
94 (test-equal (time-plus '(50 . 34) '(50 . 66))
96 (test-equal (time-plus '(1 . 999999) '(1 . 2))
105 (define (assert-times-expected time-segments expected-times)
106 (test-equal (map time-segment-time time-segments)
109 (define a-proc (const 'a))
110 (define b-proc (const 'b))
111 (define c-proc (const 'c))
112 (define d-proc (const 'd))
113 (define e-proc (const 'e))
114 (define f-proc (const 'f))
116 (define sched (make-schedule))
117 (test-assert (schedule-empty? sched))
119 ;; Add a segment at (10 . 0)
120 (schedule-add! sched 10 a-proc)
121 (test-assert (not (schedule-empty? sched)))
122 (test-equal (length (schedule-segments sched)) 1)
123 (test-equal (time-segment-time (car (schedule-segments sched)))
125 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
127 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
129 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
131 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
133 (assert-times-expected (schedule-segments sched)
136 ;; Add another segment at (10 . 0)
137 (schedule-add! sched '(10 . 0) b-proc)
138 (test-assert (not (schedule-empty? sched)))
139 (test-equal (length (schedule-segments sched)) 1)
140 (test-equal (time-segment-time (car (schedule-segments sched)))
142 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
144 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
146 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
148 (assert-times-expected (schedule-segments sched)
151 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
152 (schedule-add! sched 11 c-proc)
153 (schedule-add! sched '(8 . 1) d-proc)
154 (schedule-add! sched '(10 . 10) e-proc)
155 (test-assert (not (schedule-empty? sched)))
156 (test-equal (length (schedule-segments sched)) 4)
157 (assert-times-expected (schedule-segments sched)
158 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
161 (define (test-split-at schedule time expected-before expected-after)
162 (receive (segments-before segments-after)
163 (schedule-segments-split schedule time)
164 (assert-times-expected segments-before expected-before)
165 (assert-times-expected segments-after expected-after)))
167 (test-split-at sched 0
169 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
170 (test-split-at sched '(8 . 0)
172 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
173 (test-split-at sched '(8 . 1)
175 '((10 . 0) (10 . 10) (11 . 0)))
176 (test-split-at sched 9
178 '((10 . 0) (10 . 10) (11 . 0)))
179 (test-split-at sched 10
181 '((10 . 10) (11 . 0)))
182 (test-split-at sched 9000
183 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
185 (test-split-at sched '(9000 . 1) ; over nine thousaaaaaaand
186 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
189 ;; Break off half of those and do some tests on them
190 (define some-extracted
191 (schedule-extract-until! sched 10))
192 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
193 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
194 (define first-extracted-queue
195 (time-segment-queue (car some-extracted)))
196 (define second-extracted-queue
197 (time-segment-queue (cadr some-extracted)))
198 (test-assert (not (q-empty? first-extracted-queue)))
199 (test-equal ((deq! first-extracted-queue)) 'd)
200 (test-assert (q-empty? first-extracted-queue))
202 (test-assert (not (q-empty? second-extracted-queue)))
203 (test-equal ((deq! second-extracted-queue)) 'a)
204 (test-equal ((deq! second-extracted-queue)) 'b)
205 (test-assert (q-empty? second-extracted-queue))
207 ;; Add one more and test flattening to a queue
208 (test-assert (not (schedule-empty? sched)))
209 (schedule-add! sched '(10 . 10) f-proc)
210 (define remaining-segments
211 (schedule-extract-until! sched '(9000 . 1)))
212 (test-assert (schedule-empty? sched))
213 (define some-queue (make-q))
214 (enq! some-queue (const 'ho-ho))
215 (enq! some-queue (const 'ha-ha))
216 (add-segments-contents-to-queue! remaining-segments some-queue)
217 (test-assert (not (q-empty? some-queue)))
218 (test-equal 'ho-ho ((deq! some-queue)))
219 (test-equal 'ha-ha ((deq! some-queue)))
220 (test-equal 'e ((deq! some-queue)))
221 (test-equal 'f ((deq! some-queue)))
222 (test-equal 'c ((deq! some-queue)))
223 (test-assert (q-empty? some-queue))
227 ;;; Run/wrap request stuff
228 ;;; ======================
230 (let ((wrapped (wrap (+ 1 2))))
231 (test-assert (procedure? wrapped))
232 (test-equal (wrapped) 3))
234 (let ((run-two-squared (run-it (lambda () (* 2 2)))))
235 (test-assert (run-request? run-two-squared))
236 (test-assert (procedure? (run-request-proc run-two-squared)))
237 (test-equal ((run-request-proc run-two-squared)) 4)
238 (test-eq (run-request-when run-two-squared) #f))
240 (let ((run-two-squared (run-it (lambda () (* 2 2)) '(88 . 0))))
241 (test-assert (run-request? run-two-squared))
242 (test-assert (procedure? (run-request-proc run-two-squared)))
243 (test-equal ((run-request-proc run-two-squared)) 4)
244 (test-equal (run-request-when run-two-squared) '(88 . 0)))
246 (let ((run-two-squared (run (* 2 2))))
247 (test-assert (run-request? run-two-squared))
248 (test-assert (procedure? (run-request-proc run-two-squared)))
249 (test-equal ((run-request-proc run-two-squared)) 4)
250 (test-eq (run-request-when run-two-squared) #f))
252 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
253 (test-assert (run-request? run-two-squared))
254 (test-assert (procedure? (run-request-proc run-two-squared)))
255 (test-equal ((run-request-proc run-two-squared)) 4)
256 (test-equal (run-request-when run-two-squared) '(88 . 0)))
259 ;;; %run, %8sync and friends tests
260 ;;; ==============================
262 (define (test-%run-and-friends async-request expected-when)
263 (let* ((fake-kont (speak-it))
264 (run-request ((@@ (eightsync agenda) setup-async-request)
265 fake-kont async-request)))
266 (test-equal (car async-request) '*async-request*)
267 (test-equal (run-request-when run-request) expected-when)
268 ;; we're using speaker as a fake continuation ;p
269 ((run-request-proc run-request))
270 (test-equal (fake-kont)
273 (test-%run-and-friends (%run (string-concatenate '("apple" "sauce")))
276 (test-%run-and-friends (%run-at (string-concatenate '("apple" "sauce"))
280 (test-%run-and-friends (%run-delay (string-concatenate '("apple" "sauce"))
282 ;; whoa, I'm surprised equal? can
283 ;; compare records like this
286 ;; TODO: test %port-request
287 ;; TODO: test %8sync and friends!
295 (define (true-after-n-times n)
298 (set! count (+ count 1))
299 (if (>= count n) #t #f))))
303 (define speaker (speak-it))
306 (speaker "I'm a dummy\n"))
309 (speaker "I bet I can make you say you're a dummy!\n")
313 (set! speaker (speak-it)) ; reset the speaker
315 (start-agenda (make-agenda #:queue q)
316 #:stop-condition (true-after-n-times 2))
317 (test-equal (speaker)
318 '("I bet I can make you say you're a dummy!\n"
321 ;; should only do the first one after one round though
323 (set! speaker (speak-it)) ; reset the speaker
325 (start-agenda (make-agenda #:queue q)
326 #:stop-condition (true-after-n-times 1))
327 (test-equal (speaker)
328 '("I bet I can make you say you're a dummy!\n")))
330 ;; delimited continuation tests
332 (define (return-monkey)
333 (speaker "(Hint, it's a monkey...)\n")
336 (define (talk-about-the-zoo)
337 (speaker "Today I went to the zoo and I saw...\n")
340 `("A " ,(symbol->string (%8sync (%run (return-monkey)))) "!\n"))))
343 (set! speaker (speak-it))
344 (enq! q talk-about-the-zoo)
345 ;; (enq! q talk-about-the-zoo-but-wait)
346 (start-agenda (make-agenda #:queue q)
347 #:stop-condition (true-after-n-times 10))
348 (test-equal (speaker)
349 '("Today I went to the zoo and I saw...\n"
350 "(Hint, it's a monkey...)\n"
354 ;; Error handling tests
355 ;; --------------------
357 (define (remote-func-breaks)
358 (speaker "Here we go...\n")
360 (speaker "SHOULD NOT HAPPEN\n"))
362 (define (indirection-remote-func-breaks)
364 (%8sync (%run (remote-func-breaks)))
367 (define* (local-func-gets-break #:key with-indirection)
368 (speaker "Time for exception fun!\n")
369 (let ((caught-exception #f))
371 (%8sync (%run (if with-indirection
372 (indirection-remote-func-breaks)
373 (remote-func-breaks))))
375 (lambda (orig-stacks . orig-args)
376 (set! caught-exception #t)
377 (speaker "in here now!\n")
378 (test-equal orig-args '("/" "Numerical overflow" #f #f))
379 (test-assert (list? orig-stacks))
380 (test-equal (length orig-stacks)
381 (if with-indirection 2 1))
384 (test-assert (stack? x)))
386 (test-assert caught-exception))
387 (speaker "Well that was fun :)\n"))
391 (set! speaker (speak-it))
392 (enq! q local-func-gets-break)
393 (start-agenda (make-agenda #:queue q)
394 #:stop-condition (true-after-n-times 10))
395 (test-assert (speaker)
396 '("Time for exception fun!\n"
399 "Well that was fun :)\n")))
402 (set! speaker (speak-it))
403 (enq! q (wrap (local-func-gets-break #:with-indirection #t)))
404 (start-agenda (make-agenda #:queue q)
405 #:stop-condition (true-after-n-times 10))
406 (test-assert (speaker)
407 '("Time for exception fun!\n"
411 "Well that was fun :)\n")))
413 ;; Make sure catching tools work
415 (let ((speaker (speak-it))
420 (throw '8sync-caught-error
421 'my-orig-key '(apple orange banana) '(*fake-stack* *fake-stack* *fake-stack*))
422 (speaker "no goodbyes"))
424 (lambda (stacks . rest)
425 (speaker "should not happen")))
427 (lambda (stacks fruit1 fruit2 fruit3)
431 (fruit3 ,fruit3))))))
432 (test-equal (speaker) '("hello"))
433 (test-equal catch-result '((fruit1 apple)
439 (test-end "test-agenda")
441 ;; @@: A better way to handle this at the repl?