Commit 6eaa4bc6 authored by helei's avatar helei

add unit test for circuit breaker

parent a2bb114e
...@@ -22,8 +22,8 @@ const int kShortWindowSize = 500; ...@@ -22,8 +22,8 @@ const int kShortWindowSize = 500;
const int kLongWindowSize = 1000; const int kLongWindowSize = 1000;
const int kShortWindowErrorPercent = 10; const int kShortWindowErrorPercent = 10;
const int kLongWindowErrorPercent = 5; const int kLongWindowErrorPercent = 5;
const int kMinIsolationDurationMs = 100; const int kMinIsolationDurationMs = 10;
const int kMaxIsolationDurationMs = 1000; const int kMaxIsolationDurationMs = 200;
const int kErrorCodeForFailed = 131; const int kErrorCodeForFailed = 131;
const int kErrorCodeForSucc = 0; const int kErrorCodeForSucc = 0;
const int kErrorCost = 1000; const int kErrorCost = 1000;
...@@ -160,7 +160,6 @@ TEST_F(CircuitBreakerTest, isolation_duration_grow) { ...@@ -160,7 +160,6 @@ TEST_F(CircuitBreakerTest, isolation_duration_grow) {
EXPECT_EQ(_circuit_breaker.isolation_duration_ms(), kMinIsolationDurationMs * 2); EXPECT_EQ(_circuit_breaker.isolation_duration_ms(), kMinIsolationDurationMs * 2);
_circuit_breaker.Reset(); _circuit_breaker.Reset();
bthread_usleep(kMinIsolationDurationMs * 1000);
StartFeedbackThread(&thread_list, &fc_list, 100); StartFeedbackThread(&thread_list, &fc_list, 100);
for (int i = 0; i < kThreadNum; ++i) { for (int i = 0; i < kThreadNum; ++i) {
void* ret_data = NULL; void* ret_data = NULL;
...@@ -173,7 +172,25 @@ TEST_F(CircuitBreakerTest, isolation_duration_grow) { ...@@ -173,7 +172,25 @@ TEST_F(CircuitBreakerTest, isolation_duration_grow) {
EXPECT_EQ(_circuit_breaker.isolation_duration_ms(), kMinIsolationDurationMs * 4); EXPECT_EQ(_circuit_breaker.isolation_duration_ms(), kMinIsolationDurationMs * 4);
_circuit_breaker.Reset(); _circuit_breaker.Reset();
bthread_usleep((kMaxIsolationDurationMs + kMinIsolationDurationMs) * 1000); ::usleep((kMaxIsolationDurationMs + kMinIsolationDurationMs) * 1000);
StartFeedbackThread(&thread_list, &fc_list, 100);
for (int i = 0; i < kThreadNum; ++i) {
void* ret_data = NULL;
EXPECT_EQ(pthread_join(thread_list[i], &ret_data), 0);
FeedbackControl* fc = static_cast<FeedbackControl*>(ret_data);
EXPECT_FALSE(fc->_healthy);
EXPECT_LE(fc->_healthy_cnt, kShortWindowSize);
EXPECT_GT(fc->_unhealthy_cnt, 0);
}
EXPECT_EQ(_circuit_breaker.isolation_duration_ms(), kMinIsolationDurationMs * 8);
}
TEST_F(CircuitBreakerTest, isolation_duration_reset) {
std::vector<pthread_t> thread_list;
std::vector<std::unique_ptr<FeedbackControl>> fc_list;
_circuit_breaker.Reset();
_circuit_breaker.OnCallEnd(kErrorCodeForFailed, kLatency);
::usleep((kMaxIsolationDurationMs + kMinIsolationDurationMs) * 1000);
StartFeedbackThread(&thread_list, &fc_list, 100); StartFeedbackThread(&thread_list, &fc_list, 100);
for (int i = 0; i < kThreadNum; ++i) { for (int i = 0; i < kThreadNum; ++i) {
void* ret_data = NULL; void* ret_data = NULL;
...@@ -185,3 +202,20 @@ TEST_F(CircuitBreakerTest, isolation_duration_grow) { ...@@ -185,3 +202,20 @@ TEST_F(CircuitBreakerTest, isolation_duration_grow) {
} }
EXPECT_EQ(_circuit_breaker.isolation_duration_ms(), kMinIsolationDurationMs); EXPECT_EQ(_circuit_breaker.isolation_duration_ms(), kMinIsolationDurationMs);
} }
TEST_F(CircuitBreakerTest, isolation_duration_compute) {
std::vector<pthread_t> thread_list;
std::vector<std::unique_ptr<FeedbackControl>> fc_list;
_circuit_breaker.Reset();
::usleep((kMaxIsolationDurationMs + kMinIsolationDurationMs) * 1000);
StartFeedbackThread(&thread_list, &fc_list, 100);
for (int i = 0; i < kThreadNum; ++i) {
void* ret_data = NULL;
EXPECT_EQ(pthread_join(thread_list[i], &ret_data), 0);
FeedbackControl* fc = static_cast<FeedbackControl*>(ret_data);
EXPECT_FALSE(fc->_healthy);
EXPECT_LE(fc->_healthy_cnt, kShortWindowSize);
EXPECT_GT(fc->_unhealthy_cnt, 0);
}
EXPECT_EQ(_circuit_breaker.isolation_duration_ms(), 2 * kMinIsolationDurationMs);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment