From 364e108d171d39f1c39fd301bd483bff8839b944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fukan=20Teber?= Date: Mon, 27 Mar 2023 14:00:43 +0300 Subject: [PATCH 1/2] Add unit test for resolveConflicts function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Doğukan Teber --- pkg/ring/model_test.go | 84 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) diff --git a/pkg/ring/model_test.go b/pkg/ring/model_test.go index f20eb411755..fb24b31ebe8 100644 --- a/pkg/ring/model_test.go +++ b/pkg/ring/model_test.go @@ -1,6 +1,7 @@ package ring import ( + reflect "reflect" "testing" "time" @@ -600,3 +601,86 @@ 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: []uint32{}}, + }, + }, + { + 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: []uint32{}}, + "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 { + expectedInstance := tt.want[key] + if actualInstance.State != expectedInstance.State || !reflect.DeepEqual(actualInstance.Tokens, expectedInstance.Tokens) { + if len(actualInstance.Tokens) == len(expectedInstance.Tokens) && len(actualInstance.Tokens) != 0 { + t.Errorf("resolveConflicts() for key %s = %v, want %v", key, actualInstance, expectedInstance) + } + } + } + }) + } +} From 27664c88028d6d3e2bee8d2037634d2bf98055e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fukan=20Teber?= Date: Thu, 30 Mar 2023 22:47:28 +0300 Subject: [PATCH 2/2] Clean up the test logic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Doğukan Teber --- pkg/ring/model_test.go | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-) diff --git a/pkg/ring/model_test.go b/pkg/ring/model_test.go index fb24b31ebe8..63fd4a2e693 100644 --- a/pkg/ring/model_test.go +++ b/pkg/ring/model_test.go @@ -1,7 +1,6 @@ package ring import ( - reflect "reflect" "testing" "time" @@ -632,7 +631,7 @@ func Test_resolveConflicts(t *testing.T) { }, want: map[string]InstanceDesc{ "ing1": {State: ACTIVE, Tokens: []uint32{1, 2, 3}}, - "ing2": {State: LEFT, Tokens: []uint32{}}, + "ing2": {State: LEFT, Tokens: nil}, }, }, { @@ -642,7 +641,7 @@ func Test_resolveConflicts(t *testing.T) { "ing2": {State: PENDING, Tokens: []uint32{1, 2, 3, 4}}, }, want: map[string]InstanceDesc{ - "ing1": {State: LEAVING, Tokens: []uint32{}}, + "ing1": {State: LEAVING, Tokens: nil}, "ing2": {State: PENDING, Tokens: []uint32{1, 2, 3, 4}}, }, }, @@ -674,12 +673,7 @@ func Test_resolveConflicts(t *testing.T) { t.Run(tt.name, func(t *testing.T) { resolveConflicts(tt.args) for key, actualInstance := range tt.args { - expectedInstance := tt.want[key] - if actualInstance.State != expectedInstance.State || !reflect.DeepEqual(actualInstance.Tokens, expectedInstance.Tokens) { - if len(actualInstance.Tokens) == len(expectedInstance.Tokens) && len(actualInstance.Tokens) != 0 { - t.Errorf("resolveConflicts() for key %s = %v, want %v", key, actualInstance, expectedInstance) - } - } + assert.Equal(t, actualInstance, tt.want[key]) } }) }