From d29f8ecce262ab9159e2f9f63140bd68262ec941 Mon Sep 17 00:00:00 2001
From: Jake Taylor <jake@elixxir.io>
Date: Fri, 11 Mar 2022 16:43:46 -0600
Subject: [PATCH] fix tests

---
 backup/backup_test.go                | 16 ++++++++++++++--
 network/gateway/hostPool.go          | 27 +++++++++++++--------------
 network/gateway/hostpool_test.go     | 13 ++++++++-----
 network/manager.go                   |  1 +
 network/message/utils_test.go        |  8 ++++----
 network/rounds/remoteFilters_test.go |  4 ++--
 network/rounds/utils_test.go         |  8 ++++----
 7 files changed, 46 insertions(+), 31 deletions(-)

diff --git a/backup/backup_test.go b/backup/backup_test.go
index 4b6d964b1..1dc785aa3 100644
--- a/backup/backup_test.go
+++ b/backup/backup_test.go
@@ -24,7 +24,7 @@ import (
 // Tests that Backup.initializeBackup returns a new Backup with a copy of the
 // key and the callback.
 func Test_initializeBackup(t *testing.T) {
-	cbChan := make(chan []byte)
+	cbChan := make(chan []byte, 2)
 	cb := func(encryptedBackup []byte) { cbChan <- encryptedBackup }
 	expectedPassword := "MySuperSecurePassword"
 	b, err := initializeBackup(expectedPassword, cb, nil,
@@ -34,6 +34,12 @@ func Test_initializeBackup(t *testing.T) {
 		t.Errorf("initializeBackup returned an error: %+v", err)
 	}
 
+	select {
+	case <-cbChan:
+	case <-time.After(10 * time.Millisecond):
+		t.Error("Timed out waiting for callback.")
+	}
+
 	// Check that the correct password is in storage
 	loadedPassword, err := loadPassword(b.store.GetKV())
 	if err != nil {
@@ -89,6 +95,12 @@ func Test_resumeBackup(t *testing.T) {
 		t.Errorf("Failed to initialize new Backup: %+v", err)
 	}
 
+	select {
+	case <-cbChan1:
+	case <-time.After(10 * time.Millisecond):
+		t.Error("Timed out waiting for callback.")
+	}
+
 	// Get key and salt to compare to later
 	key1, salt1, _, err := loadBackup(b.store.GetKV())
 	if err != nil {
@@ -135,7 +147,7 @@ func Test_resumeBackup(t *testing.T) {
 
 	select {
 	case r := <-cbChan1:
-		t.Errorf("Callback of first Backup called: %q", r) // TODO: i think there is a race condition here
+		t.Errorf("Callback of first Backup called: %q", r)
 	case r := <-cbChan2:
 		if !bytes.Equal(encryptedBackup, r) {
 			t.Errorf("Callback has unexepected data."+
diff --git a/network/gateway/hostPool.go b/network/gateway/hostPool.go
index 4b0210fcf..dd4616cc4 100644
--- a/network/gateway/hostPool.go
+++ b/network/gateway/hostPool.go
@@ -74,23 +74,23 @@ type HostPool struct {
 
 // PoolParams Allows configuration of HostPool parameters
 type PoolParams struct {
-	MaxPoolSize    uint32             // Maximum number of Hosts in the HostPool
-	PoolSize       uint32             // Allows override of HostPool size. Set to zero for dynamic size calculation
-	ProxyAttempts  uint32             // How many proxies will be used in event of send failure
-	MaxPings       uint32             // How many gateways to concurrently test when initializing HostPool. Disabled if zero.
-	LazyConnection bool               // Flag determining whether Host connections are initialized when added to HostPool
-	HostParams     connect.HostParams // Parameters for the creation of new Host objects
+	MaxPoolSize     uint32             // Maximum number of Hosts in the HostPool
+	PoolSize        uint32             // Allows override of HostPool size. Set to zero for dynamic size calculation
+	ProxyAttempts   uint32             // How many proxies will be used in event of send failure
+	MaxPings        uint32             // How many gateways to concurrently test when initializing HostPool. Disabled if zero.
+	ForceConnection bool               // Flag determining whether Host connections are initialized when added to HostPool
+	HostParams      connect.HostParams // Parameters for the creation of new Host objects
 }
 
 // DefaultPoolParams Returns a default set of PoolParams
 func DefaultPoolParams() PoolParams {
 	p := PoolParams{
-		MaxPoolSize:    30,
-		ProxyAttempts:  5,
-		PoolSize:       0,
-		MaxPings:       0,
-		LazyConnection: true,
-		HostParams:     connect.GetDefaultHostParams(),
+		MaxPoolSize:     30,
+		ProxyAttempts:   5,
+		PoolSize:        0,
+		MaxPings:        0,
+		ForceConnection: false,
+		HostParams:      connect.GetDefaultHostParams(),
 	}
 	p.HostParams.MaxRetries = 1
 	p.HostParams.MaxSendRetries = 1
@@ -550,7 +550,7 @@ func (h *HostPool) replaceHostNoStore(newId *id.ID, oldPoolIndex uint32) error {
 	}
 
 	// Manually connect the new Host
-	if !h.poolParams.LazyConnection {
+	if h.poolParams.ForceConnection {
 		go func() {
 			err := newHost.Connect()
 			if err != nil {
@@ -664,7 +664,6 @@ func (h *HostPool) addGateway(gwId *id.ID, ndfIndex int) {
 	// Check if the host exists
 	host, ok := h.manager.GetHost(gwId)
 	if !ok {
-
 		// Check if gateway ID collides with an existing hard coded ID
 		if id.CollidesWithHardCodedID(gwId) {
 			jww.ERROR.Printf("Gateway ID invalid, collides with a "+
diff --git a/network/gateway/hostpool_test.go b/network/gateway/hostpool_test.go
index 5e93ba8df..30f3b6a61 100644
--- a/network/gateway/hostpool_test.go
+++ b/network/gateway/hostpool_test.go
@@ -485,11 +485,12 @@ func TestHostPool_UpdateNdf(t *testing.T) {
 
 	// Construct a manager (bypass business logic in constructor)
 	hostPool := &HostPool{
-		manager:  manager,
-		hostList: make([]*connect.Host, newIndex+1),
-		hostMap:  make(map[id.ID]uint32),
-		ndf:      testNdf,
-		storage:  storage.InitTestingSession(t),
+		manager:    manager,
+		hostList:   make([]*connect.Host, newIndex+1),
+		hostMap:    make(map[id.ID]uint32),
+		ndf:        testNdf,
+		storage:    storage.InitTestingSession(t),
+		poolParams: DefaultPoolParams(),
 		filter: func(m map[id.ID]int, _ *ndf.NetworkDefinition) map[id.ID]int {
 			return m
 		},
@@ -855,6 +856,7 @@ func TestHostPool_AddGateway(t *testing.T) {
 		hostList:       make([]*connect.Host, newIndex+1),
 		hostMap:        make(map[id.ID]uint32),
 		ndf:            testNdf,
+		poolParams:     params,
 		addGatewayChan: make(chan network.NodeGateway),
 		storage:        storage.InitTestingSession(t),
 	}
@@ -888,6 +890,7 @@ func TestHostPool_RemoveGateway(t *testing.T) {
 		hostList:       make([]*connect.Host, newIndex+1),
 		hostMap:        make(map[id.ID]uint32),
 		ndf:            testNdf,
+		poolParams:     params,
 		addGatewayChan: make(chan network.NodeGateway),
 		storage:        storage.InitTestingSession(t),
 		rng:            fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG),
diff --git a/network/manager.go b/network/manager.go
index bba264c6b..1cec83797 100644
--- a/network/manager.go
+++ b/network/manager.go
@@ -133,6 +133,7 @@ func NewManager(session *storage.Session, switchboard *switchboard.Switchboard,
 	poolParams.HostParams.KaClientOpts.Time = time.Duration(math.MaxInt64)
 	// Enable optimized HostPool initialization
 	poolParams.MaxPings = 50
+	poolParams.ForceConnection = true
 	m.sender, err = gateway.NewSender(poolParams, rng,
 		ndf, comms, session, m.NodeRegistration)
 	if err != nil {
diff --git a/network/message/utils_test.go b/network/message/utils_test.go
index 80b31c9a9..e0bf2222d 100644
--- a/network/message/utils_test.go
+++ b/network/message/utils_test.go
@@ -17,10 +17,10 @@ func (mc *MockSendCMIXComms) GetHost(*id.ID) (*connect.Host, bool) {
 	nid1 := id.NewIdFromString("zezima", id.Node, mc.t)
 	gwID := nid1.DeepCopy()
 	gwID.SetType(id.Gateway)
-	h, _ := connect.NewHost(gwID, "0.0.0.0", []byte(""), connect.HostParams{
-		MaxRetries:  0,
-		AuthEnabled: false,
-	})
+	p := connect.GetDefaultHostParams()
+	p.MaxRetries = 0
+	p.AuthEnabled = false
+	h, _ := connect.NewHost(gwID, "0.0.0.0", []byte(""), p)
 	return h, true
 }
 
diff --git a/network/rounds/remoteFilters_test.go b/network/rounds/remoteFilters_test.go
index 51d26973d..e49043285 100644
--- a/network/rounds/remoteFilters_test.go
+++ b/network/rounds/remoteFilters_test.go
@@ -20,7 +20,7 @@ import (
 )
 
 func TestMain(m *testing.M) {
-	jww.SetStdoutThreshold(jww.LevelTrace)
+	jww.SetStdoutThreshold(jww.LevelDebug)
 	connect.TestingOnlyDisableTLS = true
 	os.Exit(m.Run())
 }
@@ -98,4 +98,4 @@ func TestRemoteFilter_FirstLastRound(t *testing.T) {
 			"\n\tExpected: %v\n\tReceived: %v", receivedLastRound, firstRound+uint64(roundRange))
 	}
 
-}
\ No newline at end of file
+}
diff --git a/network/rounds/utils_test.go b/network/rounds/utils_test.go
index 8779a68ac..ea2453493 100644
--- a/network/rounds/utils_test.go
+++ b/network/rounds/utils_test.go
@@ -62,10 +62,10 @@ func (mmrc *mockMessageRetrievalComms) RemoveHost(hid *id.ID) {
 }
 
 func (mmrc *mockMessageRetrievalComms) GetHost(hostId *id.ID) (*connect.Host, bool) {
-	h, _ := connect.NewHost(hostId, "0.0.0.0", []byte(""), connect.HostParams{
-		MaxRetries:  0,
-		AuthEnabled: false,
-	})
+	p := connect.GetDefaultHostParams()
+	p.MaxRetries = 0
+	p.AuthEnabled = false
+	h, _ := connect.NewHost(hostId, "0.0.0.0", []byte(""), p)
 	return h, true
 }
 
-- 
GitLab