Refactor processExternalLabels method with slice copy for left labels

Signed-off-by: songjiayang <songjiayang1@gmail.com>
This commit is contained in:
songjiayang 2021-05-01 00:37:07 +08:00
parent e313ffa8ab
commit 9a01472780
2 changed files with 30 additions and 8 deletions

View File

@ -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() {

View File

@ -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))
}