File: predicates_test.go

package info (click to toggle)
golang-github-vulcand-oxy 2.0.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 728 kB
  • sloc: makefile: 14
file content (68 lines) | stat: -rw-r--r-- 1,685 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cbreaker

import (
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"github.com/vulcand/oxy/v2/internal/holsterv4/clock"
	"github.com/vulcand/oxy/v2/memmetrics"
)

func TestTripped(t *testing.T) {
	testCases := []struct {
		expression string
		metrics    *memmetrics.RTMetrics
		expected   bool
	}{
		{
			expression: "NetworkErrorRatio() > 0.5",
			metrics:    statsNetErrors(0.6),
			expected:   true,
		},
		{
			expression: "NetworkErrorRatio() < 0.5",
			metrics:    statsNetErrors(0.6),
			expected:   false,
		},
		{
			expression: "LatencyAtQuantileMS(50.0) > 50",
			metrics:    statsLatencyAtQuantile(50, clock.Millisecond*51),
			expected:   true,
		},
		{
			expression: "LatencyAtQuantileMS(50.0) < 50",
			metrics:    statsLatencyAtQuantile(50, clock.Millisecond*51),
			expected:   false,
		},
		{
			expression: "ResponseCodeRatio(500, 600, 0, 600) > 0.5",
			metrics:    statsResponseCodes(statusCode{Code: 200, Count: 5}, statusCode{Code: 500, Count: 6}),
			expected:   true,
		},
		{
			expression: "ResponseCodeRatio(500, 600, 0, 600) > 0.5",
			metrics:    statsResponseCodes(statusCode{Code: 200, Count: 5}, statusCode{Code: 500, Count: 4}),
			expected:   false,
		},
		{
			// quantile not defined
			expression: "LatencyAtQuantileMS(40.0) > 50",
			metrics:    statsNetErrors(0.6),
			expected:   false,
		},
	}

	for _, test := range testCases {
		test := test
		t.Run(test.expression, func(t *testing.T) {
			t.Parallel()

			p, err := parseExpression(test.expression)
			require.NoError(t, err)
			require.NotNil(t, p)

			assert.Equal(t, test.expected, p(&CircuitBreaker{metrics: test.metrics}))
		})
	}
}