Update tests

This commit is contained in:
Nathaniel Nicandro 2017-12-16 19:01:09 -06:00
parent 80de6579fa
commit 3d2aa1fe9e

View file

@ -1,7 +1,6 @@
;; -*- lexical-binding: t -*- ;; -*- lexical-binding: t -*-
(require 'jupyter-client) (require 'jupyter-client)
(require 'jupyter-messages)
(require 'cl-lib) (require 'cl-lib)
(require 'ert) (require 'ert)
@ -147,59 +146,66 @@
(mapc (lambda (se) (zmq-close (car se))) sock-endpoint)))) (mapc (lambda (se) (zmq-close (car se))) sock-endpoint))))
(ert-deftest jupyter-message-callbacks () (ert-deftest jupyter-message-callbacks ()
(let ((client (jupyter-kernel-client-using-kernel "python"))) (with-zmq-context
(jupyter-start-channels client) (let ((client (jupyter-kernel-client-using-kernel "python")))
;; Let channels start ;; FIXME: Let the kernel startup before connecting, this is necessary
(sleep-for 1) ;; since we just start a kernel using "jupyter console" at the moment
(unwind-protect ;; which does some initial handshaking with the kernel.
(progn ;;
(ert-info ("Invalid message type") ;; Actually the problem seems to be my use of multiple processes to poll
(should-error ;; for events. Because I have a subprocess per socket, the order of
(jupyter-add-receive-callback ;; received messages is not gauranteed although the kernel sends the
client 'exe-res (jupyter-send-execute client :code "y = 1+2\ny") ;; messages in a certain order. What I really should probably do is poll
(lambda (msg) (+ 1 2))))) ;; all sockets in a single subprocess.
(ert-info ("Execute on correct message ID") (jupyter-start-channels client)
(let ((id (jupyter-send-kernel-info client)) ;; Let the channels start
(recv-id nil)) (unwind-protect
(jupyter-send-kernel-info client) (progn
(jupyter-add-receive-callback client 'kernel-info-reply id (ert-info ("Waiting for messages")
(lambda (msg) (should-not
(setq recv-id (plist-get (plist-get msg :parent_header) (null (jupyter-wait-until-idle
:msg_id)))) client (jupyter-request-kernel-info client)))))
;; Receive messages from kernel ;; (ert-info ("Callbacks are removed when an idle message is received")
(sleep-for 1) ;; (let ((id (jupyter-request-kernel-info client)))
(should (equal id recv-id)))) ;; (jupyter-add-receive-callback client 'kernel-info-reply id
(ert-info ("Multiple callbacks on the same message") ;; (lambda (msg) 'foo))
(let ((id (jupyter-send-execute client :code "y = 1+2\ny")) ;; (should-not (null (gethash id (oref client message-callbacks))))
(msg-res nil) ;; (jupyter-wait-until-idle client id 2)
(msg-rep nil)) ;; (should (null (gethash id (oref client message-callbacks))))))
(jupyter-add-receive-callback client 'execute-result id (ert-info ("Message callbacks receive the right messages")
(lambda (msg) (setq msg-res msg))) (ert-info ("Callbacks fire on the right message ID")
(jupyter-add-receive-callback client 'execute-reply id (let ((id (jupyter-request-kernel-info client))
(lambda (msg) (setq msg-rep msg))) (recv-id nil))
;; Receive messages from kernel (jupyter-add-receive-callback client 'kernel-info-reply id
(sleep-for 1) (lambda (msg) (setq recv-id (jupyter-message-parent-id msg))))
(should-not (null msg-res)) (jupyter-wait-until-idle client id)
(should-not (null msg-rep)) (should (equal recv-id id))))
;; execute_result (ert-info ("Different message type, same message ID")
(should (equal id (plist-get (plist-get msg-res :parent_header) (let ((id (jupyter-request-execute client :code "y = 1+2\ny"))
:msg_id))) (msg-res nil)
(should (equal (plist-get msg-res :msg_type) "execute_result")) (msg-rep nil))
(cl-destructuring-bind (&key data &allow-other-keys) (jupyter-add-receive-callback client 'execute-result id
(plist-get msg-res :content) (lambda (msg) (setq msg-res msg)))
(should (equal (plist-get data :text/plain) "3"))) (jupyter-add-receive-callback client 'execute-reply id
;; execute_reply (lambda (msg) (setq msg-rep msg)))
(should (equal id (plist-get (plist-get msg-rep :parent_header) (should-not (null (jupyter-wait-until-idle client id)))
:msg_id))) (should (json-plist-p msg-res))
(should (equal (plist-get msg-rep :msg_type) "execute_reply"))))) (should (json-plist-p msg-rep))
(jupyter-stop-channels client) (ert-info ("Verify received contents")
(delete-process (oref client kernel)) ;; execute_result
(kill-buffer (process-buffer (oref client kernel)))))) (should (equal id (jupyter-message-parent-id msg-res)))
(should (equal (plist-get msg-res :msg_type) "execute_result"))
;; execute_reply
(should (equal id (jupyter-message-parent-id msg-rep)))
(should (equal (plist-get msg-rep :msg_type) "execute_reply")))))))
(jupyter-stop-channels client)
(delete-process (oref client kernel))
(kill-buffer (process-buffer (oref client kernel)))))))
(ert-deftest jupyter-processing-messages () (ert-deftest jupyter-processing-messages ()
(setq client (jupyter-kernel-client-using-kernel "python")) (setq client (jupyter-kernel-client-using-kernel "python"))
(jupyter-start-channels client) (jupyter-start-channels client)
;; TODO: Better interface for this. It looks like I will have to handle ;; TODO: Better interface for this. It looks like I will have to handle
;; certain messages specially. For a shutdown message, I will also have to ;; certain messages specially. For a shutdown message, I will also have to
;; delete the process as is done here. Also, I don't like these receive ;; delete the process as is done here. Also, I don't like these receive
@ -209,7 +215,7 @@
;; hangs when using `jupyter-wait-until-received'. ;; hangs when using `jupyter-wait-until-received'.
;; ;;
;; TODO: `jupyter-shutdown' seems ambiguous. Prefix the sending messages like ;; TODO: `jupyter-shutdown' seems ambiguous. Prefix the sending messages like
;; `jupyter-send-shutdown', `jupyter-send-execute', ... ;; `jupyter-request-shutdown', `jupyter-request-execute', ...
(lexical-let ((proc (oref client kernel)) (lexical-let ((proc (oref client kernel))
(id (jupyter-shutdown client))) (id (jupyter-shutdown client)))
(jupyter-add-receive-callback (jupyter-add-receive-callback