Changeset 309

Show
Ignore:
Timestamp:
02/26/08 03:47:04 (9 months ago)
Author:
rm
Message:

added unit tests for circuit breaker. added logging and fixed found bugs

Location:
trunk/thrucommon
Files:
1 added
3 modified

Legend:

Unmodified
Added
Removed
  • trunk/thrucommon/src/CircuitBreaker.cpp

    r281 r309  
    1515                                uint16_t timeout_in_seconds) 
    1616{ 
     17    char buf[64]; 
     18    sprintf (buf, "CircuitBreaker: threshold=%d, timeout_in_seconds=%d", 
     19             threshold, timeout_in_seconds); 
     20    LOG4CXX_INFO (logger, buf); 
     21 
    1722    this->threshold = threshold; 
    1823    this->timeout_in_seconds = timeout_in_seconds; 
     
    2328bool CircuitBreaker::allow () 
    2429{ 
     30    LOG4CXX_DEBUG (logger, "allow: "); 
    2531    // if timeout has elapsed give half open a try 
    26     if (this->next_check > time (0)) 
     32    if (this->state == OPEN && this->next_check < time (0)) 
    2733    { 
     34        LOG4CXX_INFO (logger, "allow:    going half-open"); 
    2835        this->state = HALF_OPEN; 
    2936    } 
     
    3340void CircuitBreaker::success () 
    3441{ 
     42    LOG4CXX_DEBUG (logger, "success: "); 
    3543    // if we're half-open and got a success close things up, note that 
    3644    // we should never be called if we're in the fully open state, that'd be a  
     
    3846    if (this->state == HALF_OPEN) 
    3947    { 
     48        LOG4CXX_INFO (logger, "success:    in half-open, reset"); 
    4049        this->reset (); 
    4150    } 
     
    4453void CircuitBreaker::failure () 
    4554{ 
     55    LOG4CXX_DEBUG (logger, "failure: "); 
    4656    if (this->state == HALF_OPEN) 
    4757    { 
     58        LOG4CXX_INFO (logger, "failure:    in half-open, trip"); 
    4859        this->trip (); 
    4960    } 
     
    5465        if (failure_count > this->threshold) 
    5566        { 
    56             this->reset (); 
     67            LOG4CXX_INFO (logger, "failure:    threashold breached,  trip"); 
     68            this->trip (); 
    5769        } 
    5870    } 
     
    6173void CircuitBreaker::reset () 
    6274{ 
     75    LOG4CXX_INFO (logger, "reset:"); 
    6376    this->state = CLOSED; 
    6477    this->failure_count = 0; 
     
    6780void CircuitBreaker::trip () 
    6881{ 
     82    LOG4CXX_INFO (logger, "trip:"); 
    6983    if (this->state != OPEN) 
    7084    { 
     85        LOG4CXX_INFO (logger, "trip: tripped"); 
    7186        this->state = OPEN; 
    72         this->next_check = time (0) + timeout_in_seconds; 
     87        this->next_check = time (0) + (time_t)timeout_in_seconds; 
    7388    } 
    7489} 
  • trunk/thrucommon/tests

    • Property svn:ignore
      •  

        old new  
         1CircuitBreakerTest 
        12.deps 
        23.libs 
  • trunk/thrucommon/tests/Makefile.am

    r306 r309  
    44LDADDS = ../src/libthrucommon.la 
    55 
    6 TESTS = SpreadTest 
     6TESTS = SpreadTest CircuitBreakerTest 
    77 
    88check_PROGRAMS=$(TESTS) 
     
    1111SpreadTest_LDADD = $(LDADDS) 
    1212SpreadTest_CXXFLAGS =  $(CPPUNIT_CFLAGS) -I../src 
    13 SpreadTest_LDFLAGS = $(CPPUNIT_LIBS) $(SPEAD_LIBS) $(SSL_LIBS) $(THRIFT_LIBS) $(UUID_LIBS) $(MEMCACHED_LIBS) 
     13SpreadTest_LDFLAGS = $(CPPUNIT_LIBS) $(SPEAD_LIBS) $(THRIFT_LIBS)  
     14 
     15CircuitBreakerTest_SOURCES = CircuitBreakerTest.cpp 
     16CircuitBreakerTest_LDADD = $(LDADDS) 
     17CircuitBreakerTest_CXXFLAGS =  $(CPPUNIT_CFLAGS) -I../src 
     18CircuitBreakerTest_LDFLAGS = $(CPPUNIT_LIBS)