Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add tests for server pool

  • Loading branch information...
commit 4685109f58df7c88430fc6e627978e2fec2d1d6e 1 parent 36bb0aa
@tkf authored
Showing with 103 additions and 7 deletions.
  1. +2 −1  Carton
  2. +11 −6 jedi.el
  3. +90 −0 test-jedi.el
View
3  Carton
@@ -4,4 +4,5 @@
(development
(depends-on "epc")
- (depends-on "auto-complete"))
+ (depends-on "auto-complete")
+ (depends-on "mocker"))
View
17 jedi.el
@@ -286,6 +286,16 @@ connection."
;; Same as `process-live-p' in Emacs >= 24:
(memq (process-status proc) '(run open listen connect stop)))))
+(defun jedi:epc--start-epc (server-prog server-args)
+ "Same as `epc:start-epc', but set query-on-exit flag for
+associated processes to nil."
+ (let ((mngr (epc:start-epc server-prog server-args)))
+ (set-process-query-on-exit-flag (epc:connection-process
+ (epc:manager-connection mngr))
+ nil)
+ (set-process-query-on-exit-flag (epc:manager-server-process mngr) nil)
+ mngr))
+
;;; Server pool
@@ -299,12 +309,7 @@ key, or start new one if there is none."
(if (and cached (jedi:epc--live-p cached))
cached
(let* ((default-directory jedi:source-dir)
- (mngr (epc:start-epc (car command)
- (cdr command))))
- (set-process-query-on-exit-flag (epc:connection-process
- (epc:manager-connection mngr))
- nil)
- (set-process-query-on-exit-flag (epc:manager-server-process mngr) nil)
+ (mngr (jedi:epc--start-epc (car command) (cdr command))))
(puthash command mngr jedi:server-pool--table)
(jedi:server-pool--gc-when-idle)
mngr))))
View
90 test-jedi.el
@@ -28,6 +28,9 @@
(eval-when-compile (require 'cl))
(require 'ert)
+
+(require 'mocker)
+
(require 'jedi)
@@ -38,6 +41,9 @@
"Check if `jedi:version' can be parsed by `version-to-list'."
(version-to-list jedi:version))
+
+;;; EPC
+
(ert-deftest jedi:complete-request ()
(jedi-testing:sync
(with-temp-buffer
@@ -86,6 +92,90 @@
(should (integerp line_nr))
(should (stringp module_path)))))
+
+;;; Server pool
+
+(defmacro jedi-testing:with-mocked-server (start-epc-records
+ epc--live-p-records
+ buffers
+ &rest body)
+ (declare (indent 3))
+ `(let ((jedi:server-pool--table (make-hash-table :test 'equal))
+ ,@(mapcar
+ (lambda (b) `(,b (generate-new-buffer "*jedi test*")))
+ buffers))
+ (mocker-let
+ ((jedi:epc--start-epc (x y) ,start-epc-records)
+ (jedi:epc--live-p (x) ,epc--live-p-records)
+ (jedi:server-pool--gc-when-idle
+ ()
+ ((:record-cls 'mocker-stub-record))))
+ (macrolet ((check (&rest args)
+ `(jedi-testing:check-start-server ,@args)))
+ (unwind-protect
+ (progn ,@body)
+ (mapc #'kill-buffer (list ,@buffers)))))))
+
+(defun jedi-testing:check-start-server (buffer command server)
+ (with-current-buffer buffer
+ (should-not jedi:epc)
+ (should (eq (let ((jedi:server-command command)
+ (jedi:server-args nil))
+ (jedi:start-server))
+ server))
+ (should (eq jedi:epc server))))
+
+(ert-deftest jedi:pool-single-server ()
+ "Successive call of `jedi:start-server' with the same setup should
+return the same server instance."
+ (jedi-testing:with-mocked-server
+ ;; Mock `epc:start-epc':
+ ((:input '("python" ("jediepcserver.py")) :output 'dummy-server))
+ ;; Mock `jedi:epc--live-p':
+ ((:input '(dummy-server) :output t))
+ ;; Buffers to use:
+ (buf1 buf2)
+ (check buf1 '("python" "jediepcserver.py") 'dummy-server)
+ (check buf2 '("python" "jediepcserver.py") 'dummy-server)))
+
+(ert-deftest jedi:pool-per-buffer-server ()
+ "Successive call of `jedi:start-server' with different setups should
+return the different server instances."
+ (jedi-testing:with-mocked-server
+ ;; Mock `epc:start-epc':
+ ((:input '("python" ("jediepcserver.py")) :output 'dummy-server-1)
+ (:input '("python3" ("jediepcserver.py")) :output 'dummy-server-2))
+ ;; Mock `jedi:epc--live-p':
+ ()
+ ;; Buffers to use:
+ (buf1 buf2)
+ (check buf1 '("python" "jediepcserver.py") 'dummy-server-1)
+ (check buf2 '("python3" "jediepcserver.py") 'dummy-server-2)))
+
+(ert-deftest jedi:pool-restart-per-buffer-server ()
+ "When one of the server died, only the died server must be
+rebooted; not still living ones."
+ (jedi-testing:with-mocked-server
+ ;; Mock `epc:start-epc':
+ ((:input '("python" ("jediepcserver.py")) :output 'dummy-server-1)
+ (:input '("python3" ("jediepcserver.py")) :output 'dummy-server-2)
+ (:input '("python" ("jediepcserver.py")) :output 'dummy-server-3))
+ ;; Mock `jedi:epc--live-p':
+ ((:input '(dummy-server-1) :output t :max-occur 1)
+ (:input '(dummy-server-1) :output nil) ; server is stopped
+ (:input '(dummy-server-2) :output t)
+ (:input '(dummy-server-3) :output t))
+ ;; Buffers to use:
+ (buf1 buf2 buf3)
+ (check buf1 '("python" "jediepcserver.py") 'dummy-server-1)
+ (check buf2 '("python3" "jediepcserver.py") 'dummy-server-2)
+ (check buf3 '("python" "jediepcserver.py") 'dummy-server-1)
+ (mapc (lambda (b) (with-current-buffer b (setq jedi:epc nil)))
+ (list buf1 buf2 buf3))
+ (check buf1 '("python" "jediepcserver.py") 'dummy-server-3) ; rebooted!
+ (check buf2 '("python3" "jediepcserver.py") 'dummy-server-2) ; not this.
+ (check buf3 '("python" "jediepcserver.py") 'dummy-server-3)))
+
(provide 'test-jedi)
;;; test-jedi.el ends here

0 comments on commit 4685109

Please sign in to comment.
Something went wrong with that request. Please try again.