summaryrefslogtreecommitdiff
path: root/tests/dist.scm
blob: 10aa90a6f081fd3c5466f3001918ae26cd5b80cf (about) (plain)
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
;;; dist.scm -- Testing of the distributed forms

;; Copyright (C) 2015, 2016, 2017 Artyom V. Poptsov <poptsov.artyom@gmail.com>
;;
;; This file is a part of Guile-SSH.
;;
;; Guile-SSH 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.
;;
;; Guile-SSH 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 Guile-SSH.  If not, see <http://www.gnu.org/licenses/>.

(add-to-load-path (getenv "abs_top_srcdir"))

(use-modules (srfi srfi-64)
             (ice-9 receive)
             (ice-9 rdelim)
             (ssh  session)
             (ssh  key)
             (ssh  auth)
             (ssh  message)
             (ssh  server)
             (ssh  log)
             (ssh  dist)
             (ssh  dist job)
             (ssh  dist node)
             (tests common))

(test-begin-with-log "dist")

;;;


(test-assert "make-node"
  (let* ((s (make-session-for-test))
         (n (make-node s)))
    (and n
         (eq? (node-repl-port n) 37146)
         (eq? (node-session n)   s))))


(test-equal "split, 1"
  '((a b) (c d) (e f g))
  (split '(a b c d e f g) 3))

(test-equal "split, 2"
  '((a))
  (split '(a) 2))


(test-assert "make-job"
  (let* ((s (make-session-for-test))
         (n (make-node s))
         (data '(1 2 3))
         (proc '(lambda (n) (1+ n)))
         (j (make-job 'map n data proc)))
    (and (eq? (job-type j) 'map)
         (eq? (job-node j) n)
         (eq? (job-data j) data)
         (eq? (job-proc j) proc))))

(test-assert "set-job-node"
  (let* ((s    (make-session-for-test))
         (n1   (make-node s))
         (n2   (make-node s))
         (data '())
         (proc '(lambda (n) (1+ n)))
         (j1   (make-job 'map n1 data proc))
         (j2   (set-job-node j1 n2)))
    (and (not (eq? j1 j2))
         (eq? (job-type j1) (job-type j2))
         (eq? (job-node j1) n1)
         (eq? (job-node j2) n2)
         (eq? (job-data j1) (job-data j2))
         (eq? (job-proc j1) (job-proc j2)))))

(test-error-with-log "hand-out-job, invalid type"
  (let ((n (make-node (make-session-for-test))))
    (hand-out-job (make-job 'invalid-job n '() (const #t)))))


(test-assert "assign-eval"
  (let* ((s     (make-session-for-test))
         (nodes (make-list 2 (make-node s)))
         (exprs (make-list 10 '(lambda (x) (1+ x))))
         (jobs  (assign-eval nodes exprs)))
    (and (eq? (length jobs) 2)
         (eq? (job-type (car jobs)) 'eval)
         (eq? (length (job-proc (car jobs))) 5))))


;;; Testing of 'rrepl-get-result'.
;; These test cases are intended to test various inputs for 'rrepl-get-result'
;; procedure.

(test-assert "rrepl-get-result"
  (receive (result eval-num module-name lang)
      (call-with-input-string "scheme@(guile-user)> $0 = test"
                              rrepl-get-result)
    ;; (format (current-error-port)
    ;;         "\tresult: ~a\neval-num: ~a"
    (and (eq?      result      'test)
         (=        eval-num    0)
         (string=? module-name "(guile-user)")
         (string=? lang        "scheme"))))

(test-assert "rrepl-get-result, unspecified"
  (receive (result eval-num module-name lang)
      (call-with-input-string "scheme@(guile-user)> "
                              rrepl-get-result)
    (and (eq?      result      *unspecified*)
         (eq?      eval-num    *unspecified*)
         (string=? module-name "(guile-user)")
         (string=? lang        "scheme"))))

(test-error-with-log/= "rrepl-get-result, error"
  'node-repl-error "scheme@(guile-user)> ERROR: error."
  (call-with-input-string "scheme@(guile-user)> ERROR: error."
                          rrepl-get-result))

;; See <https://github.com/artyom-poptsov/guile-ssh/issues/3>.
(test-error-with-log/= "rrepl-get-result, compilation error"
  'node-repl-error "scheme@(guile-user)> While compiling expression:\nERROR: no code for module (module-that-doesnt-exist)"
  (call-with-input-string
   (string-append "scheme@(guile-user)> While compiling expression:\n"
                  "ERROR: no code for module (module-that-doesnt-exist)")
   rrepl-get-result))

(test-error-with-log/= "rrepl-get-result, unbound variable error"
  'node-repl-error "scheme@(guile-user)> ;;; socket:9:7: warning: \
possibly unbound variable `e'\nsocket:9:7: In procedure #<procedure \
1a44920 at socket:9:7 ()>:\nsocket:9:7: In procedure module-lookup: \
Unbound variable: e"
  (call-with-input-string
   (string-append (string-append
                   "scheme@(guile-user)> ;;; socket:9:7: warning: "
                   "possibly unbound variable `e'\nsocket:9:7: "
                   "In procedure #<procedure 1a44920 at socket:9:7 ()>:\n"
                   "socket:9:7: In procedure module-lookup: Unbound variable: e"))
   rrepl-get-result))

(test-error-with-log/= "rrepl-get-result, unknown # object error"
  'node-repl-error "Reader error: scm_lreadr: #<unknown port>:1:3: \
Unknown # object: (#\\<): scheme@(guile-user)> \
$4 = #<session #<undefined>@#<undefined>:22 (disconnected) 453fff>"
  (call-with-input-string
   (string-append  "scheme@(guile-user)> $4 = "
                   "#<session #<undefined>@#<undefined>:22 (disconnected) 453fff>")
   rrepl-get-result))

(test-assert "rrepl-get-result, elisp"
  (receive (result eval-num module-name lang)
      (call-with-input-string "elisp@(guile-user)> $0 = #nil"
                              rrepl-get-result)
    (and (eq?      result      '#nil)
         (=        eval-num    0)
         (string=? module-name "(guile-user)")
         (string=? lang        "elisp"))))

(test-assert "rrepl-get-result, multiple values"
  (receive (result eval-num module-name lang)
      (call-with-input-string "scheme@(guile-user)> $0 = v1\n$1 = v2"
                              rrepl-get-result)
    (and (vector? eval-num)
         (vector? result)
         (eq?      (vector-ref result 0)   'v1)
         (eq?      (vector-ref result 1)   'v2)
         (=        (vector-ref eval-num 0) 0)
         (=        (vector-ref eval-num 1) 1)
         (string=? module-name "(guile-user)")
         (string=? lang        "scheme"))))


(test-assert "rrepl-skip-to-prompt, valid input"
  (begin
    (call-with-input-string "Enter `,help' for help."
      (lambda (port)
        (rrepl-skip-to-prompt port)))
    #t))

(test-error-with-log "rrepl-skip-to-prompt, invalid input" 'node-error
  (call-with-input-string "invalid input"
                          (lambda (port)
                            (rrepl-skip-to-prompt port))))


;;; Distributed forms.

;; The client uses distributed form 'with-ssh' to evaluate (+ 21 21).  The
;; server pretends to be a RREPL server and returns the evaluation "result",
;; 42.
(test-assert-with-log "with-ssh"
  (run-client-test
   ;; Server
   (lambda (server)
     (server-listen server)
     (server-set! server 'log-verbosity 'functions)
     (let ((session (server-accept server)))
       (server-handle-key-exchange session)
       (start-session-loop
        session
        (lambda (msg type)
          (format-log/scm 'nolog
                          "server"
                          "msg: ~a; type: ~a" msg type)
          (case (car type)
            ((request-channel-open)
             (let ((c (message-channel-request-open-reply-accept msg)))
               (format-log/scm 'nolog "server" "channel 0: ~a" c)
               ;; Write the last line of Guile REPL greeting message to
               ;; pretend that we're a REPL server.
               (write-line "Enter `,help' for help." c)
               (format-log/scm 'nolog "server" "channel 1: ~a" c)
               (usleep 100)
               (poll c
                     (lambda args
                       ;; Read expression
                       (let ((result (read-line c)))
                         (format-log/scm 'nolog "server"
                                         "sexp: ~a" result)
                         (or (string=? result "(begin (+ 21 21))")
                             (error "Wrong result 1" result)))

                       ;; Read newline
                       (let ((result (read-line c)))
                         (format-log/scm 'nolog "server"
                                         "sexp: ~a" result)
                         (or (string=? result "(newline)")
                             (error "Wrong result 2" result)))

                       (write-line "scheme@(guile-user)> $1 = 42\n" c)
                       (close c)
                       (while #t
                         (sleep 60))))))
            (else
             (message-reply-success msg)))))))
   ;; Client
   (lambda ()
     (call-with-connected-session
      (lambda (session)
        (authenticate-server session)
        (format-log/scm 'nolog "client" "session: ~a" session)
        (unless (equal? (userauth-none! session) 'success)
          (error "Could not authenticate with a server" session))

        (let ((n (make-node session #:start-repl-server? #f)))
          (= (with-ssh n
                       (+ 21 21))
             42)))))))

;;;


(test-end "dist")

(exit (= (test-runner-fail-count (test-runner-current)) 0))

;;; dist.scm ends here.