forked from dgraph-io/ristretto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ring_test.go
70 lines (63 loc) · 1.23 KB
/
ring_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
package ristretto
import (
"sync"
"testing"
"github.com/stretchr/testify/require"
)
type testConsumer struct {
push func([]uint64)
save bool
}
func (c *testConsumer) Push(items []uint64) bool {
if c.save {
c.push(items)
return true
}
return false
}
func TestRingDrain(t *testing.T) {
drains := 0
r := newRingBuffer(&testConsumer{
push: func(items []uint64) {
drains++
},
save: true,
}, 1)
for i := 0; i < 100; i++ {
r.Push(uint64(i))
}
require.Equal(t, 100, drains, "buffers shouldn't be dropped with BufferItems == 1")
}
func TestRingReset(t *testing.T) {
drains := 0
r := newRingBuffer(&testConsumer{
push: func(items []uint64) {
drains++
},
save: false,
}, 4)
for i := 0; i < 100; i++ {
r.Push(uint64(i))
}
require.Equal(t, 0, drains, "testConsumer shouldn't be draining")
}
func TestRingConsumer(t *testing.T) {
mu := &sync.Mutex{}
drainItems := make(map[uint64]struct{})
r := newRingBuffer(&testConsumer{
push: func(items []uint64) {
mu.Lock()
defer mu.Unlock()
for i := range items {
drainItems[items[i]] = struct{}{}
}
},
save: true,
}, 4)
for i := 0; i < 100; i++ {
r.Push(uint64(i))
}
l := len(drainItems)
require.NotEqual(t, 0, l)
require.True(t, l <= 100)
}