switch %8sync-caught-error to 8sync-caught-error
[8sync.git] / tests / test-agenda.scm
1 ;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
2
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.
7 ;;
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.
12 ;;
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
16 ;; 02110-1301 USA
17
18 #!/usr/bin/guile \
19 -s
20 !#
21
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))
28
29 (test-begin "test-agenda")
30
31 \f
32
33 ;;; Helpers
34 ;;; =======
35
36 (define (speak-it)
37   (let ((messages '()))
38     (lambda* (#:optional message)
39       (if message (set! messages (append messages (list message))))
40       messages)))
41
42 \f
43 ;; Timer tests
44 ;; ===========
45
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))))
49
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))))
55
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)
60   (test-equal
61       (time-delta+ '(2 . 3) tdelta)
62     '(10 . 3)))
63
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)
68   (test-equal
69       (time-delta+ '(2 . 3) tdelta)
70     '(12 . 4)))
71
72 (test-equal (time-minus '(100 . 100) '(50 . 66))
73             '(50 . 34))
74 (test-equal (time-minus '(2 . 0) '(0 . 1))
75             '(1 . 999999))
76
77 (test-equal (time-plus '(50 . 34) '(50 . 66))
78             '(100 . 100))
79 (test-equal (time-plus '(1 . 999999) '(1 . 2))
80             '(3 . 1))
81
82
83 \f
84 ;;; Schedule tests
85 ;;; ==============
86
87 ;; helpers
88 (define (assert-times-expected time-segments expected-times)
89   (test-equal (map time-segment-time time-segments)
90     expected-times))
91
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))
98
99 (define sched (make-schedule))
100 (test-assert (schedule-empty? sched))
101
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)))
107   '(10 . 0))
108 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
109   1)
110 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
111   a-proc)
112 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
113   a-proc)
114 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
115   'a) ;; why not
116 (assert-times-expected (schedule-segments sched)
117                        '((10 . 0)))
118
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)))
124   '(10 . 0))
125 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
126   2)
127 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
128   a-proc)
129 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
130   b-proc)
131 (assert-times-expected (schedule-segments sched)
132                        '((10 . 0)))
133
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)))
142
143 ;; Splitting 
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)))
149
150 (test-split-at sched 0
151                '()
152                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
153 (test-split-at sched '(8 . 0)
154                '()
155                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
156 (test-split-at sched '(8 . 1)
157                '((8 . 1))
158                '((10 . 0) (10 . 10) (11 . 0)))
159 (test-split-at sched 9
160                '((8 . 1))
161                '((10 . 0) (10 . 10) (11 . 0)))
162 (test-split-at sched 10
163                '((8 . 1) (10 . 0))
164                '((10 . 10) (11 . 0)))
165 (test-split-at sched 9000
166                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
167                '())
168 (test-split-at sched '(9000 . 1)    ; over nine thousaaaaaaand
169                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
170                '())
171
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))
184
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))
189
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))
207
208 ;; ... whew!
209
210 ;;; Run/wrap request stuff
211 ;;; ======================
212
213 (let ((wrapped (wrap (+ 1 2))))
214   (test-assert (procedure? wrapped))
215   (test-equal (wrapped) 3))
216
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))
222
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)))
228
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))
234
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)))
240
241
242 ;;; %run, %8sync and friends tests
243 ;;; ==============================
244
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)
254                 '("applesauce"))))
255
256 (test-%run-and-friends (%run (string-concatenate '("apple" "sauce")))
257                        #f)
258
259 (test-%run-and-friends (%run-at (string-concatenate '("apple" "sauce"))
260                                 '(8 . 0))
261                        '(8 . 0))
262
263 (test-%run-and-friends (%run-delay (string-concatenate '("apple" "sauce"))
264                                    8)
265                        ;; whoa, I'm surprised equal? can
266                        ;; compare records like this
267                        (tdelta 8))
268
269 ;; TODO: test %port-request
270 ;; TODO: test %8sync and friends!
271
272
273 ;;; Agenda tests
274 ;;; ============
275
276 ;; helpers
277
278 (define (true-after-n-times n)
279   (let ((count 0))
280     (lambda _
281       (set! count (+ count 1))
282       (if (>= count n) #t #f))))
283
284 ;; the dummy test
285
286 (define speaker (speak-it))
287
288 (define (dummy-func)
289   (speaker "I'm a dummy\n"))
290
291 (define (run-dummy)
292   (speaker "I bet I can make you say you're a dummy!\n")
293   (run-it dummy-func))
294
295 (let ((q (make-q)))
296   (set! speaker (speak-it))  ; reset the speaker
297   (enq! q run-dummy)
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"
302       "I'm a dummy\n")))
303
304 ;; should only do the first one after one round though
305 (let ((q (make-q)))
306   (set! speaker (speak-it))  ; reset the speaker
307   (enq! q run-dummy)
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")))
312
313 ;; delimited continuation tests
314
315 (define (return-monkey)
316   (speaker "(Hint, it's a monkey...)\n")
317   'monkey)
318
319 (define (talk-about-the-zoo)
320   (speaker "Today I went to the zoo and I saw...\n")
321   (speaker
322    (string-concatenate
323     `("A " ,(symbol->string (%8sync (%run (return-monkey)))) "!\n"))))
324
325 (let ((q (make-q)))
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"
334                 "A monkey!\n")))
335
336
337 ;; Error handling tests
338 ;; --------------------
339
340 (define (remote-func-breaks)
341   (speaker "Here we go...\n")
342   (+ 1 2 (/ 1 0))
343   (speaker "SHOULD NOT HAPPEN\n"))
344
345 (define (indirection-remote-func-breaks)
346   (speaker "bebop\n")
347   (%8sync (%run (remote-func-breaks)))
348   (speaker "bidop\n"))
349
350 (define* (local-func-gets-break #:key with-indirection)
351   (speaker "Time for exception fun!\n")
352   (let ((caught-exception #f))
353     (catch-8sync
354      (%8sync (%run (if with-indirection
355                        (indirection-remote-func-breaks)
356                        (remote-func-breaks))))
357       ('numerical-overflow
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))
365          (for-each
366           (lambda (x)
367             (test-assert (stack? x)))
368           orig-stacks))))
369     (test-assert caught-exception))
370   (speaker "Well that was fun :)\n"))
371
372
373 (let ((q (make-q)))
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"
380                  "Here we go...\n"
381                  "in here now!\n"
382                  "Well that was fun :)\n")))
383
384 (let ((q (make-q)))
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"
391                  "bebop\n"
392                  "Here we go...\n"
393                  "in here now!\n"
394                  "Well that was fun :)\n")))
395
396 ;; Make sure catching tools work
397
398 (let ((speaker (speak-it))
399       (catch-result #f))
400   (catch-8sync
401    (begin
402      (speaker "hello")
403      (throw '8sync-caught-error
404             'my-orig-key '(apple orange banana) '(*fake-stack* *fake-stack* *fake-stack*))
405      (speaker "no goodbyes"))
406    ('some-key
407     (lambda (stacks . rest)
408       (speaker "should not happen")))
409    ('my-orig-key
410     (lambda (stacks fruit1 fruit2 fruit3)
411       (set! catch-result
412             `((fruit1 ,fruit1)
413               (fruit2 ,fruit2)
414               (fruit3 ,fruit3))))))
415   (test-equal (speaker) '("hello"))
416   (test-equal catch-result '((fruit1 apple)
417                              (fruit2 orange)
418                              (fruit3 banana))))
419
420 ;; End tests
421
422 (test-end "test-agenda")
423
424 ;; @@: A better way to handle this at the repl?
425 (test-exit)
426