From 23421e190c4cff1986e92d02a7f02bff7c228ad4 Mon Sep 17 00:00:00 2001 From: Daniel Nephin Date: Tue, 9 Mar 2021 14:00:29 -0500 Subject: [PATCH] state: adjust compare for catalog events Document that this comparison should roughly match MatchesKey Only sort by overrideKey or service name, but not both Add namespace to the sort. The client side also builds a map of these based on the namespace/node/service key, so the only order that really matters is the ordering of register/dereigster events. --- agent/consul/state/catalog_events_test.go | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/agent/consul/state/catalog_events_test.go b/agent/consul/state/catalog_events_test.go index d012f81e1c..9aa82062e8 100644 --- a/agent/consul/state/catalog_events_test.go +++ b/agent/consul/state/catalog_events_test.go @@ -1651,14 +1651,26 @@ func assertDeepEqual(t *testing.T, x, y interface{}, opts ...cmp.Option) { } // cmpPartialOrderEvents returns a compare option which sorts events so that -// all events for a particular node/service are grouped together. The sort is -// stable so events with the same node/service retain their relative order. +// all events for a particular topic are grouped together. The sort is +// stable so events with the same key retain their relative order. +// +// This sort should match the logic in EventPayloadCheckServiceNode.MatchesKey +// to avoid masking bugs. var cmpPartialOrderEvents = cmp.Options{ cmpopts.SortSlices(func(i, j stream.Event) bool { key := func(e stream.Event) string { - csn := getPayloadCheckServiceNode(e.Payload) - // TODO: double check this sort key is correct. - return fmt.Sprintf("%s/%s/%s/%s", e.Topic, csn.Node.Node, csn.Service.Service, e.Payload.(EventPayloadCheckServiceNode).overrideKey) + payload := e.Payload.(EventPayloadCheckServiceNode) + csn := payload.Value + + name := csn.Service.Service + if payload.overrideKey != "" { + name = payload.overrideKey + } + ns := csn.Service.EnterpriseMeta.GetNamespace() + if payload.overrideNamespace != "" { + ns = payload.overrideNamespace + } + return fmt.Sprintf("%s/%s/%s/%s", e.Topic, csn.Node.Node, ns, name) } return key(i) < key(j) }),