1 ;;; 8sync --- Asynchronous programming for Guile
2 ;;; Copyright (C) 2015 Christopher Allan Webber <cwebber@dustycloud.org>
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.
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.
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
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))
27 (test-begin "test-agenda")
36 (lambda* (#:optional message)
37 (if message (set! messages (append messages (list message))))
44 (define test-q (list->q '(1 2 3)))
45 (test-equal (deq! test-q) 1)
46 (test-equal (deq! test-q) 2)
47 (test-equal (deq! test-q) 3)
48 (test-assert (q-empty? test-q))
50 (define test-q (make-q* 'apple 'banana 'carrot))
51 (test-equal (deq! test-q) 'apple)
52 (test-equal (deq! test-q) 'banana)
53 (test-equal (deq! test-q) 'carrot)
54 (test-assert (q-empty? test-q))
61 (test-assert (time= '(1 . 1) '(1 . 1)))
62 (test-assert (not (time= '(1 . 1) '(1 . 0))))
63 (test-assert (not (time= '(0 . 1) '(1 . 1))))
65 (test-assert (time< '(1 . 1) '(1 . 2)))
66 (test-assert (time< '(7 . 2) '(8 . 2)))
67 (test-assert (not (time< '(7 . 2) '(7 . 2))))
68 (test-assert (not (time< '(7 . 8) '(7 . 2))))
69 (test-assert (not (time< '(8 . 2) '(7 . 2))))
71 (let ((tdelta (make-time-delta 8)))
72 (test-assert (time-delta? tdelta))
73 (test-eqv (time-delta-sec tdelta) 8)
74 (test-eqv (time-delta-usec tdelta) 0)
76 (time-delta+ '(2 . 3) tdelta)
79 (let ((tdelta (make-time-delta '(10 . 1))))
80 (test-assert (time-delta? tdelta))
81 (test-eqv (time-delta-sec tdelta) 10)
82 (test-eqv (time-delta-usec tdelta) 1)
84 (time-delta+ '(2 . 3) tdelta)
87 (test-equal (time-minus '(100 . 100) '(50 . 66))
89 (test-equal (time-minus '(2 . 0) '(0 . 1))
92 (test-equal (time-plus '(50 . 34) '(50 . 66))
94 (test-equal (time-plus '(1 . 999999) '(1 . 2))
103 (define (assert-times-expected time-segments expected-times)
104 (test-equal (map time-segment-time time-segments)
107 (define a-proc (const 'a))
108 (define b-proc (const 'b))
109 (define c-proc (const 'c))
110 (define d-proc (const 'd))
111 (define e-proc (const 'e))
112 (define f-proc (const 'f))
114 (define sched (make-schedule))
115 (test-assert (schedule-empty? sched))
117 ;; Add a segment at (10 . 0)
118 (schedule-add! sched 10 a-proc)
119 (test-assert (not (schedule-empty? sched)))
120 (test-equal (length (schedule-segments sched)) 1)
121 (test-equal (time-segment-time (car (schedule-segments sched)))
123 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
125 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
127 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
129 (test-eq ((q-front (time-segment-queue (car (schedule-segments sched)))))
131 (assert-times-expected (schedule-segments sched)
134 ;; Add another segment at (10 . 0)
135 (schedule-add! sched '(10 . 0) b-proc)
136 (test-assert (not (schedule-empty? sched)))
137 (test-equal (length (schedule-segments sched)) 1)
138 (test-equal (time-segment-time (car (schedule-segments sched)))
140 (test-equal (q-length (time-segment-queue (car (schedule-segments sched))))
142 (test-eq (q-front (time-segment-queue (car (schedule-segments sched))))
144 (test-eq (q-rear (time-segment-queue (car (schedule-segments sched))))
146 (assert-times-expected (schedule-segments sched)
149 ;; Add a segment to (11 . 0), (8 . 1) and (10 . 10)
150 (schedule-add! sched 11 c-proc)
151 (schedule-add! sched '(8 . 1) d-proc)
152 (schedule-add! sched '(10 . 10) e-proc)
153 (test-assert (not (schedule-empty? sched)))
154 (test-equal (length (schedule-segments sched)) 4)
155 (assert-times-expected (schedule-segments sched)
156 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
159 (define (test-split-at schedule time expected-before expected-after)
160 (receive (segments-before segments-after)
161 (schedule-segments-split schedule time)
162 (assert-times-expected segments-before expected-before)
163 (assert-times-expected segments-after expected-after)))
165 (test-split-at sched 0
167 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
168 (test-split-at sched '(8 . 0)
170 '((8 . 1) (10 . 0) (10 . 10) (11 . 0)))
171 (test-split-at sched '(8 . 1)
173 '((10 . 0) (10 . 10) (11 . 0)))
174 (test-split-at sched 9
176 '((10 . 0) (10 . 10) (11 . 0)))
177 (test-split-at sched 10
179 '((10 . 10) (11 . 0)))
180 (test-split-at sched 9000
181 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
183 (test-split-at sched '(9000 . 1) ; over nine thousaaaaaaand
184 '((8 . 1) (10 . 0) (10 . 10) (11 . 0))
187 ;; Break off half of those and do some tests on them
188 (define some-extracted
189 (schedule-extract-until! sched 10))
190 (assert-times-expected some-extracted '((8 . 1) (10 . 0)))
191 (assert-times-expected (schedule-segments sched) '((10 . 10) (11 . 0)))
192 (define first-extracted-queue
193 (time-segment-queue (car some-extracted)))
194 (define second-extracted-queue
195 (time-segment-queue (cadr some-extracted)))
196 (test-assert (not (q-empty? first-extracted-queue)))
197 (test-equal ((deq! first-extracted-queue)) 'd)
198 (test-assert (q-empty? first-extracted-queue))
200 (test-assert (not (q-empty? second-extracted-queue)))
201 (test-equal ((deq! second-extracted-queue)) 'a)
202 (test-equal ((deq! second-extracted-queue)) 'b)
203 (test-assert (q-empty? second-extracted-queue))
205 ;; Add one more and test flattening to a queue
206 (test-assert (not (schedule-empty? sched)))
207 (schedule-add! sched '(10 . 10) f-proc)
208 (define remaining-segments
209 (schedule-extract-until! sched '(9000 . 1)))
210 (test-assert (schedule-empty? sched))
211 (define some-queue (make-q))
212 (enq! some-queue (const 'ho-ho))
213 (enq! some-queue (const 'ha-ha))
214 (add-segments-contents-to-queue! remaining-segments some-queue)
215 (test-assert (not (q-empty? some-queue)))
216 (test-equal 'ho-ho ((deq! some-queue)))
217 (test-equal 'ha-ha ((deq! some-queue)))
218 (test-equal 'e ((deq! some-queue)))
219 (test-equal 'f ((deq! some-queue)))
220 (test-equal 'c ((deq! some-queue)))
221 (test-assert (q-empty? some-queue))
225 ;;; Run/wrap request stuff
226 ;;; ======================
228 (let ((wrapped (wrap (+ 1 2))))
229 (test-assert (procedure? wrapped))
230 (test-equal (wrapped) 3))
232 (let ((run-two-squared (run-it (lambda () (* 2 2)))))
233 (test-assert (run-request? run-two-squared))
234 (test-assert (procedure? (run-request-proc run-two-squared)))
235 (test-equal ((run-request-proc run-two-squared)) 4)
236 (test-eq (run-request-when run-two-squared) #f))
238 (let ((run-two-squared (run-it (lambda () (* 2 2)) '(88 . 0))))
239 (test-assert (run-request? run-two-squared))
240 (test-assert (procedure? (run-request-proc run-two-squared)))
241 (test-equal ((run-request-proc run-two-squared)) 4)
242 (test-equal (run-request-when run-two-squared) '(88 . 0)))
244 (let ((run-two-squared (run (* 2 2))))
245 (test-assert (run-request? run-two-squared))
246 (test-assert (procedure? (run-request-proc run-two-squared)))
247 (test-equal ((run-request-proc run-two-squared)) 4)
248 (test-eq (run-request-when run-two-squared) #f))
250 (let ((run-two-squared (run-at (* 2 2) '(88 . 0))))
251 (test-assert (run-request? run-two-squared))
252 (test-assert (procedure? (run-request-proc run-two-squared)))
253 (test-equal ((run-request-proc run-two-squared)) 4)
254 (test-equal (run-request-when run-two-squared) '(88 . 0)))
257 ;;; %run, 8sync and friends tests
258 ;;; ==============================
260 ;; TODO: We need to rewrite the whole lot here...
267 (define (true-after-n-times n)
270 (set! count (+ count 1))
271 (if (>= count n) #t #f))))
275 (define speaker (speak-it))
278 (speaker "I'm a dummy\n"))
281 (speaker "I bet I can make you say you're a dummy!\n")
285 (set! speaker (speak-it)) ; reset the speaker
286 (start-agenda (make-agenda #:queue (make-q* run-dummy))
287 #:stop-condition (true-after-n-times 2))
288 (test-equal (speaker)
289 '("I bet I can make you say you're a dummy!\n"
292 ;; should only do the first one after one round though
294 (set! speaker (speak-it)) ; reset the speaker
295 (start-agenda (make-agenda #:queue (make-q* run-dummy))
296 #:stop-condition (true-after-n-times 1))
297 (test-equal (speaker)
298 '("I bet I can make you say you're a dummy!\n")))
303 (test-end "test-agenda")
305 ;; @@: A better way to handle this at the repl?