Explorar el Código

Add tests for config error rate type

tags/v2.0.0-rc1
9seconds hace 5 años
padre
commit
5c4536c591
Se han modificado 3 ficheros con 130 adiciones y 11 borrados
  1. 3
    3
      config/config.go
  2. 8
    8
      config/type_error_rate.go
  3. 119
    0
      config/type_error_rate_test.go

+ 3
- 3
config/config.go Ver fichero

@@ -22,9 +22,9 @@ type Config struct {
22 22
 			AllowSkewness TypeDuration `json:"allow-skewness"`
23 23
 		} `json:"time"`
24 24
 		AntiReplay struct {
25
-			Enabled   bool      `json:"enabled"`
26
-			MaxSize   TypeBytes `json:"max-size"`
27
-			ErrorRate TypeFloat `json:"error-rate"`
25
+			Enabled   bool          `json:"enabled"`
26
+			MaxSize   TypeBytes     `json:"max-size"`
27
+			ErrorRate TypeErrorRate `json:"error-rate"`
28 28
 		} `json:"anti-replay"`
29 29
 	} `json:"probes"`
30 30
 	Network struct {

config/type_float.go → config/type_error_rate.go Ver fichero

@@ -5,18 +5,18 @@ import (
5 5
 	"strconv"
6 6
 )
7 7
 
8
-type TypeFloat struct {
8
+type TypeErrorRate struct {
9 9
 	value float64
10 10
 }
11 11
 
12
-func (c *TypeFloat) UnmarshalJSON(data []byte) error {
12
+func (c *TypeErrorRate) UnmarshalJSON(data []byte) error {
13 13
 	value, err := strconv.ParseFloat(string(data), 64)
14 14
 	if err != nil {
15 15
 		return fmt.Errorf("incorrect float value: %w", err)
16 16
 	}
17 17
 
18
-	if value < 0 {
19
-		return fmt.Errorf("%f should be positive", value)
18
+	if value <= 0 || value >= 100 {
19
+		return fmt.Errorf("%f should be 0 < x < 100", value)
20 20
 	}
21 21
 
22 22
 	c.value = value
@@ -24,16 +24,16 @@ func (c *TypeFloat) UnmarshalJSON(data []byte) error {
24 24
 	return nil
25 25
 }
26 26
 
27
-func (c *TypeFloat) MarshalText() ([]byte, error) {
27
+func (c *TypeErrorRate) MarshalText() ([]byte, error) {
28 28
 	return []byte(c.String()), nil
29 29
 }
30 30
 
31
-func (c TypeFloat) String() string {
31
+func (c TypeErrorRate) String() string {
32 32
 	return strconv.FormatFloat(c.value, 'f', -1, 64)
33 33
 }
34 34
 
35
-func (c TypeFloat) Value(defaultValue float64) float64 {
36
-	if c.value < 0.00001 {
35
+func (c TypeErrorRate) Value(defaultValue float64) float64 {
36
+	if c.value < 1e-8 {
37 37
 		return defaultValue
38 38
 	}
39 39
 

+ 119
- 0
config/type_error_rate_test.go Ver fichero

@@ -0,0 +1,119 @@
1
+package config_test
2
+
3
+import (
4
+	"encoding/json"
5
+	"strconv"
6
+	"testing"
7
+
8
+	"github.com/9seconds/mtg/v2/config"
9
+	"github.com/stretchr/testify/assert"
10
+	"github.com/stretchr/testify/suite"
11
+)
12
+
13
+type typeErrorRateTestStruct struct {
14
+	Value config.TypeErrorRate `json:"value"`
15
+}
16
+
17
+type TypeErrorRateTestSuite struct {
18
+	suite.Suite
19
+}
20
+
21
+func (suite *TypeErrorRateTestSuite) TestUnmarshalFail() {
22
+	testData := []float64{
23
+		1000,
24
+		-100,
25
+		-0.0001,
26
+	}
27
+
28
+	for _, v := range testData {
29
+		data, err := json.Marshal(map[string]float64{
30
+			"value": v,
31
+		})
32
+		suite.NoError(err)
33
+
34
+		suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
35
+			assert.Error(t, json.Unmarshal(data, &typeErrorRateTestStruct{}))
36
+		})
37
+	}
38
+}
39
+
40
+func (suite *TypeErrorRateTestSuite) TestUnmarshalOk() {
41
+	testData := []float64{
42
+		1,
43
+		55.5,
44
+		0.0001,
45
+		1e-6,
46
+	}
47
+
48
+	for _, v := range testData {
49
+		value := v
50
+
51
+		data, err := json.Marshal(map[string]float64{
52
+			"value": v,
53
+		})
54
+		suite.NoError(err)
55
+
56
+		suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
57
+			testStruct := &typeErrorRateTestStruct{}
58
+
59
+			assert.NoError(t, json.Unmarshal(data, testStruct))
60
+			assert.InEpsilon(t, value, testStruct.Value.Value(0), 1e-10)
61
+		})
62
+	}
63
+}
64
+
65
+func (suite *TypeErrorRateTestSuite) TestMarshalOk() {
66
+	testData := []float64{
67
+		1,
68
+		55.5,
69
+		0.0001,
70
+		1e-6,
71
+	}
72
+
73
+	for _, v := range testData {
74
+		value := v
75
+
76
+		data, err := json.Marshal(map[string]float64{
77
+			"value": v,
78
+		})
79
+		suite.NoError(err)
80
+
81
+		suite.T().Run(strconv.FormatFloat(v, 'f', -1, 64), func(t *testing.T) {
82
+			testStruct := &typeErrorRateTestStruct{}
83
+
84
+			assert.NoError(t, json.Unmarshal(data, testStruct))
85
+
86
+			parsed, err := strconv.ParseFloat(testStruct.Value.String(), 64)
87
+			assert.NoError(t, err)
88
+			assert.InEpsilon(t, value, parsed, 1e-10)
89
+
90
+			marshalled, err := testStruct.Value.MarshalText()
91
+			assert.NoError(t, err)
92
+
93
+			parsed, err = strconv.ParseFloat(string(marshalled), 64)
94
+			assert.NoError(t, err)
95
+			assert.InEpsilon(t, value, parsed, 1e-10)
96
+		})
97
+	}
98
+}
99
+
100
+func (suite *TypeErrorRateTestSuite) TestValue() {
101
+	testStruct := &typeErrorRateTestStruct{}
102
+
103
+	suite.InEpsilon(1, testStruct.Value.Value(1), 1e-10)
104
+	suite.InEpsilon(2, testStruct.Value.Value(2), 1e-10)
105
+
106
+	data, err := json.Marshal(map[string]float64{
107
+		"value": 1,
108
+	})
109
+	suite.NoError(err)
110
+	suite.NoError(json.Unmarshal(data, testStruct))
111
+
112
+	suite.InEpsilon(1, testStruct.Value.Value(2), 1e-10)
113
+	suite.InEpsilon(1, testStruct.Value.Value(3), 1e-10)
114
+}
115
+
116
+func TestTypeErrorRate(t *testing.T) {
117
+	t.Parallel()
118
+	suite.Run(t, &TypeErrorRateTestSuite{})
119
+}

Loading…
Cancelar
Guardar