Skip to content

Commit 3a6cd16

Browse files
committed
Add test for status-codes & replace reduce with O(1) lookup
1 parent 918864f commit 3a6cd16

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/clj_http_hystrix/core.clj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,15 @@
6565
(warn message))))
6666

6767
(defn status-codes
68-
"Create a predicate that returns true whenever one of the given
68+
"Create a predicate that returns true whenever one of the given
6969
status codes is present"
70-
[& status]
71-
(fn [req resp]
72-
(reduce #(or %1 (= %2 (:status resp))) false status)))
70+
[& status-codes]
71+
(let [status-codes (set status-codes)]
72+
(fn [req resp]
73+
(contains? status-codes (:status resp)))))
7374

7475
(defn client-error?
75-
"Returns true when the response has one of the 4xx family of status
76+
"Returns true when the response has one of the 4xx family of status
7677
codes"
7778
[req resp]
7879
(http/client-error? resp))

test/clj_http_hystrix/core_test.clj

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
(http/get url {:throw-exceptions false
127127
:hystrix/command-key command-key
128128
:hystrix/bad-request-pred client-error?}) => (contains {:status 400}))
129-
(Thread/sleep 1000) ;sleep to wait for Hystrix health snapshot
129+
(Thread/sleep 600) ;sleep to wait for Hystrix health snapshot
130130
(http/get url {:throw-exceptions false
131131
:hystrix/command-key command-key}) => (contains {:status 200}))))
132132

@@ -144,7 +144,7 @@
144144
(http/get url {:throw-exceptions true
145145
:hystrix/command-key command-key
146146
:hystrix/bad-request-pred client-error?}) => (throws ExceptionInfo))
147-
(Thread/sleep 1000) ;sleep to wait for Hystrix health snapshot
147+
(Thread/sleep 600) ;sleep to wait for Hystrix health snapshot
148148
(http/get url {:throw-exceptions false
149149
:hystrix/command-key command-key}) => (contains {:status 200}))))
150150

@@ -159,6 +159,15 @@
159159
(http/get url {:throw-exceptions false
160160
:hystrix/command-key command-key
161161
:hystrix/bad-request-pred (constantly false)}) => (contains {:status 400}))
162-
(Thread/sleep 1000) ;sleep to wait for Hystrix health snapshot
162+
(Thread/sleep 600) ;sleep to wait for Hystrix health snapshot
163163
(http/get url {:throw-exceptions false
164164
:hystrix/command-key command-key}) => (contains {:status 503}))))
165+
166+
(fact "status-codes predicate matches only given status codes"
167+
(let [predicate (status-codes 100 200 300)]
168+
(predicate {} {:status 100}) => true
169+
(predicate {} {:status 200}) => true
170+
(predicate {} {:status 300}) => true
171+
(predicate {} {:status 101}) => false
172+
(predicate {} {:status 202}) => false
173+
(predicate {} {:status 299}) => false))

0 commit comments

Comments
 (0)