From 3cb9a96cdfd38250c2c5b08b32e6d472fd9f897b Mon Sep 17 00:00:00 2001 From: Devon Hudson Date: Sat, 28 Jan 2023 16:14:27 -0700 Subject: [PATCH] Make monolith tests reliable with random keys --- build/gobind-pinecone/monolith.go | 27 +++++++++++++------------- build/gobind-pinecone/monolith_test.go | 24 ++++++++++++++++++++++- 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/build/gobind-pinecone/monolith.go b/build/gobind-pinecone/monolith.go index 3860f824e..5e8e5875c 100644 --- a/build/gobind-pinecone/monolith.go +++ b/build/gobind-pinecone/monolith.go @@ -600,20 +600,21 @@ func (m *DendriteMonolith) Start() { } }() - go func(ch <-chan pineconeEvents.Event) { - eLog := logrus.WithField("pinecone", "events") - stopRelayServerSync := make(chan bool) + stopRelayServerSync := make(chan bool) - m.relayRetriever = RelayServerRetriever{ - Context: context.Background(), - ServerName: gomatrixserverlib.ServerName(m.PineconeRouter.PublicKey().String()), - FederationAPI: m.federationAPI, - relayServersQueried: make(map[gomatrixserverlib.ServerName]bool), - RelayAPI: monolith.RelayAPI, - running: *atomic.NewBool(false), - quit: stopRelayServerSync, - } - m.relayRetriever.InitializeRelayServers(eLog) + eLog := logrus.WithField("pinecone", "events") + m.relayRetriever = RelayServerRetriever{ + Context: context.Background(), + ServerName: gomatrixserverlib.ServerName(m.PineconeRouter.PublicKey().String()), + FederationAPI: m.federationAPI, + relayServersQueried: make(map[gomatrixserverlib.ServerName]bool), + RelayAPI: monolith.RelayAPI, + running: *atomic.NewBool(false), + quit: stopRelayServerSync, + } + m.relayRetriever.InitializeRelayServers(eLog) + + go func(ch <-chan pineconeEvents.Event) { for event := range ch { switch e := event.(type) { diff --git a/build/gobind-pinecone/monolith_test.go b/build/gobind-pinecone/monolith_test.go index c8435158b..3c8873e09 100644 --- a/build/gobind-pinecone/monolith_test.go +++ b/build/gobind-pinecone/monolith_test.go @@ -18,6 +18,7 @@ import ( "context" "fmt" "net" + "strings" "testing" "time" @@ -247,12 +248,33 @@ func TestMonolithSetRelayServers(t *testing.T) { relays := monolith.GetRelayServers(nodeID) monolith.Stop() - if relays != expectedRelays { + if !containSameKeys(strings.Split(relays, ","), strings.Split(expectedRelays, ",")) { t.Fatalf("%s: expected %s got %s", tc.name, expectedRelays, relays) } } } +func containSameKeys(expected []string, actual []string) bool { + if len(expected) != len(actual) { + return false + } + + for _, expectedKey := range expected { + hasMatch := false + for _, actualKey := range actual { + if actualKey == expectedKey { + hasMatch = true + } + } + + if !hasMatch { + return false + } + } + + return true +} + func TestParseServerKey(t *testing.T) { testCases := []struct { name string