Add (catch-8sync) stuff
[8sync.git] / tests / test-agenda.scm
index e183fdcfbcc57a9cf0c247f409852797e66d6726..6918c992818c625336fe4febfe41d6fa3d448dfa 100644 (file)
 -s
 !#
 
-(define-module (tests test-core)
+(define-module (tests test-agenda)
   #:use-module (srfi srfi-64)
   #:use-module (ice-9 q)
   #:use-module (ice-9 receive)
-  #:use-module (eightsync agenda))
+  #:use-module (eightsync agenda)
+  #:use-module (tests utils))
 
-(test-begin "tests-agenda")
+(test-begin "test-agenda")
 
 \f
 
@@ -60,7 +61,7 @@
       (time-delta+ '(2 . 3) tdelta)
     '(10 . 3)))
 
-(let ((tdelta (make-time-delta 10 1)))
+(let ((tdelta (make-time-delta '(10 . 1))))
   (test-assert (time-delta? tdelta))
   (test-eqv (time-delta-sec tdelta) 10)
   (test-eqv (time-delta-usec tdelta) 1)
       (time-delta+ '(2 . 3) tdelta)
     '(12 . 4)))
 
+(test-equal (time-minus '(100 . 100) '(50 . 66))
+            '(50 . 34))
+(test-equal (time-minus '(2 . 0) '(0 . 1))
+            '(1 . 999999))
+
+(test-equal (time-plus '(50 . 34) '(50 . 66))
+            '(100 . 100))
+(test-equal (time-plus '(1 . 999999) '(1 . 2))
+            '(3 . 1))
+
 
 \f
 ;;; Schedule tests
 
 ;; ... whew!
 
-;; Run/wrap request stuff
-;; ----------------------
+;;; Run/wrap request stuff
+;;; ======================
 
 (let ((wrapped (wrap (+ 1 2))))
   (test-assert (procedure? wrapped))
   (test-equal (run-request-when run-two-squared) '(88 . 0)))
 
 
-;;; %run, %sync and friends tests
-;;; -----------------------------
+;;; %run, %8sync and friends tests
+;;; ==============================
 
 (define (test-%run-and-friends async-request expected-when)
   (let* ((fake-kont (speak-it))
                                    8)
                        ;; whoa, I'm surprised equal? can
                        ;; compare records like this
-                       (tdelta 8 0))
+                       (tdelta 8))
 
 ;; TODO: test %port-request
-;; TODO: test %sync and friends!
+;; TODO: test %8sync and friends!
 
 
 ;;; Agenda tests
-;;; ------------
+;;; ============
 
 ;; helpers
 
   (speaker "Today I went to the zoo and I saw...\n")
   (speaker
    (string-concatenate
-    `("A " ,(symbol->string (%sync (%run (return-monkey)))) "!\n"))))
+    `("A " ,(symbol->string (%8sync (%run (return-monkey)))) "!\n"))))
 
 (let ((q (make-q)))
   (set! speaker (speak-it))
                 "(Hint, it's a monkey...)\n"
                 "A monkey!\n")))
 
+
+;; Error handling tests
+;; --------------------
+
+(define (remote-func-breaks)
+  (speaker "Here we go...\n")
+  (+ 1 2 (/ 1 0))
+  (speaker "SHOULD NOT HAPPEN\n"))
+
+(define (indirection-remote-func-breaks)
+  (speaker "bebop\n")
+  (%8sync (%run (remote-func-breaks)))
+  (speaker "bidop\n"))
+
+(define* (local-func-gets-break #:key with-indirection)
+  (speaker "Time for exception fun!\n")
+  (let ((caught-exception #f))
+    (catch '%8sync-caught-error
+      (lambda ()
+        (%8sync (%run (if with-indirection
+                          (indirection-remote-func-breaks)
+                          (remote-func-breaks)))))
+      (lambda (_ orig-key orig-args orig-stacks)
+        (set! caught-exception #t)
+        (speaker "in here now!\n")
+        (test-equal orig-key 'numerical-overflow)
+        (test-equal orig-args '("/" "Numerical overflow" #f #f))
+        (test-assert (list? orig-stacks))
+        (test-equal (length orig-stacks)
+                    (if with-indirection 2 1))
+        (for-each
+         (lambda (x)
+           (test-assert (stack? x)))
+         orig-stacks)))
+    (test-assert caught-exception))
+  (speaker "Well that was fun :)\n"))
+
+
+(let ((q (make-q)))
+  (set! speaker (speak-it))
+  (enq! q local-func-gets-break)
+  (start-agenda (make-agenda #:queue q)
+                #:stop-condition (true-after-n-times 10))
+  (test-assert (speaker)
+               '("Time for exception fun!\n"
+                 "Here we go...\n"
+                 "in here now!\n"
+                 "Well that was fun :)\n")))
+
+(let ((q (make-q)))
+  (set! speaker (speak-it))
+  (enq! q (wrap (local-func-gets-break #:with-indirection #t)))
+  (start-agenda (make-agenda #:queue q)
+                #:stop-condition (true-after-n-times 10))
+  (test-assert (speaker)
+               '("Time for exception fun!\n"
+                 "bebop\n"
+                 "Here we go...\n"
+                 "in here now!\n"
+                 "Well that was fun :)\n")))
+
+;; Make sure catching tools work
+
+(let ((speaker (speak-it))
+      (catch-result #f))
+  (catch-8sync
+   (begin
+     (speaker "hello")
+     (throw '%8sync-caught-error
+            'my-orig-key '(apple orange banana) '(*fake-stack* *fake-stack* *fake-stack*))
+     (speaker "no goodbyes"))
+   ('some-key
+    (lambda (stacks . rest)
+      (speaker "should not happen")))
+   ('my-orig-key
+    (lambda (stacks fruit1 fruit2 fruit3)
+      (set! catch-result
+            `((fruit1 ,fruit1)
+              (fruit2 ,fruit2)
+              (fruit3 ,fruit3))))))
+  (test-equal (speaker) '("hello"))
+  (test-equal catch-result '((fruit1 apple)
+                             (fruit2 orange)
+                             (fruit3 banana))))
+
 ;; End tests
 
-(test-end "tests-agenda")
-;; (test-exit)
+(test-end "test-agenda")
+
+;; @@: A better way to handle this at the repl?
+(test-exit)