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