MCPcopy
hub / github.com/grafana/dskit / TestPartitionRingDesc_AddOrUpdateOwner

Function TestPartitionRingDesc_AddOrUpdateOwner

ring/partition_ring_model_test.go:128–184  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

126}
127
128func TestPartitionRingDesc_AddOrUpdateOwner(t *testing.T) {
129 now := time.Now()
130
131 t.Run("should add a new owner", func(t *testing.T) {
132 desc := NewPartitionRingDesc()
133 require.True(t, desc.AddOrUpdateOwner("instance-1", OwnerActive, 1, now))
134
135 assert.Equal(t, &PartitionRingDesc{
136 Partitions: map[int32]PartitionDesc{},
137 Owners: map[string]OwnerDesc{
138 "instance-1": {
139 UpdatedTimestamp: now.Unix(),
140 State: OwnerActive,
141 OwnedPartition: 1,
142 },
143 },
144 }, desc)
145 })
146
147 t.Run("should update an existing owner", func(t *testing.T) {
148 desc := NewPartitionRingDesc()
149 require.True(t, desc.AddOrUpdateOwner("instance-1", OwnerActive, 1, now))
150
151 // Update the owner.
152 require.True(t, desc.AddOrUpdateOwner("instance-1", OwnerActive, 2, now.Add(time.Second)))
153
154 assert.Equal(t, &PartitionRingDesc{
155 Partitions: map[int32]PartitionDesc{},
156 Owners: map[string]OwnerDesc{
157 "instance-1": {
158 UpdatedTimestamp: now.Add(time.Second).Unix(),
159 State: OwnerActive,
160 OwnedPartition: 2,
161 },
162 },
163 }, desc)
164 })
165
166 t.Run("should be a no-op if the owner already exist", func(t *testing.T) {
167 desc := NewPartitionRingDesc()
168 desc.AddOrUpdateOwner("instance-1", OwnerActive, 1, now)
169
170 // Update the owner.
171 require.False(t, desc.AddOrUpdateOwner("instance-1", OwnerActive, 1, now.Add(time.Second)))
172
173 assert.Equal(t, &PartitionRingDesc{
174 Partitions: map[int32]PartitionDesc{},
175 Owners: map[string]OwnerDesc{
176 "instance-1": {
177 UpdatedTimestamp: now.Unix(), // Timestamp should not be updated.
178 State: OwnerActive,
179 OwnedPartition: 1,
180 },
181 },
182 }, desc)
183 })
184}
185

Callers

nothing calls this directly

Calls 5

AddOrUpdateOwnerMethod · 0.95
NewPartitionRingDescFunction · 0.85
RunMethod · 0.80
AddMethod · 0.65
EqualMethod · 0.45

Tested by

no test coverage detected