diff --git a/pkg/ring/model_test.go b/pkg/ring/model_test.go index f20eb411755..63fd4a2e693 100644 --- a/pkg/ring/model_test.go +++ b/pkg/ring/model_test.go @@ -600,3 +600,81 @@ func TestDesc_FindDifference(t *testing.T) { }) } } + +func Test_resolveConflicts(t *testing.T) { + tests := []struct { + name string + args map[string]InstanceDesc + want map[string]InstanceDesc + }{ + { + name: "Empty input", + args: map[string]InstanceDesc{}, + want: map[string]InstanceDesc{}, + }, + { + name: "No conflicts", + args: map[string]InstanceDesc{ + "ing1": {State: ACTIVE, Tokens: []uint32{1, 2, 3}}, + "ing2": {State: ACTIVE, Tokens: []uint32{4, 5, 6}}, + }, + want: map[string]InstanceDesc{ + "ing1": {State: ACTIVE, Tokens: []uint32{1, 2, 3}}, + "ing2": {State: ACTIVE, Tokens: []uint32{4, 5, 6}}, + }, + }, + { + name: "Conflict resolution with LEFT state", + args: map[string]InstanceDesc{ + "ing1": {State: ACTIVE, Tokens: []uint32{1, 2, 3}}, + "ing2": {State: LEFT, Tokens: []uint32{1, 2, 3, 4}}, + }, + want: map[string]InstanceDesc{ + "ing1": {State: ACTIVE, Tokens: []uint32{1, 2, 3}}, + "ing2": {State: LEFT, Tokens: nil}, + }, + }, + { + name: "Conflict resolution with LEAVING state", + args: map[string]InstanceDesc{ + "ing1": {State: LEAVING, Tokens: []uint32{1, 2, 3}}, + "ing2": {State: PENDING, Tokens: []uint32{1, 2, 3, 4}}, + }, + want: map[string]InstanceDesc{ + "ing1": {State: LEAVING, Tokens: nil}, + "ing2": {State: PENDING, Tokens: []uint32{1, 2, 3, 4}}, + }, + }, + { + name: "Conflict resolution with JOINING state", + args: map[string]InstanceDesc{ + "ing1": {State: ACTIVE, Tokens: []uint32{1, 2, 3}}, + "ing2": {State: JOINING, Tokens: []uint32{1, 2, 3, 4}}, + }, + want: map[string]InstanceDesc{ + "ing1": {State: ACTIVE, Tokens: []uint32{1, 2, 3}}, + "ing2": {State: JOINING, Tokens: []uint32{4}}, + }, + }, + { + name: "Conflict resolution with same state", + args: map[string]InstanceDesc{ + "ing1": {State: ACTIVE, Tokens: []uint32{1, 2, 3}}, + "ing2": {State: ACTIVE, Tokens: []uint32{1, 2, 3, 4}}, + }, + want: map[string]InstanceDesc{ + "ing1": {State: ACTIVE, Tokens: []uint32{1, 2, 3}}, + "ing2": {State: ACTIVE, Tokens: []uint32{4}}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + resolveConflicts(tt.args) + for key, actualInstance := range tt.args { + assert.Equal(t, actualInstance, tt.want[key]) + } + }) + } +}