Permalink
Browse files

Allow to use a random port for each subtest in handler test suites.

  • Loading branch information...
1 parent c098ec5 commit e1ae1ded15950b75ad73cc6da78a82113c63234f @fukamachi committed Aug 19, 2015
Showing with 49 additions and 30 deletions.
  1. +1 −1 .travis.yml
  2. +39 −19 src/test.lisp
  3. +4 −8 src/test/suite.lisp
  4. +2 −1 t/handler/fcgi.lisp
  5. +3 −1 t/handler/hunchentoot.lisp
View
@@ -57,5 +57,5 @@ script:
- if [ "$LISP" = "sbcl-bin" ]; then
run-prove t-*.asd;
else
- run-prove t-clack-handler-hunchentoot.asd
+ run-prove t-clack-handler-hunchentoot.asd;
fi
View
@@ -13,7 +13,9 @@
:socket-error)
(:export :*clack-test-handler*
:*clack-test-port*
- :*enable-debug-p*
+ :*clack-test-access-port*
+ :*enable-debug*
+ :*random-port*
:localhost
:subtest-app))
(in-package :clack.test)
@@ -24,7 +26,13 @@
(defvar *clack-test-port* 4242
"HTTP port number of Handler.")
-(defvar *enable-debug-p* t)
+(defvar *clack-test-access-port* *clack-test-port*
+ "Port of localhost to request.
+Use if you want to set another port. The default is `*clack-test-port*`.")
+
+(defvar *enable-debug* t)
+
+(defvar *random-port* nil)
(defun port-available-p (port)
(let (socket)
@@ -40,6 +48,12 @@
(usocket:socket-close socket)
t))))
+(defun random-port ()
+ "Return a port number not in use from 50000 to 60000."
+ (loop for port from (+ 50000 (random 1000)) upto 60000
+ if (port-available-p port)
+ return port))
+
(defun localhost (&optional (path "/") (port *clack-test-port*))
(check-type path string)
(setf path
@@ -52,23 +66,29 @@
port path))
(defun %subtest-app (desc app client)
- (loop repeat 5
- until (port-available-p *clack-test-port*)
- do (sleep 0.1)
- finally
- (unless (port-available-p *clack-test-port*)
- (error "Port ~D is already in use." *clack-test-port*)))
- (let ((acceptor (clackup app
- :server *clack-test-handler*
- :use-thread t
- :silent t
- :port *clack-test-port*
- :debug *enable-debug-p*)))
- (subtest desc
- (sleep 0.5)
- (unwind-protect
- (funcall client)
- (stop acceptor)))))
+ (let* ((*clack-test-port* (if *random-port*
+ (random-port)
+ *clack-test-port*))
+ (*clack-test-access-port* (if *random-port*
+ *clack-test-port*
+ *clack-test-access-port*)))
+ (loop repeat 5
+ until (port-available-p *clack-test-port*)
+ do (sleep 0.1)
+ finally
+ (unless (port-available-p *clack-test-port*)
+ (error "Port ~D is already in use." *clack-test-port*)))
+ (let ((acceptor (clackup app
+ :server *clack-test-handler*
+ :use-thread t
+ :silent t
+ :port *clack-test-port*
+ :debug *enable-debug*)))
+ (subtest desc
+ (sleep 0.5)
+ (unwind-protect
+ (funcall client)
+ (stop acceptor))))))
(defmacro subtest-app (desc app &body client)
`(%subtest-app ,desc ,app (lambda () ,@client)))
View
@@ -5,7 +5,8 @@
(:import-from :clack.test
:*clack-test-handler*
:*clack-test-port*
- :*enable-debug-p*
+ :*clack-test-access-port*
+ :*enable-debug*
:subtest-app)
(:import-from :flexi-streams
:octet
@@ -15,14 +16,9 @@
:http-request)
(:import-from :http-body
:parse)
- (:export :*clack-test-access-port*
- :run-server-tests))
+ (:export :run-server-tests))
(in-package :clack.test.suite)
-(defvar *clack-test-access-port* *clack-test-port*
- "Port of localhost to request.
-Use if you want to set another port. The default is `*clack-test-port*`.")
-
(defvar *clack-pathname*
(asdf:system-source-directory :clack))
@@ -297,7 +293,7 @@ you would call like this: `(run-server-tests :foo)'."
"T"
:test #'equalp))
- (let ((*enable-debug-p* nil)
+ (let ((*enable-debug* nil)
(*error-output* (make-broadcast-stream)))
(subtest-app "Do not crash when the app dies"
(lambda (env)
View
@@ -1,9 +1,10 @@
(in-package :cl-user)
(defpackage clack-test.handler.fcgi
(:use :cl
+ :clack.test
:clack.test.suite))
(in-package :clack-test.handler.fcgi)
(let ((clack.test:*clack-test-port* 14949)
- (clack.test.suite:*clack-test-access-port* 4949))
+ (clack.test:*clack-test-access-port* 4949))
(clack.test.suite:run-server-tests :fcgi))
@@ -1,7 +1,9 @@
(in-package :cl-user)
(defpackage t.clack.handler.hunchentoot
(:use :cl
+ :clack.test
:clack.test.suite))
(in-package :t.clack.handler.hunchentoot)
-(clack.test.suite:run-server-tests :hunchentoot)
+(let ((clack.test:*random-port* t))
+ (clack.test.suite:run-server-tests :hunchentoot))

0 comments on commit e1ae1de

Please sign in to comment.