Skip to content

Instantly share code, notes, and snippets.

@daveray
Created May 28, 2013 21:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daveray/5666216 to your computer and use it in GitHub Desktop.
Save daveray/5666216 to your computer and use it in GitHub Desktop.
Hystrix command property caching
(def init-fn-timeout (atom 5000))
(def init-fn-called (atom 0))
(defcommand init-fn-test-command
{:hystrix/group-key "init-fn-test-command"
:hystrix/thread-pool-key "init-fn-test-command"
:hystrix/init-fn (fn [hystrix-map setter]
(swap! init-fn-called inc)
(let [prop-setter (. com.netflix.hystrix.HystrixCommandProperties Setter)]
(doto setter
(.andCommandPropertiesDefaults
(doto prop-setter
(.withExecutionIsolationThreadTimeoutInMilliseconds @init-fn-timeout)))))) }
[])
(deftest test-init-fn
(testing "properties are applied to command when instantiated"
(let [; make sure init-fn is called on every instantiation
_ (reset! init-fn-called 0)
; create command with 5000 timeout
_ (reset! init-fn-timeout 5000)
c0 (instantiate #'init-fn-test-command)
c0-timeout (-> c0 .getProperties .executionIsolationThreadTimeoutInMilliseconds .get)
; create command with 6000 timeout
_ (reset! init-fn-timeout 6000)
c1 (instantiate #'init-fn-test-command)
c1-timeout (-> c1 .getProperties .executionIsolationThreadTimeoutInMilliseconds .get)
]
(is 2 @init-fn-called)
(is (= 5000 c0-timeout))
; This fails because 5000 is cached forever after the first time a command is instantiated.
(is (= 6000 c0-timeout)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment