/
jupyter-test.el
2926 lines (2764 loc) · 109 KB
/
jupyter-test.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
85
86
87
88
89
90
91
92
93
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; jupyter-test.el --- Jupyter tests -*- lexical-binding: t -*-
;; Copyright (C) 2018-2020 Nathaniel Nicandro
;; Author: Nathaniel Nicandro <nathanielnicandro@gmail.com>
;; Created: 08 Jan 2018
;; 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, 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;
;;; Code:
(require 'zmq)
(require 'jupyter-env)
(require 'jupyter-client)
(require 'jupyter-repl)
(require 'jupyter-org-client)
(require 'jupyter-kernel-manager)
(require 'cl-lib)
(require 'ert)
(require 'subr-x) ; string-trim
(declare-function org-babel-python-table-or-string "ob-python" (results))
;; TODO: Required tests
;; - Jupyter REPL
;;; Mock
(ert-deftest jupyter-echo-client ()
:tags '(mock)
(jupyter-with-echo-client client
(ert-info ("Mock echo client echo's messages back to channel.")
(let* ((msg (jupyter-message-execute-request :code "foo"))
(req (jupyter-send client :shell :execute-request msg)))
(sleep-for 0.3)
(setq msgs (nreverse (ring-elements (oref client messages))))
(should (= (length msgs) 3))
(should (equal (jupyter-message-type (car msgs)) :status))
(should (equal (jupyter-message-parent-id (car msgs))
(jupyter-request-id req)))
(should (equal (jupyter-message-get (car msgs) :execution_state) "busy"))
(should (equal (jupyter-message-type (cadr msgs)) :execute-reply))
(should (equal (jupyter-message-parent-id (cadr msgs))
(jupyter-request-id req)))
(should (equal (jupyter-message-content (cadr msgs))
(plist-get (jupyter-test-message (jupyter-request) nil msg) :content)))
(should (equal (jupyter-message-type (caddr msgs)) :status))
(should (equal (jupyter-message-parent-id (caddr msgs))
(jupyter-request-id req)))
(should (equal (jupyter-message-get (caddr msgs) :execution_state) "idle"))))))
;;;; Comm layer
(ert-deftest jupyter-comm-layer ()
:tags '(mock comm)
(let ((comm (jupyter-mock-comm-layer))
(obj (make-jupyter-mock-comm-obj)))
(jupyter-comm-add-handler comm obj)
(should (= (length (oref comm handlers)) 1))
(should (eq (jupyter-weak-ref-resolve (car (oref comm handlers))) obj))
(should (= (oref comm alive) 1))
(jupyter-comm-add-handler comm obj)
(should (= (length (oref comm handlers)) 1))
(should (eq (jupyter-weak-ref-resolve (car (oref comm handlers))) obj))
(should (= (oref comm alive) 1))
(should-not (jupyter-mock-comm-obj-event obj))
(jupyter-event-handler comm '(event))
;; Events are handled in a timer, not right away
(sleep-for 0.01)
(should (equal (jupyter-mock-comm-obj-event obj) '(event)))
(jupyter-comm-remove-handler comm obj)
(should (= (length (oref comm handlers)) 0))
(should-not (oref comm alive))
(jupyter-comm-remove-handler comm obj)))
;;; Callbacks
(ert-deftest jupyter-wait-until-idle ()
:tags '(callbacks)
(jupyter-with-echo-client client
(let ((req (jupyter-send-execute-request client :code "foo")))
(ert-info ("Blocking callbacks")
(jupyter-wait-until-idle req)
(should (jupyter-request-idle-received-p req)))
(ert-info ("Error after idle message has been received")
(should-error (jupyter-add-callback req :status #'identity))))))
(ert-deftest jupyter-callbacks ()
:tags '(callbacks)
(jupyter-with-echo-client client
(ert-info ("Callbacks called on the right message types")
(let* ((callback-count 0)
(cb (lambda (msg)
(should (eq (jupyter-message-type msg) :status))
(setq callback-count (1+ callback-count))))
(req (jupyter-send-execute-request client :code "foo")))
(jupyter-add-callback req :status cb)
(jupyter-wait-until-idle req)
(should (= callback-count 2))))
(ert-info ("Adding callbacks, message type list")
(let* ((callback-count 0)
(cb (lambda (msg)
(setq callback-count (1+ callback-count))
(should (memq (jupyter-message-type msg)
'(:status :execute-reply)))))
(req (jupyter-send-execute-request client :code "foo")))
(jupyter-add-callback req '(:status :execute-reply) cb)
(jupyter-wait-until-idle req)
(should (= callback-count 3))))))
;;; `jupyter-insert'
(ert-deftest jupyter-map-mime-bundle ()
:tags '(mime)
(let ((res (current-time))
(content
'(:data
(:text/plain
1 :text/html 2
:text/latex 3)
:metadata
(:text/plain
4 :text/html 5
:text/latex 6))))
(should
(eq
res
(jupyter-map-mime-bundle
'(:text/html :text/latex :text/plain)
content
(lambda (mime content)
(when (eq mime :text/plain)
(should (= (plist-get content :data) 1))
(should (= (plist-get content :metadata) 4))
res)))))
(let (called)
(should-not
(eq
res
(jupyter-map-mime-bundle
'(:text/html :text/latex :mime-without-data) content
(lambda (mime content)
(pcase mime
(:text/latex
(should (= (plist-get content :data) 3))
(should (= (plist-get content :metadata) 6))
(setq called t)
nil)
(:mime-without-data res)
(_ nil))))))
(should called))))
(defvar jupyter-nongraphic-mime-types)
(ert-deftest jupyter-insert ()
"Test the `jupyter-insert' method."
:tags '(mime)
(with-temp-buffer
(let ((msg (list :data (list :text/plain "foo")
:metadata nil)))
(ert-info ("Return value is the mimetype inserted")
(should (eq (jupyter-insert msg) :text/plain))
(should (equal (buffer-string) "foo\n"))
(erase-buffer))
(ert-info ("Return nil on invalid mimetype")
(should-not (jupyter-insert :text/foo "bar"))
(should-not (jupyter-insert (list :data (list :text/foo "bar")))))
(ert-info ("Calling with data plist directly")
(should (eq (jupyter-insert (plist-get msg :data)) :text/plain))
(should (equal (buffer-string) "foo\n"))
(erase-buffer))
(ert-info ("Calling with message plist directly")
(should (eq (jupyter-insert msg) :text/plain))
(should (equal (buffer-string) "foo\n"))
(erase-buffer)))
(let ((msg (list :data (list :text/plain "foo"
:text/html "<b>bar</b>")
:metadata nil)))
(ert-info ("Mimetype priority")
(should (eq (jupyter-insert msg) :text/html))
(should (equal (string-trim (buffer-string)) "bar"))
(erase-buffer)))
(let ((data (list :image/jpeg (base64-encode-string "kjdaljk")))
;; So that this test runs under ert-runner
(jupyter-nongraphic-mime-types jupyter-mime-types))
(ert-info ("The right method specializers are called")
(cl-letf (((symbol-function #'jupyter-insert-image)
(lambda (data &rest _) (insert data))))
(cl-letf (((symbol-function #'image-type-available-p)
(lambda (_typ) nil)))
(should-not (jupyter-insert data)))
(cl-letf (((symbol-function #'image-type-available-p)
(lambda (typ) (eq typ 'jpeg))))
(should (eq (jupyter-insert data) :image/jpeg)))
(should (equal (buffer-string) "kjdaljk\n"))
(erase-buffer))))))
(defun jupyter-test-display-id-all (id beg end)
(not (text-property-not-all beg end 'jupyter-display id)))
(ert-deftest jupyter-insert-with-ids ()
:tags '(mime display-id)
(with-temp-buffer
(let ((id "1")
(msg (list :data (list :text/plain "foo"))))
(ert-info ("`jupyter-display-ids' initialization")
(should-not jupyter-display-ids)
(should (eq (jupyter-insert id msg) :text/plain))
(should (hash-table-p jupyter-display-ids))
(should (equal (buffer-string) "foo\n")))
(ert-info ("`jupyter-display-ids' is updated with ID")
(should (not (null (gethash id jupyter-display-ids)))))
(ert-info ("IDs are `eq'")
;; This is done so that they are comparable as text properties.
(should (eq (gethash id jupyter-display-ids) id)))
(ert-info ("Text property added to inserted text")
(should (jupyter-test-display-id-all id (point-min) (point-max)))))))
(ert-deftest jupyter-delete-current-display ()
:tags '(mime display-id)
(with-temp-buffer
(let ((id1 "1")
(id2 "2")
(msg (list :data (list :text/plain "foo"))))
(ert-info ("Actually deletes text with display ID")
(jupyter-insert id1 msg)
(should (equal (buffer-string) "foo\n"))
(goto-char (point-min))
(jupyter-delete-current-display)
(should (= (point-min) (point-max))))
(ert-info ("Does not do anything if no display ID at point")
(insert "bar")
(goto-char (point-min))
(jupyter-delete-current-display)
(should (equal (buffer-string) "bar"))
(erase-buffer))
(ert-info ("Deletes only text with the same display ID")
(jupyter-insert id1 msg)
(jupyter-insert id2 msg)
(goto-char (point-min))
(jupyter-delete-current-display)
(should (equal (buffer-string) "foo\n"))
(should (jupyter-test-display-id-all id2 (point-min) (point-max)))
(erase-buffer)))))
(ert-deftest jupyter-update-display ()
:tags '(mime display-id)
(with-temp-buffer
(let ((id1 "1")
(id2 "2")
(msg1 (list :data (list :text/plain "foo")))
(msg2 (list :data (list :text/plain "bar"))))
(ert-info ("Text with matching display ID is actually updated")
(jupyter-insert id1 msg1)
(jupyter-insert id2 msg2)
(should (equal (buffer-string) "foo\nbar\n"))
(should (jupyter-test-display-id-all
id1 (point-min) (+ 4 (point-min))))
(should (jupyter-test-display-id-all
id2 (- (point-max) 4) (point-max)))
(jupyter-update-display "1" (list :data (list :text/plain "baz")))
(should (equal (buffer-string) "baz\nbar\n"))
(should (jupyter-test-display-id-all
id1 (point-min) (+ 4 (point-min))))
(should (jupyter-test-display-id-all
id2 (- (point-max) 4) (point-max)))
(erase-buffer))
(ert-info ("All displays are updated")
(jupyter-insert id1 msg1)
(jupyter-insert id1 msg1)
(pop-to-buffer (current-buffer))
(should (equal (buffer-string) "foo\nfoo\n"))
(should (jupyter-test-display-id-all
id1 (point-min) (point-max)))
(jupyter-update-display "1" (list :data (list :text/plain "baz")))
(should (equal (buffer-string) "baz\nbaz\n"))
(should (jupyter-test-display-id-all
id1 (point-min) (point-max)))))))
(ert-deftest jupyter-insert-html ()
:tags '(mime)
(ert-info ("Correct libxml parser is called depending on prolog")
(ert-info ("XML prolog means to parse as XML")
(with-temp-buffer
(cl-letf* ((xml-parser-called nil)
((symbol-function #'libxml-parse-xml-region)
(lambda (&rest _)
(prog1 nil
(setq xml-parser-called t)))))
(jupyter-insert :text/html "<?xml version=\"1.0\" encoding=\"UTF-8\"?><p>hello</p>")
(should xml-parser-called))))
(ert-info ("Parse as html")
(with-temp-buffer
(cl-letf* ((html-parser-called nil)
((symbol-function #'libxml-parse-html-region)
(lambda (&rest _)
(prog1 nil
(setq html-parser-called t)))))
(jupyter-insert :text/html "<p>hello</p>")
(should html-parser-called))))))
(ert-deftest jupyter-with-display-buffer ()
:tags '(buffers)
(jupyter-with-display-buffer "foo" t)
(jupyter-with-display-buffer "foo" nil
(should (= jupyter-display-buffer-marker (point-min)))
(insert "12345")
(should (= jupyter-display-buffer-marker (point-max)))
(goto-char (point-min)))
(jupyter-with-display-buffer "foo" nil
(should (= (point) (point-max)))
(should (= jupyter-display-buffer-marker (point-max)))
(insert "foobar")
(should (= jupyter-display-buffer-marker (point-max)))
(should (equal (buffer-string) "12345foobar")))
(jupyter-with-display-buffer "foo" t
(should (equal (buffer-string) ""))
(should (= (point-min) (point-max)))
(should (= jupyter-display-buffer-marker (point-min)))))
(ert-deftest jupyter-with-contol-code-handling ()
:tags '(buffers)
(jupyter-with-display-buffer "foo" t)
(jupyter-with-display-buffer "foo" nil
(insert "foo\r"))
(jupyter-with-display-buffer "foo" nil
(should (equal (buffer-string) "foo\r"))
(jupyter-test-text-has-property 'invisible t '(4))
(insert "foo\r"))
(jupyter-with-display-buffer "foo" nil
(should (equal (buffer-string) "foo\r"))
(insert "bar\r\nbaz\rfoo"))
(jupyter-with-display-buffer "foo" nil
(should (equal (buffer-string) "bar\nfoo"))))
;;; Messages
(ert-deftest jupyter-message-identities ()
:tags '(messages)
(let ((msg (list "123" "323" jupyter-message-delimiter
"msg1" "msg2" "\0\0")))
(should (equal (jupyter--split-identities msg)
(cons (list "123" "323")
(list "msg1" "msg2" "\0\0"))))
(setq msg (list "123" "No" "delim" "in" "message"))
(should-error (jupyter--split-identities msg))))
(ert-deftest jupyter-message-headers ()
:tags '(messages)
(let* ((session (jupyter-session :key (jupyter-new-uuid)))
(id (jupyter-new-uuid))
(header (jupyter--message-header session :input-reply id)))
(should (plist-get header :msg_id))
(should (plist-get header :date))
(should (eq (plist-get header :msg_type) :input-reply))
(should (string= (plist-get header :version) jupyter-protocol-version))
(should (string= (plist-get header :username) user-login-name))
(should (string= (plist-get header :session) (jupyter-session-id session)))))
(ert-deftest jupyter-message-time ()
:tags '(messages)
(let ((tz (getenv "TZ")))
(setenv "TZ" "UTC0")
(should (equal (jupyter-encode-time '(23385 27704 100000))
"2018-07-26T06:37:44.100000"))
(should (equal (jupyter-decode-time "2018-07-26T01:37:44.100")
'(23385 9704 100000 0)))
(should (equal (jupyter-decode-time "2018-07-26T01:37:44.10011122")
'(23385 9704 100111 0)))
(should (equal (jupyter-decode-time "2018-07-26T01:37:44")
'(23385 9704 0 0)))
(should (equal (jupyter-decode-time "2018-07-26")
'(23385 3840 0 0)))
(setenv "TZ" tz)))
(ert-deftest jupyter-message-signing ()
:tags '(messages)
(let ((session (jupyter-session :key "foo"))
(msg (list "" "{\"msg_id\":\"1\",\"msg_type\":\"execute_reply\"}" "{}" "{}" "{}"))
(signature "f9080fb30e80a1b424895b557b8249157d5f83d6fc897cb96a4d2fa54a1280e6"))
(should (equal signature
(jupyter-sign-message session msg #'jupyter-hmac-sha256)))))
(ert-deftest jupyter-message-decoding ()
:tags '(messages)
(let ((session (jupyter-session)))
(ert-info ("Minimum message length")
(should-error (jupyter-decode-message session (list "1" "2" "3"))))
(ert-info ("Form of decoded message")
(let* ((session (jupyter-session :key "foo"))
(msg (list "f9080fb30e80a1b424895b557b8249157d5f83d6fc897cb96a4d2fa54a1280e6"
"{\"msg_id\":\"1\",\"msg_type\":\"execute_reply\"}" "{}" "{}" "{}"))
(plist (jupyter-decode-message session msg)))
(cl-loop
with true-msg = (list
:header '(message-part
"{\"msg_id\":\"1\",\"msg_type\":\"execute_reply\"}"
(:msg_id "1" :msg_type :execute-reply))
:msg_id "1"
:msg_type :execute-reply
:parent_header '(message-part "{}" nil)
:content '(message-part "{}" nil)
:metadata '(message-part "{}" nil)
:buffers nil)
for key in '(:header :msg_id :msg_type :content
:parent_header :metadata :buffers)
do (should (equal (plist-get true-msg key) (plist-get plist key))))))))
(ert-deftest jupyter-message-encoding ()
:tags '(messages)
;; TODO
)
(ert-deftest jupyter-message-types ()
:tags '(client messages)
(jupyter-test-with-python-client client
(ert-info ("Kernel info")
(let ((res (jupyter-wait-until-received :kernel-info-reply
(jupyter-send-kernel-info-request client))))
(should res)
(should (json-plist-p res))
(should (eq (jupyter-message-type res) :kernel-info-reply))))
(ert-info ("Comm info")
(let ((res (jupyter-wait-until-received :comm-info-reply
(jupyter-send-comm-info-request client))))
(should-not (null res))
(should (json-plist-p res))
(should (eq (jupyter-message-type res) :comm-info-reply))))
(ert-info ("Execute")
(let ((res (jupyter-wait-until-received :execute-reply
(jupyter-send-execute-request client :code "y = 1 + 2"))))
(should-not (null res))
(should (json-plist-p res))
(should (eq (jupyter-message-type res) :execute-reply))))
(ert-info ("Input")
(cl-letf (((symbol-function 'read-from-minibuffer)
(lambda (_prompt &rest _args) "foo")))
(let ((res (jupyter-wait-until-received :execute-result
(jupyter-send-execute-request client :code "input('')"))))
(should-not (null res))
(should (json-plist-p res))
(should (eq (jupyter-message-type res) :execute-result))
(should (equal (jupyter-message-data res :text/plain) "'foo'")))))
(ert-info ("Inspect")
(let ((res (jupyter-wait-until-received :inspect-reply
(jupyter-send-inspect-request client
:code "list((1, 2, 3))"
:pos 2
:detail 0))))
(should-not (null res))
(should (json-plist-p res))
(should (eq (jupyter-message-type res) :inspect-reply))))
(ert-info ("Complete")
(let ((res (jupyter-wait-until-received :complete-reply
(jupyter-send-complete-request client
:code "foo = lis"
:pos 8))))
(should-not (null res))
(should (json-plist-p res))
(should (eq (jupyter-message-type res) :complete-reply))))
(ert-info ("History")
(let ((res (jupyter-wait-until-received :history-reply
(jupyter-send-history-request client
:hist-access-type "tail" :n 2))))
(should-not (null res))
(should (json-plist-p res))
(should (eq (jupyter-message-type res) :history-reply))))
(ert-info ("Is Complete")
(let ((res (jupyter-wait-until-received :is-complete-reply
(jupyter-send-is-complete-request client
:code "for i in range(5):"))))
(should-not (null res))
(should (json-plist-p res))
(should (eq (jupyter-message-type res) :is-complete-reply))))
(ert-info ("Shutdown")
(let ((res (jupyter-wait-until-received :shutdown-reply
(jupyter-send-shutdown-request client))))
(should-not (null res))
(should (json-plist-p res))
(should (eq (jupyter-message-type res) :shutdown-reply))
;; Ensure we give the kernel process time to die off
(when (oref client manager)
(jupyter-with-timeout (nil jupyter-long-timeout)
(not (jupyter-kernel-alive-p (oref client manager)))))))))
(ert-deftest jupyter-message-lambda ()
:tags '(messages)
(let ((msg (jupyter-test-message
(jupyter-request) :execute-reply
(list :status "idle" :data (list :text/plain "foo")))))
(should (equal (funcall (jupyter-message-lambda (status)
status)
msg)
"idle"))
(should (equal (funcall (jupyter-message-lambda ((res text/plain))
res)
msg)
"foo"))
(should (equal (funcall (jupyter-message-lambda (status (res text/plain))
(cons status res))
msg)
(cons "idle" "foo")))))
;;; Channels
(ert-deftest jupyter-zmq-channel ()
:tags '(channels zmq)
(let* ((port (car (jupyter-available-local-ports 1)))
(channel (jupyter-zmq-channel
:type :shell
:endpoint (format "tcp://127.0.0.1:%s" port))))
(ert-info ("Starting the channel")
(should-not (jupyter-channel-alive-p channel))
(jupyter-start-channel channel :identity "foo")
(should (jupyter-channel-alive-p channel))
(should (equal (zmq-socket-get (oref channel socket)
zmq-ROUTING-ID)
"foo")))
(ert-info ("Stopping the channel")
(let ((sock (oref channel socket)))
(jupyter-stop-channel channel)
(should-not (jupyter-channel-alive-p channel))
;; Ensure the socket was disconnected
(should-error (zmq-send sock "foo" zmq-NOBLOCK) :type 'zmq-EAGAIN)))))
(ert-deftest jupyter-hb-channel ()
:tags '(channels)
(should (eq (oref (jupyter-hb-channel) type) :hb))
(let* ((port (car (jupyter-available-local-ports 1)))
(channel (jupyter-hb-channel
:endpoint (format "tcp://127.0.0.1:%s" port)
:session (jupyter-session)))
(died-cb-called nil)
(jupyter-hb-max-failures 1))
(oset channel time-to-dead 0.1)
(should-not (jupyter-channel-alive-p channel))
(should-not (jupyter-hb-beating-p channel))
(should (oref channel paused))
(oset channel beating t)
(jupyter-start-channel channel)
(jupyter-hb-on-kernel-dead channel (lambda () (setq died-cb-called t)))
(should (jupyter-channel-alive-p channel))
;; `jupyter-hb-unpause' needs to explicitly called
(should (oref channel paused))
(jupyter-hb-unpause channel)
(sleep-for 0.2)
;; It seems the timers are run after returning from the first `sleep-for'
;; call.
(sleep-for 0.1)
(should (oref channel paused))
(should-not (oref channel beating))
(should died-cb-called)
(should (jupyter-channel-alive-p channel))
(should-not (jupyter-hb-beating-p channel))))
;;; GC
(ert-deftest jupyter-weak-ref ()
:tags '(gc)
(let (ref)
(let ((obj (list 1)))
(setq ref (jupyter-weak-ref obj)))
(let ((table (make-hash-table)))
(dotimes (_ gc-cons-threshold)
(puthash (random) t table)))
(garbage-collect)
(garbage-collect)
(garbage-collect)
(should-not (jupyter-weak-ref-resolve ref))))
(defclass jupyter-test-object (jupyter-finalized-object)
((val)))
(ert-deftest jupyter-add-finalizer ()
:tags '(gc)
(let ((val (list 1)))
(let ((obj (jupyter-test-object)))
(oset obj val val)
(jupyter-add-finalizer obj
(lambda () (setcar (oref obj val) nil))))
(ignore (make-list (* 2 gc-cons-threshold) ?0))
(garbage-collect)
(should (null (car val)))))
;;; Kernel
(ert-deftest jupyter-locate-python ()
:tags '(kernel)
;; TODO: Generalize for Windows
(skip-unless (not (memq system-type '(ms-dos windows-nt cygwin))))
;; Load file name handlers
(ignore (file-remote-p "/ssh:foo:"))
(cl-letf (((symbol-function #'jupyter-command)
(lambda (&rest _)
"{\"data\": [\"/home/USER/.local/share/jupyter\", \
\"/home/USER/.julia/conda/3/share/jupyter\", \
\"/usr/local/share/jupyter\", \
\"/usr/share/jupyter\"]}"))
((symbol-function #'file-exists-p)
(lambda (file)
(member file '("/home/USER/.julia/conda/3/bin/python3"
"/ssh:foo:/usr/local/bin/python3")))))
(should (equal (jupyter-locate-python) "/home/USER/.julia/conda/3/bin/python3"))
(let ((default-directory "/ssh:foo:"))
(should (equal (jupyter-locate-python) "/usr/local/bin/python3"))))
(cl-letf (((symbol-function #'jupyter-command)
(lambda (&rest _)
"{\"foo\": [\"/home/USER/.local/share/jupyter\", \
\"/usr/share/jupyter\"]}")))
(should-error (jupyter-locate-python)))
(cl-letf (((symbol-function #'jupyter-command)
(lambda (&rest _)
"{\"data\": [\"/home/USER/.local/share/jupyter\", \
\"/usr/share/jupyter\"]}"))
((symbol-function #'file-exists-p)
(lambda (_) nil)))
(should-error (jupyter-locate-python))))
(ert-deftest jupyter-kernel-lifetime ()
:tags '(kernel)
(let* ((conn-info (jupyter-local-tcp-conn-info))
(kernel (jupyter-spec-kernel
:spec (jupyter-guess-kernelspec "python")
:session (jupyter-session
:key (plist-get conn-info :key)
:conn-info conn-info))))
(should-not (jupyter-kernel-alive-p kernel))
(jupyter-start-kernel kernel)
(should (jupyter-kernel-alive-p kernel))
(jupyter-kill-kernel kernel)
(should-not (jupyter-kernel-alive-p kernel))
(setq conn-info (jupyter-local-tcp-conn-info))
(ert-info ("`jupyter-kernel-manager'")
;; TODO: Should the manager create a session if one isn't present?
(oset kernel session (jupyter-session
:key (plist-get conn-info :key)
:conn-info conn-info))
(let* ((manager (jupyter-kernel-process-manager :kernel kernel))
(control-channel (oref manager control-channel))
process)
(should-not (jupyter-kernel-alive-p manager))
(should-not control-channel)
(jupyter-start-kernel manager)
(setq process (oref kernel process))
(setq control-channel (oref manager control-channel))
(should (jupyter-zmq-channel-p control-channel))
(should (jupyter-kernel-alive-p manager))
(should (jupyter-kernel-alive-p kernel))
(jupyter-shutdown-kernel manager)
(ert-info ("Kernel shutdown is clean")
(should-not (process-live-p process))
(should (zerop (process-exit-status process)))
(should-not (jupyter-kernel-alive-p manager))
(should-not (jupyter-kernel-alive-p kernel)))
(setq control-channel (oref manager control-channel))
(should-not (jupyter-zmq-channel-p control-channel))))))
(ert-deftest jupyter-command-kernel ()
:tags '(kernel)
(let ((kernel (jupyter-command-kernel
:spec (jupyter-guess-kernelspec "python"))))
(ert-info ("Session set after kernel starts")
(should-not (jupyter-kernel-alive-p kernel))
(jupyter-start-kernel kernel)
(should (jupyter-kernel-alive-p kernel))
(should (oref kernel session))
(jupyter-kill-kernel kernel)
(should-not (jupyter-kernel-alive-p kernel)))
(ert-info ("Can we communicate?")
(let ((manager (jupyter-kernel-process-manager :kernel kernel)))
(jupyter-start-kernel manager)
(unwind-protect
(let ((jupyter-current-client
(jupyter-make-client manager 'jupyter-kernel-client)))
(jupyter-start-channels jupyter-current-client)
(unwind-protect
(progn
(jupyter-wait-until-startup jupyter-current-client)
(should (equal (jupyter-eval "1 + 1") "2")))
(jupyter-stop-channels jupyter-current-client)))
(jupyter-shutdown-kernel manager))))))
;;; Environment
(ert-deftest jupyter-canonicalize-language-string ()
:tags '(base env)
(should (equal (jupyter-canonicalize-language-string "Wolfram Language")
"Wolfram-Language"))
(should (equal (jupyter-canonicalize-language-string "R") "R"))
(should (equal (jupyter-canonicalize-language-string "/gnu/store/python")
"python")))
(ert-deftest jupyter-runtime-directory ()
:tags '(env)
(let (dir-created jupyter-runtime-directory)
(cl-letf (((symbol-function #'jupyter-command)
(lambda (&rest _) "foo"))
((symbol-function #'make-directory)
(lambda (&rest _)
(setq dir-created t))))
(jupyter-runtime-directory)
(should dir-created)
(setq dir-created nil)
(should (equal jupyter-runtime-directory "foo"))
(let ((default-directory "/ssh:foo:/"))
(should (equal (jupyter-runtime-directory) "/ssh:foo:foo"))
(ert-info ("Variable definition is always local")
(setq jupyter-runtime-directory nil)
(jupyter-runtime-directory)
(should (equal jupyter-runtime-directory "foo")))))))
;;; Client
;; TODO: Different values of the session argument
;;
;; FIXME: Re-work after refactoring the kernelspec -> connectable kernel code paths.
(ert-deftest jupyter-comm-initialize ()
:tags '(client init)
(skip-unless nil)
(jupyter-test-with-python-client client
(with-slots (session kcomm) client
(ert-info ("Client session")
(should (string= (jupyter-session-key session)
(plist-get conn-info :key)))
(should (equal (jupyter-session-conn-info session)
conn-info)))
(ert-info ("Heartbeat channel initialized")
(should (eq session (oref (oref kcomm hb) session)))
(should (string= (oref (oref kcomm hb) endpoint)
(format "tcp://127.0.0.1:%d"
(plist-get conn-info :hb_port)))))
(ert-info ("Shell, iopub, stdin initialized")
(cl-loop
for channel in '(:shell :iopub :stdin)
for port_sym = (intern (concat (symbol-name channel) "_port"))
do
(should (plist-member (plist-get channels channel) :alive-p))
(should (plist-member (plist-get channels channel) :endpoint))
(should
(string= (plist-get (plist-get channels channel) :endpoint)
(format "tcp://127.0.0.1:%d"
(plist-get conn-info port_sym))))))
(ert-info ("Initialization stops any running channels")
(should-not (jupyter-channels-running-p client))
(jupyter-start-channels client)
(should (jupyter-channels-running-p client))
(jupyter-comm-initialize client conn-info)
(should-not (jupyter-channels-running-p client)))
(ert-info ("Invalid signature scheme")
(plist-put conn-info :signature_scheme "hmac-foo")
(should-error (jupyter-comm-initialize client conn-info))))))
(ert-deftest jupyter-write-connection-file ()
:tags '(client)
(skip-unless (not (memq system-type '(ms-dos windows-nt cygwin))))
(let (file fun)
(let* ((session (jupyter-session
:conn-info (jupyter-local-tcp-conn-info)))
(client (jupyter-kernel-client))
(hook (copy-sequence kill-emacs-hook)))
(setq file (jupyter-write-connection-file session client))
(should (file-exists-p file))
(should-not (equal kill-emacs-hook hook))
(setq fun (car (cl-set-difference kill-emacs-hook hook)))
(should-not (memq fun hook)))
(garbage-collect)
(garbage-collect)
(garbage-collect)
(garbage-collect)
(unwind-protect
;; This fails on Windows, probably has something to do with the file
;; handle still being opened somehow.
(should-not (file-exists-p file))
(when (file-exists-p file)
(delete-file file)))
(should-not (memq fun kill-emacs-hook))))
(ert-deftest jupyter-client-channels ()
:tags '(client channels)
(ert-info ("Starting/stopping channels")
;; FIXME: Without a new client, I'm getting
;;
;; (zmq-EFSM "Operation cannot be accomplished in current state")
;;
;; on the `jupyter-connect-repl' test pretty consistently.
(let ((jupyter-test-with-new-client t))
(jupyter-test-with-python-client client
(jupyter-stop-channels client)
(cl-loop
for channel in '(:hb :shell :iopub :stdin)
for alive-p = (jupyter-channel-alive-p client channel)
do (should-not alive-p))
(jupyter-start-channels client)
(cl-loop
for channel in '(:hb :shell :iopub :stdin)
for alive-p = (jupyter-channel-alive-p client channel)
do (should alive-p))
(jupyter-stop-channels client)
(cl-loop
for channel in '(:hb :shell :iopub :stdin)
for alive-p = (jupyter-channel-alive-p client channel)
do (should-not alive-p))))))
(ert-deftest jupyter-inhibited-handlers ()
:tags '(client handlers)
(jupyter-test-with-python-client client
(let* ((jupyter-inhibit-handlers '(:stream))
(req (jupyter-send-kernel-info-request client)))
(should (equal (jupyter-request-inhibited-handlers req)
'(:stream)))
(should-not (jupyter--run-handler-p
req (jupyter-test-message
req :stream (list :name "stdout" :text "foo"))))
(setq jupyter-inhibit-handlers '(:foo))
(should-error (jupyter-send-kernel-info-request client)))))
(ert-deftest jupyter-requests-pending-p ()
:tags '(client)
(jupyter-test-with-python-client client
(let (pending)
(jupyter-with-timeout (nil jupyter-long-timeout)
(while (jupyter-requests-pending-p client)
(when-let* ((last-sent (gethash "last-sent" (oref client requests))))
(jupyter-wait-until-idle last-sent))))
;; Don't pass CLIENT to `should-not' because `ert' will attempt to
;; print the class object on failure and will fail at doing so.
(setq pending (jupyter-requests-pending-p client))
(should-not pending)
(let ((req (jupyter-send-kernel-info-request client)))
(ert-info ("Pending after send")
(setq pending (jupyter-requests-pending-p client))
(should pending)
(jupyter-wait-until-idle req)
(setq pending (jupyter-requests-pending-p client))
(should-not pending))
(ert-info ("Pending until idle received")
(setq req (jupyter-send-execute-request client
:code "import time; time.sleep(0.2)"))
;; Empty out the pending-requests slot of CLIENT
(jupyter-wait-until-received :status req)
(setq pending (jupyter-requests-pending-p client))
(should pending)
(jupyter-wait-until-idle req)
(setq pending (jupyter-requests-pending-p client))
(should-not pending))))))
(ert-deftest jupyter-eval ()
:tags '(client)
(jupyter-test-with-python-client client
(let ((jupyter-current-client client))
(should (equal (jupyter-eval "1 + 1") "2")))))
(ert-deftest jupyter-line-count-greater-p ()
:tags '(client)
(should (jupyter-line-count-greater-p "\n\n" 1))
(should (jupyter-line-count-greater-p "a\n\n" 1))
(should (jupyter-line-count-greater-p "\na\n" 1))
(should (jupyter-line-count-greater-p "a\na\n" 1))
(should (jupyter-line-count-greater-p "\n\na" 1))
(should (jupyter-line-count-greater-p "a\n\na" 1))
(should (jupyter-line-count-greater-p "\na\na" 1))
(should (jupyter-line-count-greater-p "a\na\na" 1))
(should-not (jupyter-line-count-greater-p "\n\n" 2))
(should-not (jupyter-line-count-greater-p "\n\n" 3)))
(ert-deftest jupyter-available-local-ports ()
:tags '(client)
(let ((ports (jupyter-available-local-ports 5)))
(should (= (length ports) 5))
(dolist (p ports) (should (integerp p)))
(dolist (proc (process-list))
(should-not (string-match-p "jupyter-available-local-ports"
(process-name proc))))))
(defvar server-mode)
(defvar server-buffer)
(ert-deftest jupyter-server-mode-set-client ()
:tags '(client)
(let (server-buffer)
(with-temp-buffer
(setq server-buffer (buffer-name))
(let ((server-mode t)
(client (jupyter-kernel-client)))
(should-not jupyter-current-client)
(with-temp-buffer
(jupyter-server-mode-set-client client 0.01))
(should jupyter-current-client)
(sleep-for 0.1)
(should-not jupyter-current-client)))))
(ert-deftest jupyter-map-pending-requests ()
:tags '(client)
(let ((err (should-error
(jupyter-map-pending-requests nil #'identity)
:type 'wrong-type-argument)))
(should (equal (nth 1 err) 'jupyter-kernel-client)))
(jupyter-with-echo-client client
(let ((r1 (jupyter-request :id "id1"))
(r2 (jupyter-request :id "id2"))
(mapped nil))
(puthash "last-sent" r1 (oref client requests))
(puthash "id1" r1 (oref client requests))
(puthash "id2" r2 (oref client requests))
(jupyter-map-pending-requests client
(lambda (req) (push req mapped)))
(should (= (length mapped) 2))
(should (memq r1 mapped))
(should (memq r2 mapped))
(setq mapped nil)
(setf (jupyter-request-idle-received-p r2) t)
(jupyter-map-pending-requests client
(lambda (req) (push req mapped)))
(should (= (length mapped) 1))
(should (memq r1 mapped))
(should-not (memq r2 mapped)))))
;;; IOloop
(ert-deftest jupyter-ioloop-lifetime ()
:tags '(ioloop)
(let ((ioloop (jupyter-ioloop))
(jupyter-default-timeout 2))
(should-not (process-live-p (oref ioloop process)))
(jupyter-ioloop-start ioloop :tag1)
(should (equal (jupyter-ioloop-last-event ioloop) '(start)))
(with-slots (process) ioloop
(should (process-live-p process))
(jupyter-ioloop-stop ioloop)
(should (equal (jupyter-ioloop-last-event ioloop) '(quit)))
(sleep-for 0.1)
(should-not (process-live-p process)))))
(defvar jupyter-ioloop-test-handler-called nil
"Flag variable used for testing the `juyter-ioloop'.")
(cl-defmethod jupyter-ioloop-handler ((_ioloop jupyter-ioloop)
(_tag (eql :test))
(event (head test)))
(should (equal (cadr event) "message"))
(setq jupyter-ioloop-test-handler-called t))
(ert-deftest jupyter-ioloop-wait-until ()
:tags '(ioloop)
(let ((ioloop (jupyter-ioloop)))
(should-not (jupyter-ioloop-last-event ioloop))
(jupyter-ioloop-start ioloop :test)
(should (equal (jupyter-ioloop-last-event ioloop) '(start)))
(jupyter-ioloop-stop ioloop)))
(ert-deftest jupyter-ioloop-callbacks ()
:tags '(ioloop)
(ert-info ("Callback added before starting the ioloop")
(let ((ioloop (jupyter-ioloop)))
(setq jupyter-ioloop-test-handler-called nil)
(jupyter-ioloop-add-callback ioloop
`(lambda () (zmq-prin1 (list 'test "message"))))
(jupyter-ioloop-start ioloop :test)
(jupyter-ioloop-stop ioloop)
(should jupyter-ioloop-test-handler-called)))
(ert-info ("Callback added after starting the ioloop")
(let ((ioloop (jupyter-ioloop)))
(setq jupyter-ioloop-test-handler-called nil)
(jupyter-ioloop-start ioloop :test)
(should (process-live-p (oref ioloop process)))
(jupyter-ioloop-add-callback ioloop
`(lambda () (zmq-prin1 (list 'test "message"))))
(jupyter-ioloop-wait-until ioloop 'test #'identity)
(jupyter-ioloop-stop ioloop)
(should jupyter-ioloop-test-handler-called))))
(ert-deftest jupyter-ioloop-setup ()
:tags '(ioloop)
(let ((ioloop (jupyter-ioloop)))
(setq jupyter-ioloop-test-handler-called nil)
(jupyter-ioloop-add-setup ioloop
(zmq-prin1 (list 'test "message")))
(jupyter-ioloop-start ioloop :test)
(jupyter-ioloop-stop ioloop)