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 (test-assert (time= '(1 . 1) '(1 . 1)))
47 (test-assert (not (time= '(1 . 1) '(1 . 0))))
48 (test-assert (not (time= '(0 . 1) '(1 . 1))))
50 (test-assert (time< '(1 . 1) '(1 . 2)))
51 (test-assert (time< '(7 . 2) '(8 . 2)))
52 (test-assert (not (time< '(7 . 2) '(7 . 2))))
53 (test-assert (not (time< '(7 . 8) '(7 . 2))))
54 (test-assert (not (time< '(8 . 2) '(7 . 2))))
56 (let ((tdelta (make-time-delta 8)))
57 (test-assert (time-delta? tdelta))
58 (test-eqv (time-delta-sec tdelta) 8)
59 (test-eqv (time-delta-usec tdelta) 0)
61 (time-delta+ '(2 . 3) tdelta)
64 (let ((tdelta (make-time-delta '(10 . 1))))
65 (test-assert (time-delta? tdelta))
66 (test-eqv (time-delta-sec tdelta) 10)
67 (test-eqv (time-delta-usec tdelta) 1)
69 (time-delta+ '(2 . 3) tdelta)
72 (test-equal (time-minus '(100 . 100) '(50 . 66))
74 (test-equal (time-minus '(2 . 0) '(0 . 1))
77 (test-equal (time-plus '(50 . 34) '(50 . 66))
79 (test-equal (time-plus '(1 . 999999) '(1 . 2))
88 (define (assert-times-expected time-segments expected-times)
89 (test-equal (map time-segment-time time-segments)
92 (define a-proc (const 'a))
93 (define b-proc (const 'b))
94 (define c-proc (const 'c))
95 (define d-proc (const 'd))
96 (define e-proc (const 'e))
97 (define f-proc (const 'f))
99 (define sched (make-schedule))
100 (test-assert (schedule-empty? sched))
102 ;; Add a segment at (10 . 0)
103 (schedule-add! sched 10 a-proc)
104 (test-assert (not (schedule-empty? sched)))
105 (test-equal (length (schedule-segments sched)) 1)
106 (test-equal (time-segment-time (car (schedule-segments sched)))
108 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
110 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
112 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
114 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
116 (assert-times-expected (schedule-segments sched)
119 ;; Add another segment at (10 . 0)
120 (schedule-add! sched '(10 . 0) b-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 (assert-times-expected (schedule-segments sched)
134 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
135 (schedule-add! sched 11 c-proc)
136 (schedule-add! sched '(8 . 1) d-proc)
137 (schedule-add! sched '(10 . 10) e-proc)
138 (test-assert (not (schedule-empty? sched)))
139 (test-equal (length (schedule-segments sched)) 4)
140 (assert-times-expected (schedule-segments sched)
141 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
144 (define (test-split-at schedule time expected-before expected-after)
145 (receive (segments-before segments-after)
146 (schedule-segments-split schedule time)
147 (assert-times-expected segments-before expected-before)
148 (assert-times-expected segments-after expected-after)))
150 (test-split-at sched 0
152 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
153 (test-split-at sched '(8 . 0)
155 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
156 (test-split-at sched '(8 . 1)
158 '((10 . 0) (10 . 10) (11 . 0)))
159 (test-split-at sched 9
161 '((10 . 0) (10 . 10) (11 . 0)))
162 (test-split-at sched 10
164 '((10 . 10) (11 . 0)))
165 (test-split-at sched 9000
166 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
168 (test-split-at sched '(9000 . 1) ; over nine thousaaaaaaand
169 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
172 ;; Break off half of those and do some tests on them
173 (define some-extracted
174 (schedule-extract-until! sched 10))
175 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
176 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
177 (define first-extracted-queue
178 (time-segment-queue (car some-extracted)))
179 (define second-extracted-queue
180 (time-segment-queue (cadr some-extracted)))
181 (test-assert (not (q-empty? first-extracted-queue)))
182 (test-equal ((deq! first-extracted-queue)) 'd)
183 (test-assert (q-empty? first-extracted-queue))
185 (test-assert (not (q-empty? second-extracted-queue)))
186 (test-equal ((deq! second-extracted-queue)) 'a)
187 (test-equal ((deq! second-extracted-queue)) 'b)
188 (test-assert (q-empty? second-extracted-queue))
190 ;; Add one more and test flattening to a queue
191 (test-assert (not (schedule-empty? sched)))
192 (schedule-add! sched '(10 . 10) f-proc)
193 (define remaining-segments
194 (schedule-extract-until! sched '(9000 . 1)))
195 (test-assert (schedule-empty? sched))
196 (define some-queue (make-q))
197 (enq! some-queue (const 'ho-ho))
198 (enq! some-queue (const 'ha-ha))
199 (add-segments-contents-to-queue! remaining-segments some-queue)
200 (test-assert (not (q-empty? some-queue)))
201 (test-equal 'ho-ho ((deq! some-queue)))
202 (test-equal 'ha-ha ((deq! some-queue)))
203 (test-equal 'e ((deq! some-queue)))
204 (test-equal 'f ((deq! some-queue)))
205 (test-equal 'c ((deq! some-queue)))
206 (test-assert (q-empty? some-queue))
210 ;;; Run/wrap request stuff
211 ;;; ======================
213 (let ((wrapped (wrap (+ 1 2))))
214 (test-assert (procedure? wrapped))
215 (test-equal (wrapped) 3))
217 (let ((run-two-squared (run-it (lambda () (* 2 2)))))
218 (test-assert (run-request? run-two-squared))
219 (test-assert (procedure? (run-request-proc run-two-squared)))
220 (test-equal ((run-request-proc run-two-squared)) 4)
221 (test-eq (run-request-when run-two-squared) #f))
223 (let ((run-two-squared (run-it (lambda () (* 2 2)) '(88 . 0))))
224 (test-assert (run-request? run-two-squared))
225 (test-assert (procedure? (run-request-proc run-two-squared)))
226 (test-equal ((run-request-proc run-two-squared)) 4)
227 (test-equal (run-request-when run-two-squared) '(88 . 0)))
229 (let ((run-two-squared (run (* 2 2))))
230 (test-assert (run-request? run-two-squared))
231 (test-assert (procedure? (run-request-proc run-two-squared)))
232 (test-equal ((run-request-proc run-two-squared)) 4)
233 (test-eq (run-request-when run-two-squared) #f))
235 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
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-equal (run-request-when run-two-squared) '(88 . 0)))
242 ;;; %run, %8sync and friends tests
243 ;;; ==============================
245 (define (test-%run-and-friends async-request expected-when)
246 (let* ((fake-kont (speak-it))
247 (run-request ((@@ (eightsync agenda) setup-async-request)
248 fake-kont async-request)))
249 (test-equal (car async-request) '*async-request*)
250 (test-equal (run-request-when run-request) expected-when)
251 ;; we're using speaker as a fake continuation ;p
252 ((run-request-proc run-request))
253 (test-equal (fake-kont)
256 (test-%run-and-friends (%run (string-concatenate '("apple" "sauce")))
259 (test-%run-and-friends (%run-at (string-concatenate '("apple" "sauce"))
263 (test-%run-and-friends (%run-delay (string-concatenate '("apple" "sauce"))
265 ;; whoa, I'm surprised equal? can
266 ;; compare records like this
269 ;; TODO: test %port-request
270 ;; TODO: test %8sync and friends!
278 (define (true-after-n-times n)
281 (set! count (+ count 1))
282 (if (>= count n) #t #f))))
286 (define speaker (speak-it))
289 (speaker "I'm a dummy\n"))
292 (speaker "I bet I can make you say you're a dummy!\n")
296 (set! speaker (speak-it)) ; reset the speaker
298 (start-agenda (make-agenda #:queue q)
299 #:stop-condition (true-after-n-times 2))
300 (test-equal (speaker)
301 '("I bet I can make you say you're a dummy!\n"
304 ;; should only do the first one after one round though
306 (set! speaker (speak-it)) ; reset the speaker
308 (start-agenda (make-agenda #:queue q)
309 #:stop-condition (true-after-n-times 1))
310 (test-equal (speaker)
311 '("I bet I can make you say you're a dummy!\n")))
313 ;; delimited continuation tests
315 (define (return-monkey)
316 (speaker "(Hint, it's a monkey...)\n")
319 (define (talk-about-the-zoo)
320 (speaker "Today I went to the zoo and I saw...\n")
323 `("A " ,(symbol->string (%8sync (%run (return-monkey)))) "!\n"))))
326 (set! speaker (speak-it))
327 (enq! q talk-about-the-zoo)
328 ;; (enq! q talk-about-the-zoo-but-wait)
329 (start-agenda (make-agenda #:queue q)
330 #:stop-condition (true-after-n-times 10))
331 (test-equal (speaker)
332 '("Today I went to the zoo and I saw...\n"
333 "(Hint, it's a monkey...)\n"
337 ;; Error handling tests
338 ;; --------------------
340 (define (remote-func-breaks)
341 (speaker "Here we go...\n")
343 (speaker "SHOULD NOT HAPPEN\n"))
345 (define (indirection-remote-func-breaks)
347 (%8sync (%run (remote-func-breaks)))
350 (define* (local-func-gets-break #:key with-indirection)
351 (speaker "Time for exception fun!\n")
352 (let ((caught-exception #f))
354 (%8sync (%run (if with-indirection
355 (indirection-remote-func-breaks)
356 (remote-func-breaks))))
358 (lambda (orig-stacks . orig-args)
359 (set! caught-exception #t)
360 (speaker "in here now!\n")
361 (test-equal orig-args '("/" "Numerical overflow" #f #f))
362 (test-assert (list? orig-stacks))
363 (test-equal (length orig-stacks)
364 (if with-indirection 2 1))
367 (test-assert (stack? x)))
369 (test-assert caught-exception))
370 (speaker "Well that was fun :)\n"))
374 (set! speaker (speak-it))
375 (enq! q local-func-gets-break)
376 (start-agenda (make-agenda #:queue q)
377 #:stop-condition (true-after-n-times 10))
378 (test-assert (speaker)
379 '("Time for exception fun!\n"
382 "Well that was fun :)\n")))
385 (set! speaker (speak-it))
386 (enq! q (wrap (local-func-gets-break #:with-indirection #t)))
387 (start-agenda (make-agenda #:queue q)
388 #:stop-condition (true-after-n-times 10))
389 (test-assert (speaker)
390 '("Time for exception fun!\n"
394 "Well that was fun :)\n")))
396 ;; Make sure catching tools work
398 (let ((speaker (speak-it))
403 (throw '8sync-caught-error
404 'my-orig-key '(apple orange banana) '(*fake-stack* *fake-stack* *fake-stack*))
405 (speaker "no goodbyes"))
407 (lambda (stacks . rest)
408 (speaker "should not happen")))
410 (lambda (stacks fruit1 fruit2 fruit3)
414 (fruit3 ,fruit3))))))
415 (test-equal (speaker) '("hello"))
416 (test-equal catch-result '((fruit1 apple)
422 (test-end "test-agenda")
424 ;; @@: A better way to handle this at the repl?