From 9a014727804451a4294f3886de63126de0c9938b Mon Sep 17 00:00:00 2001 From: songjiayang Date: Sat, 1 May 2021 00:37:07 +0800 Subject: [PATCH] Refactor processExternalLabels method with slice copy for left labels Signed-off-by: songjiayang --- storage/remote/queue_manager.go | 10 ++-------- storage/remote/queue_manager_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/storage/remote/queue_manager.go b/storage/remote/queue_manager.go index 1dea309c3..44f3df055 100644 --- a/storage/remote/queue_manager.go +++ b/storage/remote/queue_manager.go @@ -727,14 +727,8 @@ func processExternalLabels(ls labels.Labels, externalLabels labels.Labels) label j++ } } - for ; i < len(ls); i++ { - result = append(result, labels.Label{ - Name: ls[i].Name, - Value: ls[i].Value, - }) - } - result = append(result, externalLabels[j:]...) - return result + + return append(append(result, ls[i:]...), externalLabels[j:]...) } func (t *QueueManager) updateShardsLoop() { diff --git a/storage/remote/queue_manager_test.go b/storage/remote/queue_manager_test.go index 4d8b40e98..1424f23c6 100644 --- a/storage/remote/queue_manager_test.go +++ b/storage/remote/queue_manager_test.go @@ -794,6 +794,34 @@ func TestProcessExternalLabels(t *testing.T) { externalLabels: labels.Labels{{Name: "a", Value: "c"}}, expected: labels.Labels{{Name: "a", Value: "b"}}, }, + + // Test empty externalLabels. + { + labels: labels.Labels{{Name: "a", Value: "b"}}, + externalLabels: labels.Labels{}, + expected: labels.Labels{{Name: "a", Value: "b"}}, + }, + + // Test empty labels. + { + labels: labels.Labels{}, + externalLabels: labels.Labels{{Name: "a", Value: "b"}}, + expected: labels.Labels{{Name: "a", Value: "b"}}, + }, + + // Test labels is longer than externalLabels. + { + labels: labels.Labels{{Name: "a", Value: "b"}, {Name: "c", Value: "d"}}, + externalLabels: labels.Labels{{Name: "e", Value: "f"}}, + expected: labels.Labels{{Name: "a", Value: "b"}, {Name: "c", Value: "d"}, {Name: "e", Value: "f"}}, + }, + + // Test externalLabels is longer than labels. + { + labels: labels.Labels{{Name: "c", Value: "d"}}, + externalLabels: labels.Labels{{Name: "a", Value: "b"}, {Name: "e", Value: "f"}}, + expected: labels.Labels{{Name: "a", Value: "b"}, {Name: "c", Value: "d"}, {Name: "e", Value: "f"}}, + }, } { require.Equal(t, tc.expected, processExternalLabels(tc.labels, tc.externalLabels)) }