Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add with-context macro for conviently wrapping collapsers in thier own context #135

Merged
merged 2 commits into from
Apr 4, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,9 @@
HystrixCollapser
HystrixCollapser$Scope
HystrixCollapser$Setter
HystrixCollapser$CollapsedRequest]))
HystrixCollapser$CollapsedRequest]
[com.netflix.hystrix.strategy.concurrency
HystrixRequestContext]))

(defmacro ^:private key-fn
"Make a function that creates keys of the given class given one of:
Expand Down Expand Up @@ -225,6 +227,15 @@

;################################################################################

(defmacro with-request-context
"Executes body within a new Hystrix Context"
[& body]
`(let [context# (HystrixRequestContext/initializeContext)]
(try
~@body
(finally
(.shutdown context#)))))

(defn command
"Helper function that takes a definition map for a HystrixCommand and returns a normalized
version ready for use with execute and queue.
Expand Down Expand Up @@ -599,4 +610,3 @@
(if (instance? Exception response)
(.setException request ^Exception response)
(.setResponse request response))))))))

Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,15 @@
(is (= (- 99 42) (.execute c)))))

(testing "makes a HystrixCommand that implements getCacheKey"

(let [context (com.netflix.hystrix.strategy.concurrency.HystrixRequestContext/initializeContext)]
(try
(let [call-count (atom 0) ; make sure run-fn is only called once
test-def (normalize (assoc base-def
:run-fn (fn [arg] (swap! call-count inc) (str arg "!"))
:cache-key-fn (fn [arg] arg)))
result1 (.execute (instantiate test-def "hi"))
result2 (.execute (instantiate test-def "hi"))]
(is (= "hi!" result1 result2))
(is (= 1 @call-count)))
(finally
(.shutdown context))))))
(with-request-context
(let [call-count (atom 0) ; make sure run-fn is only called once
test-def (normalize (assoc base-def
:run-fn (fn [arg] (swap! call-count inc) (str arg "!"))
:cache-key-fn (fn [arg] arg)))
result1 (.execute (instantiate test-def "hi"))
result2 (.execute (instantiate test-def "hi"))]
(is (= "hi!" result1 result2))
(is (= 1 @call-count))))))
(testing "throws if :hystrix metadata isn't found in a var"
(is (thrown? IllegalArgumentException
(instantiate #'map))))
Expand Down Expand Up @@ -292,4 +288,3 @@
(is (= "V" single-call))
(is (= n (count expected-results) (count results)))
(is (= expected-results results)))))