-
Notifications
You must be signed in to change notification settings - Fork 67
/
partition_ring_watcher_test.go
105 lines (86 loc) · 3.83 KB
/
partition_ring_watcher_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
package ring
import (
"context"
"strings"
"testing"
"time"
"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/grafana/dskit/kv/consul"
"github.com/grafana/dskit/services"
)
func TestPartitionRingWatcher_ShouldWatchUpdates(t *testing.T) {
const ringKey = "ring"
ctx := context.Background()
logger := log.NewNopLogger()
store, closer := consul.NewInMemoryClient(GetPartitionRingCodec(), logger, nil)
t.Cleanup(func() { assert.NoError(t, closer.Close()) })
reg := prometheus.NewPedanticRegistry()
watcher := NewPartitionRingWatcher("test", ringKey, store, logger, reg)
// PartitionRing should never return nil, even if the watcher hasn't been started yet.
assert.NotNil(t, watcher.PartitionRing())
// Start the watcher with an empty ring.
require.NoError(t, services.StartAndAwaitRunning(ctx, watcher))
t.Cleanup(func() {
require.NoError(t, services.StopAndAwaitTerminated(ctx, watcher))
})
assert.Equal(t, 0, watcher.PartitionRing().PartitionsCount())
assert.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP partition_ring_partitions Number of partitions by state in the partitions ring.
# TYPE partition_ring_partitions gauge
partition_ring_partitions{name="test",state="Pending"} 0
partition_ring_partitions{name="test",state="Active"} 0
partition_ring_partitions{name="test",state="Inactive"} 0
`)))
// Add an ACTIVE partition to the ring.
require.NoError(t, store.CAS(ctx, ringKey, func(in interface{}) (out interface{}, retry bool, err error) {
desc := GetOrCreatePartitionRingDesc(in)
desc.AddPartition(1, PartitionActive, time.Now())
return desc, true, nil
}))
require.Eventually(t, func() bool {
return watcher.PartitionRing().PartitionsCount() == 1
}, time.Second, 10*time.Millisecond)
assert.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP partition_ring_partitions Number of partitions by state in the partitions ring.
# TYPE partition_ring_partitions gauge
partition_ring_partitions{name="test",state="Pending"} 0
partition_ring_partitions{name="test",state="Active"} 1
partition_ring_partitions{name="test",state="Inactive"} 0
`)))
// Add an INACTIVE partition to the ring.
require.NoError(t, store.CAS(ctx, ringKey, func(in interface{}) (out interface{}, retry bool, err error) {
desc := GetOrCreatePartitionRingDesc(in)
desc.AddPartition(2, PartitionInactive, time.Now())
return desc, true, nil
}))
require.Eventually(t, func() bool {
return watcher.PartitionRing().PartitionsCount() == 2
}, time.Second, 10*time.Millisecond)
assert.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP partition_ring_partitions Number of partitions by state in the partitions ring.
# TYPE partition_ring_partitions gauge
partition_ring_partitions{name="test",state="Pending"} 0
partition_ring_partitions{name="test",state="Active"} 1
partition_ring_partitions{name="test",state="Inactive"} 1
`)))
// Add a PENDING partition to the ring.
require.NoError(t, store.CAS(ctx, ringKey, func(in interface{}) (out interface{}, retry bool, err error) {
desc := GetOrCreatePartitionRingDesc(in)
desc.AddPartition(3, PartitionPending, time.Now())
return desc, true, nil
}))
require.Eventually(t, func() bool {
return watcher.PartitionRing().PartitionsCount() == 3
}, time.Second, 10*time.Millisecond)
assert.NoError(t, testutil.GatherAndCompare(reg, strings.NewReader(`
# HELP partition_ring_partitions Number of partitions by state in the partitions ring.
# TYPE partition_ring_partitions gauge
partition_ring_partitions{name="test",state="Pending"} 1
partition_ring_partitions{name="test",state="Active"} 1
partition_ring_partitions{name="test",state="Inactive"} 1
`)))
}