Skip to content

Commit

Permalink
Add unit test for resolveConflicts function (cortexproject#5233)
Browse files Browse the repository at this point in the history
* Add unit test for resolveConflicts function

Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>

* Clean up the test logic

Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>

---------

Signed-off-by: Doğukan Teber <dogukanteber1@hotmail.com>
Signed-off-by: Alex Le <leqiyue@amazon.com>
  • Loading branch information
dogukanteber authored and alexqyle committed May 2, 2023
1 parent a098908 commit b625a16
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions pkg/ring/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}
})
}
}

0 comments on commit b625a16

Please sign in to comment.