Skip to content

Instantly share code, notes, and snippets.

View krukow's full-sized avatar

Karl Krukow krukow

View GitHub Profile
(defprotocol CircuitBreakerTransitions
"Transition functions for circuit-breaker states"
(proceed [s] "true if breaker should proceed with call in this state")
(on-success [s] "transition from s to this state after a successful call")
(on-error [s] "transition from s to this state after an unsuccessful call")
(on-before-call [s] "transition from s to this state before a call"))
(def #^{:private true
:doc "Default shared transition functions"}
def-transitions
(defrecord TransitionPolicy [#^int max-fails #^long timeout])
(defrecord ClosedState [#^TransitionPolicy policy #^int fail-count])
(defrecord OpenState [#^TransitionPolicy policy #^long time-stamp])
(defrecord InitialHalfOpenState [#^TransitionPolicy policy])
(defrecord PendingHalfOpenState [#^TransitionPolicy policy])
(extend ClosedState CircuitBreakerTransitions
(merge
def-transitions
{:proceed (constantly true)
:on-success
(fn [{f :fail-count p :policy, :as s}]
(if (zero? f) s (ClosedState. p 0)))
:on-error
(def default-policy (TransitionPolicy. 5 5000))
(def initial-state (ClosedState. default-policy 0))
(assert (= initial-state (on-success initial-state)))
(assert (= (ClosedState. default-policy 1) (on-error initial-state)))
(def e4 (ClosedState. default-policy 4))
(assert (= OpenState
(def default-policy (TransitionPolicy. 5 5000))
(def initial-state (ClosedState. default-policy 0))
(defn make-circuit-breaker
"Creates a circuit-breaker instance. If called with no arguments
a circuit-breaker in the initial closed state with the default policy is
created. If called with one argument supporting the CircuitBreakerTransitions
protocol, a circuit-breaker is created using that as state."
([] (atom initial-state))
([#^CircuitBreakerTransitions s] (atom s)))
(def cb (make-circuit-breaker))
(def succ (wrap-with (constantly 42) cb))
(def fail (wrap-with (fn [] (throw (Exception.))) cb))
(dotimes [i 5]
(try (fail) (catch Exception e)))
(assert (= (ClosedState. default-policy 5) @cb))
(try (fail) (catch Exception e))
@krukow
krukow / swipe-uitouch.m
Created June 10, 2011 12:31
swipt-UITouch not working
- (UIQuery *)swipe {
[[UIQueryExpectation withQuery:self] exist:@"before you can swipe it"];
for (UIView *view in [self targetViews]) {
UITouch *touch = [[UITouch alloc] initInView:view];
UIEvent *eventDown = [[NSClassFromString(@"UITouchesEvent") alloc] initWithTouch:touch];
NSSet *touches = [[NSMutableSet alloc] initWithObjects:&touch count:1];
[touch.view touchesBegan:touches withEvent:eventDown];
@krukow
krukow / UIQuery.m
Created June 13, 2011 18:54
structs from iCuke
...
- (UIQuery *)swipe {
[[UIQueryExpectation withQuery:self] exist:@"before you can swipe it"];
for (UIView *view in [self targetViews]) {
NSMutableArray* events = [NSMutableArray
arrayWithCapacity:10];
for (NSMutableDictionary *event in [self swipeEventsForView:view]) {
@krukow
krukow / example.m
Created June 20, 2011 06:34
synthesize move
- (UIQuery *)swipe {
postMouseEvent(96,97,0);
postMouseEvent(96,97,1);
[self performSelector:@selector(postEvent) withObject:nil afterDelay:2];
//...
}
- (void) postEvent {
for (int i=0;i<150;i++) {
// [self performSelector:@selector(postEvent) withObject:nil afterDelay:0.2+0.01*i];
@krukow
krukow / production.txt
Created October 6, 2011 16:23
Closure compiler options - production
//line breaks inserted for readability
java -jar closure-compiler/compiler.jar --compilation_level ADVANCED_OPTIMIZATIONS --define goog.DEBUG=false --module_output_path_prefix public/2011-10-06_18-20-43_compiled_ --module chatpp_modules:97 --module chatpp_storage_module:2:chatpp_modules
--js public/closure-library/closure/goog/deps.js
--js public/closure-library/closure/goog/base.js
--js public/closure-library/closure/goog/debug/errorhandlerweakdep.js
...
--js public/chatpp/model/user/User.js
--js public/chatpp/events/AppEvent.js
--js public/chatpp/model/message/Message.js
--js public/chatpp/model/model.js