File: websocket-functional-test.el

package info (click to toggle)
emacs-websocket 1.15-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 180 kB
  • sloc: lisp: 1,589; python: 26; makefile: 2
file content (98 lines) | stat: -rw-r--r-- 4,186 bytes parent folder | download
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
;;; websocket-functional-test.el --- Simple functional testing

;; Copyright (c) 2013, 2016  Free Software Foundation, Inc.

;; 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 2 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 GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; These are functional tests that may fail for various environmental reasons,
;; such as blocked ports. For example Windows users have to have gnutls DLLs in
;; the Emacs bin directory for this to work. A firewall may also interfere with
;; these tests.
;;
;; These tests are written to test the basic connectivity and message-sending.
;; Corner-cases and error handling is tested in websocket-test.el.

(require 'tls)   ;; tests a particular bug we had on Emacs 23
(require 'websocket)
(require 'cl)

;;; Code:

(defmacro websocket-test-wait-with-timeout (timeout &rest body)
  "Run BODY until true or TIMEOUT (in seconds) is reached.

Will return false if the timeout was reached. This macro is not
written to be used widely."
  `(let ((begin (current-time))
         (result nil))
     (while (and (< (- (float-time (time-subtract (current-time) begin))) ,timeout) (not result))
       (setq result ,@body)
       (sleep-for 0.5))
     result))

(defun websocket-functional-client-test (wstest-server-url)
  "Run the main part of an ert test against WSTEST-SERVER-URL."
  ;; the server may have an untrusted certificate, for the test to proceed, we
  ;; need to disable trust checking.
  (let* ((tls-checktrust nil)
         (wstest-closed nil)
         (wstest-msg)
         (wstest-server-proc)
         (wstest-ws
          (websocket-open
           wstest-server-url
           :on-message (lambda (_websocket frame)
                         (setq wstest-msg (websocket-frame-text frame)))
           :on-close (lambda (_websocket) (setq wstest-closed t)))))
    (should (websocket-test-wait-with-timeout 2 (websocket-openp wstest-ws)))
    (should (websocket-test-wait-with-timeout 2 (eq 'open (websocket-ready-state wstest-ws))))
    (should (null wstest-msg))
    (websocket-send-text wstest-ws "Hi!")
    (should (websocket-test-wait-with-timeout 5 (equal wstest-msg "Hi!")))
    (websocket-close wstest-ws)))

(ert-deftest websocket-client-with-local-server ()
  ;; If testserver.py cannot start, this test will fail.
  (let ((proc (start-process
               "websocket-testserver" "*websocket-testserver*"
               "python3" "testserver.py" "--log_to_stderr" "--logging=debug")))
    (when proc
      (sleep-for 1)
      (websocket-functional-client-test "ws://127.0.0.1:9999"))))

(ert-deftest websocket-server ()
  (let* ((wstest-closed)
         (wstest-msg)
         (server-conn (websocket-server
                       9998
                       :host 'local
                       :on-message (lambda (ws frame)
                                     (websocket-send-text
                                      ws (websocket-frame-text frame)))
                       :on-close (lambda (_websocket)
                                   (setq wstest-closed t))))
         (wstest-ws (websocket-open
                    "ws://localhost:9998"
                    :on-message (lambda (_websocket frame)
                                  (setq wstest-msg (websocket-frame-text frame))))))
    (should (websocket-test-wait-with-timeout 1 (websocket-openp wstest-ws)))
    (websocket-send-text wstest-ws "你好")
    (should (websocket-test-wait-with-timeout 1 (equal wstest-msg "你好")))
    (websocket-server-close server-conn)
    (should (websocket-test-wait-with-timeout 1 wstest-closed))))

(provide 'websocket-functional-test)
;;; websocket-functional-test.el ends here