forked from probe-lab/zikade
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backend_provider_test.go
179 lines (139 loc) · 4.24 KB
/
backend_provider_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package zikade
import (
"context"
"io"
"sync"
"testing"
"time"
"github.com/benbjohnson/clock"
ds "github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/exp/slog"
"github.com/plprobelab/zikade/internal/kadtest"
)
var devnull = slog.New(slog.NewTextHandler(io.Discard, nil))
func newBackendProvider(t testing.TB, cfg *ProvidersBackendConfig) *ProvidersBackend {
h := newTestHost(t, libp2p.NoListenAddrs)
dstore, err := InMemoryDatastore()
require.NoError(t, err)
t.Cleanup(func() {
if err = dstore.Close(); err != nil {
t.Logf("closing datastore: %s", err)
}
if err = h.Close(); err != nil {
t.Logf("closing host: %s", err)
}
})
b, err := NewBackendProvider(h.Peerstore(), dstore, cfg)
require.NoError(t, err)
return b
}
func TestProvidersBackend_GarbageCollection(t *testing.T) {
clk := clock.NewMock()
cfg, err := DefaultProviderBackendConfig()
require.NoError(t, err)
cfg.clk = clk
cfg.Logger = devnull
b := newBackendProvider(t, cfg)
// start the garbage collection process
b.StartGarbageCollection()
t.Cleanup(func() { b.StopGarbageCollection() })
// write random record to datastore and peerstore
ctx := context.Background()
p := newAddrInfo(t)
// write to datastore
dsKey := newDatastoreKey(namespaceProviders, "random-key", string(p.ID))
rec := expiryRecord{expiry: clk.Now()}
err = b.datastore.Put(ctx, dsKey, rec.MarshalBinary())
require.NoError(t, err)
// write to peerstore
b.addrBook.AddAddrs(p.ID, p.Addrs, time.Hour)
// advance clock half the validity time and check if record is still there
clk.Add(cfg.ProvideValidity / 2)
// we expect the record to still be there after half the ProvideValidity
_, err = b.datastore.Get(ctx, dsKey)
require.NoError(t, err)
// advance clock another time and check if the record was GC'd now
clk.Add(cfg.ProvideValidity + cfg.GCInterval)
// we expect the record to be GC'd now
val, err := b.datastore.Get(ctx, dsKey)
assert.ErrorIs(t, err, ds.ErrNotFound)
assert.Nil(t, val)
}
func TestProvidersBackend_GarbageCollection_lifecycle_thread_safe(t *testing.T) {
cfg, err := DefaultProviderBackendConfig()
require.NoError(t, err)
cfg.Logger = devnull
b := newBackendProvider(t, cfg)
var wg sync.WaitGroup
wg.Add(1)
go func() {
for i := 0; i < 100; i++ {
b.StartGarbageCollection()
}
wg.Done()
}()
wg.Add(1)
go func() {
for i := 0; i < 100; i++ {
b.StopGarbageCollection()
}
wg.Done()
}()
wg.Wait()
b.StopGarbageCollection()
assert.Nil(t, b.gcCancel)
assert.Nil(t, b.gcDone)
}
func TestProvidersBackend_Validate(t *testing.T) {
ctx := kadtest.CtxShort(t)
b := newBackendProvider(t, nil)
pid := newPeerID(t)
peer1 := peer.AddrInfo{ID: pid, Addrs: make([]multiaddr.Multiaddr, 0)}
peer2 := peer.AddrInfo{ID: pid, Addrs: make([]multiaddr.Multiaddr, 1)}
peer3 := peer.AddrInfo{ID: pid, Addrs: make([]multiaddr.Multiaddr, 2)}
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", peer1)
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", peer1, peer2, peer3)
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", peer1, peer3, peer2)
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", peer1, nil, peer2, nil)
assert.NoError(t, err)
assert.Equal(t, 2, idx)
})
t.Run("identically good values", func(t *testing.T) {
idx, err := b.Validate(ctx, "some-key", peer1, peer1)
assert.NoError(t, err)
assert.Equal(t, 0, idx)
})
}