kernels.scm 4.88 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
;;; Guix-kernel -- Guix kernel for Jupyter
;;; Copyright (C) 2019 Inria
;;;
;;; This program is free software: you can redistribute it and/or modify
;;; it under the terms of the GNU General Public License as published by
;;; the Free Software Foundation, either version 3 of the License, or
;;; (at your option) any later version.
;;;
;;; This program is distributed in the hope that it will be useful,
;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;; GNU General Public License for more details.
;;;
;;; You should have received a copy of the GNU General Public License
;;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

(define-module (tests kernels)
  #:use-module (jupyter kernels)
  #:use-module (jupyter messages)
  #:use-module (simple-zmq)
  #:use-module (json)
  #:use-module (rnrs bytevectors)
23
  #:use-module (srfi srfi-1)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
  #:use-module (srfi srfi-64))

(define %context
  (zmq-create-context))

(define %kernel-key "secretkey")

(define %kernel #f)

(test-begin "kernels")

;; These tests require the "python3" kernel provided by ipykernel.
(unless (find-kernel-by-name "python3") (test-skip 1))

(test-assert "run-kernel python3"
39
  (let ((kernel (run-kernel %context "python3" %kernel-key)))
40
41
    (set! %kernel kernel)
    (and (kernel? (pk 'kernel kernel))
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
         (begin
           (kill (kernel-pid kernel) 0)

           ;; FIXME: For some reason, we don't always get a "starting" status
           ;; message.  Thus, don't fail if we don't receive it.
           (let ((greetings (read-message %kernel 3000)))
             (or (not greetings)
                 (list (message-type greetings)
                       (hash-ref (json-string->scm
                                  (message-content greetings))
                                 "execution_state"))))))))

(unless %kernel (test-skip 1))
(test-equal "kernel_info_request"
  "python"
  (let ((request (message (header "kernel_info_request" "luser" "12345")
                          "{}")))
    (send-message %kernel request)
    (let* ((reply1  (read-message %kernel))
           (reply2  (read-message %kernel))
           (reply3  (read-message %kernel))
           (replies (pk 'replies (list reply1 reply2 reply3))))
      (and (every message? replies)

           (string=? "status" (message-type reply1))
           (string=? "busy"
                     (hash-ref (json-string->scm (message-content reply1))
                               "execution_state"))
           (equal? (message-parent-header reply1) (message-header request))

           (string=? "kernel_info_reply" (message-type reply2))
           (equal? (message-parent-header reply2) (message-header request))

           (string=? "status" (message-type reply3))
           (string=? "idle"
                     (hash-ref (json-string->scm (message-content reply3))
                               "execution_state"))
           (equal? (message-parent-header reply3) (message-header request))

           (hash-ref (hash-ref
                      (json-string->scm (message-content reply2))
                      "language_info")
                     "name")))))
85
86

(unless %kernel (test-skip 1))
87
88
(test-equal "execute_request"
  42
89
90
91
92
  (let ((request (message (header "execute_request" "luser" "12345")
                          (scm->json-string
                           '((code . "40 + 2\n")
                             (silent . #f))))))
93
    (send-message %kernel request)
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

    ;; As noted at
    ;; <https://jupyter-client.readthedocs.io/en/latest/messaging.html#request-reply>,
    ;; in the request-reply pattern, we first receive on the iopub socket a
    ;; "status" message, then our input is broadcast on iopub, and then we
    ;; get the result.
    (let* ((reply1  (read-message %kernel))
           (reply2  (read-message %kernel))
           (reply3  (read-message %kernel))
           (replies (pk 'replies (list reply1 reply2 reply3))))
      (and (every message? replies)
           (every (compose bytevector? message-sender) replies)

           (string=? "status" (message-type reply1))
           (string=? "busy"
                     (hash-ref (json-string->scm (message-content reply1))
                               "execution_state"))
           (equal? (message-parent-header reply1) (message-header request))

           (string=? "execute_input" (message-type reply2))
           (equal? (message-parent-header reply2) (message-header request))

           (string=? (message-type reply3) "execute_result")
           (equal? (message-parent-header reply3) (message-header request))
           (let* ((content (json-string->scm (message-content reply3)))
                  (data    (hash-ref content "data"))
                  (text    (hash-ref data "text/plain")))
             (string->number text))))))
122
123
124
125
126
127

(when %kernel
  (close-kernel %kernel)
  (kill (kernel-pid %kernel) 15))

(test-end "kernels")