diff options
author | rekado <rekado@elephly.net> | 2014-12-16 08:42:21 +0100 |
---|---|---|
committer | rekado <rekado@elephly.net> | 2014-12-16 08:42:21 +0100 |
commit | 6feadbd3d86532e0eec51423452f9e29fa369ddc (patch) | |
tree | 5729a660d428c82d60cc2203dad968ef9b30b5e1 | |
parent | 0012f9809af22a0b3d6774059e718005e63fb464 (diff) |
test (and fix) run-handler-loop
-rw-r--r-- | spec/xmpp.scm | 22 | ||||
-rw-r--r-- | xmpp.scm | 23 |
2 files changed, 34 insertions, 11 deletions
diff --git a/spec/xmpp.scm b/spec/xmpp.scm index cf61e7c..8a945d1 100644 --- a/spec/xmpp.scm +++ b/spec/xmpp.scm @@ -137,4 +137,24 @@ (suite "handle-stanza" (tests)) (suite "register-temp-stanza-handler-for-id" (tests)) (suite "register-stanza-handler" (tests)) -(suite "run-handler-loop" (tests)) + +(suite "run-handler-loop" + (tests + (test "does not die when a handler throws an exception" e + (assert-false + (error? (begin + (register-stanza-handler + (lambda _ (throw 'some-error "no reason"))) + (call-with-input-string + "<iq from='test' id='123'/><iq from='someone' id='234'/>" + run-handler-loop))))) + (test "processes all stanzas in the queue" e + (assert-equal 2 + (let ((count 0)) + (register-stanza-handler + (lambda _ (begin (set! count (1+ count)) + (throw 'some-error "no reason")))) + (call-with-input-string + "<iq from='test' id='123'/><iq from='someone' id='234'/>" + run-handler-loop) + count))))) @@ -63,21 +63,24 @@ is unregistered." ;; return whatever argument was passed to 'halt (car args))))) -(define (run-handler-loop port) - "Repeatedly check for incoming stanzas. When a stanza handler exists it is executed in parallel. A handler may register new handlers." - (catch #t - (lambda () - (while (not (port-closed? port)) +(define* (run-handler-loop port #:optional (log-port #t)) + "Repeatedly check for incoming stanzas. When a stanza handler +exists it is executed in parallel. A handler may register new +handlers." + (while (not (or (port-closed? port) + (eof-object? (peek-char port)))) + (catch #t + (lambda _ (let ((stanza (xml->sxml port #:trim-whitespace? #t))) - (format #t "received a stanza: ~s\n" stanza) + (format log-port "received a stanza: ~s\n" stanza) ;; TODO: handle the stanza in a non-blocking manner! (handle-stanza stanza) ;; no tight loop, please (sleep 1))) - (display "port was closed.\n")) - (lambda (key . args) - (format #t "ERROR: in handler loop: ~a ~a\n" key args) - key))) + (lambda (key . args) + (format log-port "ERROR: in handler loop: ~a ~a\n" key args) + key))) + (format log-port "port was closed.\n")) (define next-stanza-id! |