public void DefaultStateIsClosed()
        {
            // Fixture setup
            var fixture = new WcfFixture();
            var sut     = fixture.CreateAnonymous <CircuitBreaker>();
            // Exercise system
            ICircuitState result = sut.State;

            // Verify outcome
            Assert.IsAssignableFrom <ClosedCircuitState>(result);
            // Teardown
        }
Example #2
0
        private void SwitchToState(ICircuitState state)
        {
            var currentState = _currentState;

            if (currentState != state &&
                Interlocked.CompareExchange(ref _currentState, state, currentState) == currentState)
            {
                state.Entered();
                try
                {
                    _onStateChange?.Invoke(this, state.Status);
                }
                catch (Exception)
                { }
            }
        }
Example #3
0
        internal void OnFailure(ICircuitState state)
        {
            switch (state.Status)
            {
            case CircuitStatus.Closed:
                SwitchToState(_halfOpenState);
                break;

            case CircuitStatus.HalfOpen:
                SwitchToState(_openState);
                break;

            case CircuitStatus.Open:
                SwitchToState(_closedState);
                break;
            }
        }
Example #4
0
        public CircuitBreaker(CircuitPolicy policy    = null,
                              ICircuitInvoker invoker = null,
                              Action <CircuitBreaker, Exception> onFailure         = null,
                              Action <CircuitBreaker, CircuitStatus> onStateChange = null)
        {
            _onFailure     = onFailure;
            _onStateChange = onStateChange;

            _policy = policy ?? CircuitPolicy.DefaultPolicy;

            invoker = invoker ?? DefaultInvoker;

            _closedState   = new ClosedState(this, _policy, invoker);
            _halfOpenState = new HalfOpenState(this, _policy, invoker);
            _openState     = new OpenState(this, _policy, invoker);

            _currentState = _closedState;
        }
Example #5
0
 public CircuitBreaker(TimeSpan timeout)
 {
     this.state = new ClosedCircuitState(timeout);
 }
Example #6
0
 public void Succeed()
 {
     this.state = this.state.NextState();
     this.state.Succeed();
     this.state = this.state.NextState();
 }
Example #7
0
 public void Trip(Exception e)
 {
     this.state = this.state.NextState();
     this.state.Trip(e);
     this.state = this.state.NextState();
 }
Example #8
0
 public void Guard()
 {
     this.state = this.state.NextState();
     this.state.Guard();
     this.state = this.state.NextState();
 }
 public void Succeed()
 {
     this.state = this.state.NextState();
     this.state.Succeed();
     this.state = this.state.NextState();
 }
 public void Trip(Exception e)
 {
     this.state = this.state.NextState();
     this.state.Trip(e);
     this.state = this.state.NextState();
 }
 public void Guard()
 {
     this.state = this.state.NextState();
     this.state.Guard();
     this.state = this.state.NextState();
 }
 public CircuitBreaker(TimeSpan timeout)
 {
     this.state = new ClosedCircuitState(timeout);
 }
Example #13
0
 public void Trip(Exception exception)
 {
     this.State = this.State.NextState();
     this.State.Trip(exception);
     this.State = this.State.NextState();
 }
Example #14
0
 internal void OnSuccess(ICircuitState state)
 {
     SwitchToState(_closedState);
 }