Refactor processExternalLabels method with slice copy for left labels

Signed-off-by: songjiayang <songjiayang1@gmail.com>
pull/8757/head
songjiayang 4 years ago
parent e313ffa8ab
commit 9a01472780

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

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

Loading…
Cancel
Save