-
Notifications
You must be signed in to change notification settings - Fork 3
/
backend_record_test.go
114 lines (94 loc) · 2.76 KB
/
backend_record_test.go
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package zikade
import (
"fmt"
"strconv"
"strings"
"testing"
record "github.com/libp2p/go-libp2p-record"
"github.com/stretchr/testify/assert"
"github.com/probe-lab/zikade/internal/kadtest"
)
// testValidator is a validator that considers all values valid that have a
// "valid-" prefix. Then the suffix will determine which value is better. For
// example, "valid-2" is better than "valid-1".
type testValidator struct{}
var _ record.Validator = (*testValidator)(nil)
func (t testValidator) Validate(key string, value []byte) error {
if strings.HasPrefix(string(value), "valid-") {
return nil
}
return fmt.Errorf("invalid value")
}
func (t testValidator) Select(key string, values [][]byte) (int, error) {
idx := -1
best := -1
for i, val := range values {
if !strings.HasPrefix(string(val), "valid-") {
continue
}
newBest, err := strconv.Atoi(string(val)[6:])
if err != nil {
continue
}
if newBest > best {
idx = i
best = newBest
}
}
if idx == -1 {
return idx, fmt.Errorf("no valid value")
}
return idx, nil
}
func TestRecordBackend_Validate(t *testing.T) {
ctx := kadtest.CtxShort(t)
b := &RecordBackend{
namespace: "test",
validator: &testValidator{},
}
t.Run("no values", func(t *testing.T) {
idx, err := b.Validate(ctx, "some-key")
assert.Error(t, err)
assert.Equal(t, -1, idx)
})
t.Run("nil value", func(t *testing.T) {
idx, err := b.Validate(ctx, "some-key", nil)
assert.Error(t, err)
assert.Equal(t, -1, idx)
})
t.Run("nil values", func(t *testing.T) {
idx, err := b.Validate(ctx, "some-key", nil, nil)
assert.Error(t, err)
assert.Equal(t, -1, idx)
})
t.Run("single valid value", func(t *testing.T) {
idx, err := b.Validate(ctx, "some-key", []byte("valid-0"))
assert.NoError(t, err)
assert.Equal(t, 0, idx)
})
t.Run("increasing better values", func(t *testing.T) {
idx, err := b.Validate(ctx, "some-key", []byte("valid-0"), []byte("valid-1"), []byte("valid-2"))
assert.NoError(t, err)
assert.Equal(t, 2, idx)
})
t.Run("mixed better values", func(t *testing.T) {
idx, err := b.Validate(ctx, "some-key", []byte("valid-0"), []byte("valid-2"), []byte("valid-1"))
assert.NoError(t, err)
assert.Equal(t, 1, idx)
})
t.Run("mixed invalid values", func(t *testing.T) {
idx, err := b.Validate(ctx, "some-key", []byte("valid-0"), []byte("invalid"), []byte("valid-2"), []byte("invalid"))
assert.NoError(t, err)
assert.Equal(t, 2, idx)
})
t.Run("only invalid values", func(t *testing.T) {
idx, err := b.Validate(ctx, "some-key", []byte("invalid"), nil)
assert.Error(t, err)
assert.Equal(t, -1, idx)
})
t.Run("identically good values", func(t *testing.T) {
idx, err := b.Validate(ctx, "some-key", []byte("valid-0"), []byte("valid-0"))
assert.NoError(t, err)
assert.Equal(t, 0, idx)
})
}