agenda: When re-invoking a continuation at the agenda, don't wrap in a catch.
[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 (define-module (tests test-agenda)
20   #:use-module (srfi srfi-64)
21   #:use-module (ice-9 q)
22   #:use-module (ice-9 match)
23   #:use-module (ice-9 receive)
24   #:use-module (8sync agenda)
25   #:use-module (tests utils))
26
27 (test-begin "test-agenda")
28
29 (define-syntax-rule (%import var)
30   (define var
31     (@@ (8sync agenda) var)))
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 (%import time=)
66 (%import time<)
67 (%import time-minus)
68 (%import time-plus)
69
70 (test-assert (time= '(1 . 1) '(1 . 1)))
71 (test-assert (not (time= '(1 . 1) '(1 . 0))))
72 (test-assert (not (time= '(0 . 1) '(1 . 1))))
73
74 (test-assert (time< '(1 . 1) '(1 . 2)))
75 (test-assert (time< '(7 . 2) '(8 . 2)))
76 (test-assert (not (time< '(7 . 2) '(7 . 2))))
77 (test-assert (not (time< '(7 . 8) '(7 . 2))))
78 (test-assert (not (time< '(8 . 2) '(7 . 2))))
79
80 (test-equal (time-minus '(100 . 100) '(50 . 66))
81             '(50 . 34))
82 (test-equal (time-minus '(2 . 0) '(0 . 1))
83             '(1 . 999999))
84
85 (test-equal (time-plus '(50 . 34) '(50 . 66))
86             '(100 . 100))
87 (test-equal (time-plus '(1 . 999999) '(1 . 2))
88             '(3 . 1))
89
90
91 \f
92 ;;; Schedule tests
93 ;;; ==============
94
95 (%import time-segment-time)
96 (%import time-segment-queue)
97
98 ;; helpers
99 (define (assert-times-expected time-segments expected-times)
100   (test-equal (map time-segment-time time-segments)
101     expected-times))
102
103 (define a-proc (const 'a))
104 (define b-proc (const 'b))
105 (define c-proc (const 'c))
106 (define d-proc (const 'd))
107 (define e-proc (const 'e))
108 (define f-proc (const 'f))
109
110 (define sched (make-schedule))
111 (test-assert (schedule-empty? sched))
112
113 ;; Add a segment at (10 . 0)
114 (schedule-add! sched '(10 . 0) a-proc)
115 (test-assert (not (schedule-empty? sched)))
116 (test-equal (length (schedule-segments sched)) 1)
117 (test-equal (time-segment-time (car (schedule-segments sched)))
118   '(10 . 0))
119 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
120   1)
121 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
122   a-proc)
123 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
124   a-proc)
125 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
126   'a) ;; why not
127 (assert-times-expected (schedule-segments sched)
128                        '((10 . 0)))
129
130 ;; Add another segment at (10 . 0)
131 (schedule-add! sched '(10 . 0) b-proc)
132 (test-assert (not (schedule-empty? sched)))
133 (test-equal (length (schedule-segments sched)) 1)
134 (test-equal (time-segment-time (car (schedule-segments sched)))
135   '(10 . 0))
136 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
137   2)
138 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
139   a-proc)
140 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
141   b-proc)
142 (assert-times-expected (schedule-segments sched)
143                        '((10 . 0)))
144
145 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
146 (schedule-add! sched '(11 . 0) c-proc)
147 (schedule-add! sched '(8 . 1) d-proc)
148 (schedule-add! sched '(10 . 10) e-proc)
149 (test-assert (not (schedule-empty? sched)))
150 (test-equal (length (schedule-segments sched)) 4)
151 (assert-times-expected (schedule-segments sched)
152                        '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
153
154 ;; Splitting 
155 (define (test-split-at schedule time expected-before expected-after)
156   (receive (segments-before segments-after)
157       (schedule-segments-split schedule time)
158     (assert-times-expected segments-before expected-before)
159     (assert-times-expected segments-after expected-after)))
160
161 (test-split-at sched '(0 . 0)
162                '()
163                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
164 (test-split-at sched '(8 . 0)
165                '()
166                '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
167 (test-split-at sched '(8 . 1)
168                '((8 . 1))
169                '((10 . 0) (10 . 10) (11 . 0)))
170 (test-split-at sched '(9 . 0)
171                '((8 . 1))
172                '((10 . 0) (10 . 10) (11 . 0)))
173 (test-split-at sched '(10 . 0)
174                '((8 . 1) (10 . 0))
175                '((10 . 10) (11 . 0)))
176 (test-split-at sched '(9000 . 0)
177                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
178                '())
179 (test-split-at sched '(9000 . 1)    ; over nine thousaaaaaaand
180                '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
181                '())
182
183 ;; Break off half of those and do some tests on them
184 (define some-extracted
185   (schedule-extract-until! sched '(10 . 0)))
186 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
187 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
188 (define first-extracted-queue
189   (time-segment-queue (car some-extracted)))
190 (define second-extracted-queue
191   (time-segment-queue (cadr some-extracted)))
192 (test-assert (not (q-empty? first-extracted-queue)))
193 (test-equal ((deq! first-extracted-queue)) 'd)
194 (test-assert (q-empty? first-extracted-queue))
195
196 (test-assert (not (q-empty? second-extracted-queue)))
197 (test-equal ((deq! second-extracted-queue)) 'a)
198 (test-equal ((deq! second-extracted-queue)) 'b)
199 (test-assert (q-empty? second-extracted-queue))
200
201 ;; Add one more and test flattening to a queue
202 (test-assert (not (schedule-empty? sched)))
203 (schedule-add! sched '(10 . 10) f-proc)
204 (define remaining-segments
205   (schedule-extract-until! sched '(9000 . 1)))
206 (test-assert (schedule-empty? sched))
207 (define some-queue (make-q))
208 (enq! some-queue (const 'ho-ho))
209 (enq! some-queue (const 'ha-ha))
210 (add-segments-contents-to-queue! remaining-segments some-queue)
211 (test-assert (not (q-empty? some-queue)))
212 (test-equal 'ho-ho ((deq! some-queue)))
213 (test-equal 'ha-ha ((deq! some-queue)))
214 (test-equal 'e ((deq! some-queue)))
215 (test-equal 'f ((deq! some-queue)))
216 (test-equal 'c ((deq! some-queue)))
217 (test-assert (q-empty? some-queue))
218
219 ;; ... whew!
220
221 ;;; Run/wrap request stuff
222 ;;; ======================
223
224 (let ((wrapped (wrap (+ 1 2))))
225   (test-assert (procedure? wrapped))
226   (test-equal (wrapped) 3))
227
228 (let ((run-two-squared (run-it (lambda () (* 2 2)))))
229   (test-assert (run-request? run-two-squared))
230   (test-assert (procedure? (run-request-proc run-two-squared)))
231   (test-equal ((run-request-proc run-two-squared)) 4)
232   (test-eq (run-request-when run-two-squared) #f))
233
234 (let ((run-two-squared (run-it (lambda () (* 2 2)) '(88 . 0))))
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-equal (run-request-when run-two-squared) '(88 . 0)))
239
240 (let ((run-two-squared (run (* 2 2))))
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-eq (run-request-when run-two-squared) #f))
245
246 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
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-equal (run-request-when run-two-squared) '(88 . 0)))
251
252
253 ;;; %run, 8sync and friends tests
254 ;;; ==============================
255
256 ;; TODO: We need to rewrite the whole lot here...
257
258 ;;; Agenda tests
259 ;;; ============
260
261 ;; helpers
262
263 (define (true-after-n-times n)
264   (let ((count 0))
265     (lambda _
266       (define ans
267         (if (>= count n) #t #f))
268       (set! count (+ count 1))
269       ans)))
270
271 ;; the dummy test
272
273 (define speaker (speak-it))
274
275 (define (dummy-func)
276   (speaker "I'm a dummy\n"))
277
278 (define (run-dummy)
279   (speaker "I bet I can make you say you're a dummy!\n")
280   (run-it dummy-func))
281
282 (begin
283   (set! speaker (speak-it))  ; reset the speaker
284   (start-agenda (make-agenda #:queue (make-q* run-dummy))
285                 #:stop-condition (true-after-n-times 2))
286   (test-equal (speaker)
287     '("I bet I can make you say you're a dummy!\n"
288       "I'm a dummy\n")))
289
290 ;; should only do the first one after one round though
291 (begin
292   (set! speaker (speak-it))  ; reset the speaker
293   (start-agenda (make-agenda #:queue (make-q* run-dummy))
294                 #:stop-condition (true-after-n-times 1))
295   (test-equal (speaker)
296     '("I bet I can make you say you're a dummy!\n")))
297
298
299 ;; End tests
300
301 (test-end "test-agenda")
302
303 ;; @@: A better way to handle this at the repl?
304 (test-exit)
305