Skip to content

Commit 6750e71

Browse files
committed
Harden async tests
1 parent 81ed184 commit 6750e71

File tree

1 file changed

+40
-13
lines changed

1 file changed

+40
-13
lines changed

test/clj/puppetlabs/trapperkeeper/services/webserver/jetty12_service_test.clj

Lines changed: 40 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,19 @@
143143
(is (= (:status response) 200))
144144
(is (= (:body response) hello-body))))))
145145

146+
(defn await-result
147+
[result timeout-ms description]
148+
(let [timeout-value ::timeout
149+
value (deref result timeout-ms timeout-value)]
150+
(is (not= timeout-value value)
151+
(str "Timed out waiting for " description))
152+
value))
153+
154+
(defn request-failed?
155+
[response]
156+
(or (some? (:error response))
157+
(not= 200 (:status response))))
158+
146159
(deftest basic-ring-test
147160
(testing "ring request over http succeeds"
148161
(validate-ring-handler
@@ -723,27 +736,34 @@
723736
(with-app-with-config
724737
app
725738
[jetty12-service]
726-
{:webserver {:port 8080 :shutdown-timeout-seconds 10}}
739+
{:webserver {:port 8080
740+
:shutdown-timeout-seconds 10
741+
:gzip-enable false}}
727742
(let [s (tk-app/get-service app :WebserverService)
728743
add-ring-handler (partial add-ring-handler s)
729744
in-request-handler (promise)
730745
ring-handler (fn [_]
731746
(deliver in-request-handler true)
732-
(Thread/sleep 3000)
747+
(Thread/sleep 1000)
733748
{:status 200 :body "Hello, World!"})]
734749
(add-ring-handler ring-handler "/hello")
735-
(with-open [async-client (async/create-client {})]
750+
(with-open [async-client (async/create-client {:socket-timeout-milliseconds 30000})]
736751
(let [response (http-client-common/get async-client "http://localhost:8080/hello" {:as :text})]
737752
@in-request-handler
738753
(tk-app/stop app)
739-
(is (= (:status @response) 200))
740-
(is (= (:body @response) "Hello, World!")))))))
754+
(let [response (await-result response 15000 "graceful shutdown response")]
755+
(is (nil? (:error response))
756+
(str "Unexpected request error during graceful shutdown: " (:error response)))
757+
(is (= (:status response) 200))
758+
(is (= (:body response) "Hello, World!"))))))))
741759

742760
(testing "jetty12's stop timeout can be changed from config"
743761
(with-app-with-config
744762
app
745763
[jetty12-service]
746-
{:webserver {:port 8080 :shutdown-timeout-seconds 1}}
764+
{:webserver {:port 8080
765+
:shutdown-timeout-seconds 1
766+
:gzip-enable false}}
747767
(let [s (tk-app/get-service app :WebserverService)
748768
add-ring-handler (partial add-ring-handler s)
749769
in-request-handler (promise)
@@ -752,12 +772,14 @@
752772
(Thread/sleep 2000)
753773
{:status 200 :body "Hello, World!"})]
754774
(add-ring-handler ring-handler "/hello")
755-
(with-open [async-client (async/create-client {})]
775+
(with-open [async-client (async/create-client {:socket-timeout-milliseconds 30000})]
756776
(let [response (http-client-common/get async-client "http://localhost:8080/hello" {:as :text})]
757777
@in-request-handler
758778
(tk-log-testutils/with-test-logging
759779
(tk-app/stop app))
760-
(is (not (nil? (:error @response)))))))))
780+
(let [response (await-result response 15000 "request interrupted after shutdown timeout")]
781+
(is (request-failed? response)
782+
(str "Expected in-flight request to fail after stop timeout expired, got: " response))))))))
761783

762784
; Per [TK-437](https://tickets.puppetlabs.com/browse/TK-437) we've been having issues
763785
; with this test randomly hanging, but the fixes applied for TK-437 didn't seem to resolve
@@ -837,16 +859,21 @@
837859
app
838860
[jetty12-service
839861
sleepy-service]
840-
{:webserver {:port 8080 :shutdown-timeout-seconds 1}}
841-
(with-open [async-client (async/create-client {})]
862+
{:webserver {:port 8080
863+
:shutdown-timeout-seconds 1
864+
:gzip-enable false}}
865+
(with-open [async-client (async/create-client {:socket-timeout-milliseconds 30000})]
842866
(let [response (http-client-common/get async-client "http://localhost:8080/hi_world" {:as :text})]
843867
@in-request-handler?
844868
(tk-app/restart app)
845-
(is (not (nil? (:error @response)))))
869+
(let [response (await-result response 15000 "request interrupted during restart")]
870+
(is (request-failed? response)
871+
(str "Expected in-flight request to fail during restart, got: " response))))
846872
(deliver unblock-request? true)
847873
(let [response (http-client-common/get async-client "http://localhost:8080/hi_world" {:as :text})]
848-
(is (= 200 (:status @response)))
849-
(is (= hello-body (:body @response))))))))))
874+
(let [response (await-result response 15000 "post-restart request")]
875+
(is (= 200 (:status response)))
876+
(is (= hello-body (:body response)))))))))))
850877

851878
(deftest double-stop-test
852879
(testing "if the stop lifecycle is called more than once, we handle that gracefully and quietly"

0 commit comments

Comments
 (0)