agenda: Rename %8sync (and %8sync-*) to 8sync (and 8sync-*)
[8sync.git] / tests / test-agenda.scm
1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
3 ;;;
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.
8 ;;;
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.
13 ;;;
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
17 ;;; 02110-1301 USA
18
19 #!/usr/bin/guile \
20 -s
21 !#
22
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))
30
31 (test-begin "test-agenda")
32
33 \f
34
35 ;;; Helpers
36 ;;; =======
37
38 (define (speak-it)
39   (let ((messages '()))
40     (lambda* (#:optional message)
41       (if message (set! messages (append messages (list message))))
42       messages)))
43
44 \f
45 ;;; queue helpers
46 ;;; =============
47
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))
53
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))
59
60
61 \f
62 ;;; Timer tests
63 ;;; ===========
64
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))))
68
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))))
74
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)
79   (test-equal
80       (time-delta+ '(2 . 3) tdelta)
81     '(10 . 3)))
82
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)
87   (test-equal
88       (time-delta+ '(2 . 3) tdelta)
89     '(12 . 4)))
90
91 (test-equal (time-minus '(100 . 100) '(50 . 66))
92             '(50 . 34))
93 (test-equal (time-minus '(2 . 0) '(0 . 1))
94             '(1 . 999999))
95
96 (test-equal (time-plus '(50 . 34) '(50 . 66))
97             '(100 . 100))
98 (test-equal (time-plus '(1 . 999999) '(1 . 2))
99             '(3 . 1))
100
101
102 \f
103 ;;; Schedule tests
104 ;;; ==============
105
106 ;; helpers
107 (define (assert-times-expected time-segments expected-times)
108   (test-equal (map time-segment-time time-segments)
109     expected-times))
110
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))
117
118 (define sched (make-schedule))
119 (test-assert (schedule-empty? sched))
120
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)))
126   '(10 . 0))
127 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
128   1)
129 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
130   a-proc)
131 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
132   a-proc)
133 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
134   'a) ;; why not
135 (assert-times-expected (schedule-segments sched)
136                        '((10 . 0)))
137
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)))
143   '(10 . 0))
144 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
145   2)
146 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
147   a-proc)
148 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
149   b-proc)
150 (assert-times-expected (schedule-segments sched)
151                        '((10 . 0)))
152
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)))
161
162 ;; Splitting 
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)))
168
169 (test-split-at sched 0
170                '()
171                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
172 (test-split-at sched '(8 . 0)
173                '()
174                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
175 (test-split-at sched '(8 . 1)
176                '((8 . 1))
177                '((10 . 0) (10 . 10) (11 . 0)))
178 (test-split-at sched 9
179                '((8 . 1))
180                '((10 . 0) (10 . 10) (11 . 0)))
181 (test-split-at sched 10
182                '((8 . 1) (10 . 0))
183                '((10 . 10) (11 . 0)))
184 (test-split-at sched 9000
185                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
186                '())
187 (test-split-at sched '(9000 . 1)    ; over nine thousaaaaaaand
188                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
189                '())
190
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))
203
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))
208
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))
226
227 ;; ... whew!
228
229 ;;; Run/wrap request stuff
230 ;;; ======================
231
232 (let ((wrapped (wrap (+ 1 2))))
233   (test-assert (procedure? wrapped))
234   (test-equal (wrapped) 3))
235
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))
241
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)))
247
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))
253
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)))
259
260
261 ;;; %run, 8sync and friends tests
262 ;;; ==============================
263
264 (define-syntax-rule (run-in-fake-agenda
265                      code-to-run)
266   (let ((agenda (make-agenda)))
267     (parameterize ((%current-agenda agenda))
268       (call-with-prompt
269        (agenda-prompt-tag agenda)
270        (lambda ()
271          (list '*normal-result* code-to-run))
272        (lambda (kont async-request)
273          (list '*caught-kont*
274                kont async-request
275                ((@@ (8sync agenda) setup-async-request)
276                 kont async-request)))))))
277
278 (define (test-%run-and-friends run-result expected-when)
279   (match run-result
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)
289                    '("applesauce"))))))
290
291 (test-%run-and-friends (run-in-fake-agenda
292                         (8sync (string-concatenate '("apple" "sauce"))))
293                        #f)
294
295 (test-%run-and-friends (run-in-fake-agenda
296                         (8sync (string-concatenate '("apple" "sauce"))
297                                 '(8 . 0)))
298                        '(8 . 0))
299
300 (test-%run-and-friends (run-in-fake-agenda
301                         (8sync-delay (string-concatenate '("apple" "sauce"))
302                                       8))
303                        ;; whoa, I'm surprised equal? can
304                        ;; compare records like this
305                        (tdelta 8))
306
307 ;; TODO: test %port-request
308 ;; TODO: test 8sync and friends!
309
310
311 ;;; Agenda tests
312 ;;; ============
313
314 ;; helpers
315
316 (define (true-after-n-times n)
317   (let ((count 0))
318     (lambda _
319       (set! count (+ count 1))
320       (if (>= count n) #t #f))))
321
322 ;; the dummy test
323
324 (define speaker (speak-it))
325
326 (define (dummy-func)
327   (speaker "I'm a dummy\n"))
328
329 (define (run-dummy)
330   (speaker "I bet I can make you say you're a dummy!\n")
331   (run-it dummy-func))
332
333 (begin
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"
339       "I'm a dummy\n")))
340
341 ;; should only do the first one after one round though
342 (begin
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")))
348
349 ;; delimited continuation tests
350
351 (define (return-monkey)
352   (speaker "(Hint, it's a monkey...)\n")
353   'monkey)
354
355 (define (talk-about-the-zoo)
356   (speaker "Today I went to the zoo and I saw...\n")
357   (speaker
358    (string-concatenate
359     `("A " ,(symbol->string (8sync (return-monkey))) "!\n"))))
360
361 (begin
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"
369                 "A monkey!\n")))
370
371
372 ;; Error handling tests
373 ;; --------------------
374
375 (define (remote-func-breaks)
376   (speaker "Here we go...\n")
377   (+ 1 2 (/ 1 0))
378   (speaker "SHOULD NOT HAPPEN\n"))
379
380 (define (indirection-remote-func-breaks)
381   (speaker "bebop\n")
382   (8sync (remote-func-breaks))
383   (speaker "bidop\n"))
384
385 (define* (local-func-gets-break #:key with-indirection)
386   (speaker "Time for exception fun!\n")
387   (let ((caught-exception #f))
388     (catch-8sync
389      (8sync-run (if with-indirection
390                          (indirection-remote-func-breaks)
391                          (remote-func-breaks)))
392       ('numerical-overflow
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))
400          (for-each
401           (lambda (x)
402             (test-assert (stack? x)))
403           orig-stacks))))
404     (test-assert caught-exception))
405   (speaker "Well that was fun :)\n"))
406
407
408 (begin
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"
414                 "Here we go...\n"
415                 "in here now!\n"
416                 "Well that was fun :)\n")))
417
418 (begin
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"
426                 "bebop\n"
427                 "Here we go...\n"
428                 "in here now!\n"
429                 "Well that was fun :)\n")))
430
431 ;; Make sure catching tools work
432
433 (let ((speaker (speak-it))
434       (catch-result #f))
435   (catch-8sync
436    (begin
437      (speaker "hello")
438      (throw '8sync-caught-error
439             'my-orig-key '(apple orange banana) '(*fake-stack* *fake-stack* *fake-stack*))
440      (speaker "no goodbyes"))
441    ('some-key
442     (lambda (stacks . rest)
443       (speaker "should not happen")))
444    ('my-orig-key
445     (lambda (stacks fruit1 fruit2 fruit3)
446       (set! catch-result
447             `((fruit1 ,fruit1)
448               (fruit2 ,fruit2)
449               (fruit3 ,fruit3))))))
450   (test-equal (speaker) '("hello"))
451   (test-equal catch-result '((fruit1 apple)
452                              (fruit2 orange)
453                              (fruit3 banana))))
454
455 ;; End tests
456
457 (test-end "test-agenda")
458
459 ;; @@: A better way to handle this at the repl?
460 (test-exit)
461