|
| 1 | +package throttle |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/github/freno/pkg/base" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | +) |
| 10 | + |
| 11 | +func Test_checkAppMetricResult(t *testing.T) { |
| 12 | + throttler := NewThrottler() |
| 13 | + |
| 14 | + t.Run("threshold exceeded", func(t *testing.T) { |
| 15 | + check := NewThrottlerCheck(throttler) |
| 16 | + metricResultFunc := func() (metricResult base.MetricResult, threshold float64) { |
| 17 | + return base.NewSimpleMetricResult(10.0), 1.0 |
| 18 | + } |
| 19 | + result := check.checkAppMetricResult("test-app", "mysql", "test-cluster", metricResultFunc, &CheckFlags{}) |
| 20 | + assert.Equal(t, 10.0, result.Value) |
| 21 | + assert.EqualError(t, result.Error, base.ThresholdExceededError.Error()) |
| 22 | + assert.Equal(t, http.StatusTooManyRequests, result.StatusCode) |
| 23 | + }) |
| 24 | + t.Run("not throttled", func(t *testing.T) { |
| 25 | + check := NewThrottlerCheck(throttler) |
| 26 | + metricResultFunc := func() (metricResult base.MetricResult, threshold float64) { |
| 27 | + return base.NewSimpleMetricResult(1.0), 10.0 |
| 28 | + } |
| 29 | + result := check.checkAppMetricResult("test-app", "mysql", "test-cluster", metricResultFunc, &CheckFlags{}) |
| 30 | + assert.Equal(t, 1.0, result.Value) |
| 31 | + assert.Nil(t, result.Error) |
| 32 | + assert.Equal(t, http.StatusOK, result.StatusCode) |
| 33 | + }) |
| 34 | + t.Run("no hosts", func(t *testing.T) { |
| 35 | + check := NewThrottlerCheck(throttler) |
| 36 | + metricResultFunc := func() (metricResult base.MetricResult, threshold float64) { |
| 37 | + return base.NoHostsMetricResult, 10.0 |
| 38 | + } |
| 39 | + result := check.checkAppMetricResult("test-app", "mysql", "test-cluster", metricResultFunc, &CheckFlags{}) |
| 40 | + assert.Equal(t, 0.0, result.Value) |
| 41 | + assert.EqualError(t, result.Error, base.NoHostsError.Error()) |
| 42 | + assert.Equal(t, http.StatusOK, result.StatusCode) |
| 43 | + }) |
| 44 | + t.Run("low priority denied", func(t *testing.T) { |
| 45 | + check := NewThrottlerCheck(throttler) |
| 46 | + throttler.nonLowPriorityAppRequestsThrottled.SetDefault("mysql/test-cluster", true) |
| 47 | + metricResultFunc := func() (metricResult base.MetricResult, threshold float64) { |
| 48 | + return base.NewSimpleMetricResult(1.0), 10.0 |
| 49 | + } |
| 50 | + result := check.checkAppMetricResult("test-app", "mysql", "test-cluster", metricResultFunc, &CheckFlags{LowPriority: true}) |
| 51 | + assert.EqualError(t, result.Error, base.AppDeniedError.Error()) |
| 52 | + assert.Equal(t, http.StatusExpectationFailed, result.StatusCode) |
| 53 | + |
| 54 | + }) |
| 55 | +} |
0 commit comments