Add some early error handling tests
[8sync.git] / tests / test-agenda.scm
index e3e12c59a5491d2f09d4aae67a27fc265f66cd42..8975b3e3f1e28631088ef593da9d5445dc2539cb 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
 
 
 ;; ... 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 (local-func-gets-break)
+  (speaker "Time for exception fun!\n")
+  (let ((caught-exception #f))
+    (catch '%8sync-caught-error
+      (lambda ()
+        (%8sync (%run (remote-func-breaks))))
+      (lambda (_ orig-key orig-args orig-stack)
+        (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 (stack? orig-stack)))))
+  (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")))
+
 ;; End tests
 
-(test-end "tests-agenda")
-;; (test-exit)
+(test-end "test-agenda")
+
+(test-exit)