diff --git a/network/address/addressSpace.go b/network/address/addressSpace.go index 065bcd7b8f23e06a04239d2d69bf853067d55c14..c596893aafc3d7d38a862637cbd512d02ee4e670 100644 --- a/network/address/addressSpace.go +++ b/network/address/addressSpace.go @@ -4,7 +4,6 @@ import ( "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "sync" - "testing" ) const ( @@ -121,25 +120,3 @@ func (as *space) UnregisterAddressSpaceNotification(tag string) { delete(as.notifyMap, tag) } - -// NewTestAddressSpace initialises a new AddressSpace for testing with the given -// size. -func NewTestAddressSpace(newSize uint8, x interface{}) *space { - switch x.(type) { - case *testing.T, *testing.M, *testing.B, *testing.PB: - break - default: - jww.FATAL.Panicf( - "NewTestAddressSpace is restricted to testing only. Got %T", x) - } - - as := &space{ - size: initSize, - notifyMap: make(map[string]chan uint8), - cond: sync.NewCond(&sync.Mutex{}), - } - - as.UpdateAddressSpace(newSize) - - return as -} diff --git a/network/address/addressSpace_test.go b/network/address/addressSpace_test.go index d2e57f474f9c4692eef163b5b01f87f7963f3349..a4622524077737286099d407aa6a4a5e74cfbd5b 100644 --- a/network/address/addressSpace_test.go +++ b/network/address/addressSpace_test.go @@ -9,7 +9,7 @@ import ( ) // Unit test of NewAddressSpace. -func Test_newAddressSpace(t *testing.T) { +func TestNewAddressSpace(t *testing.T) { expected := &space{ size: initSize, notifyMap: make(map[string]chan uint8), @@ -19,14 +19,14 @@ func Test_newAddressSpace(t *testing.T) { as := NewAddressSpace() if !reflect.DeepEqual(expected, as) { - t.Errorf("NewAddressSpace failed to return the expected AddressSpace."+ + t.Errorf("NewAddressSpace failed to return the expected Space."+ "\nexpected: %+v\nreceived: %+v", expected, as) } } -// Test that AddressSpace.Get blocks when the address space size has not been -// set and that it does not block when it has been set. -func Test_addressSpace_Get(t *testing.T) { +// Test that Space.GetAddressSpace blocks when the address space size has not +// been set and that it does not block when it has been set. +func TestSpace_GetAddressSpace(t *testing.T) { as := NewAddressSpace() expectedSize := uint8(42) @@ -58,8 +58,8 @@ func Test_addressSpace_Get(t *testing.T) { } } -// Test that AddressSpace.Get blocks until the condition broadcasts. -func Test_addressSpace_Get_WaitBroadcast(t *testing.T) { +// Test that Space.GetAddressSpace blocks until the condition broadcasts. +func TestSpace_GetAddressSpace_WaitBroadcast(t *testing.T) { as := NewAddressSpace() wait := make(chan uint8) @@ -82,8 +82,8 @@ func Test_addressSpace_Get_WaitBroadcast(t *testing.T) { as.(*space).cond.Broadcast() } -// Unit test of AddressSpace.GetAddressSpaceWithoutWait. -func Test_addressSpace_GetWithoutWait(t *testing.T) { +// Unit test of Space.GetAddressSpaceWithoutWait. +func TestSpace_GetAddressSpaceWithoutWait(t *testing.T) { as := NewAddressSpace() size := as.GetAddressSpaceWithoutWait() @@ -93,8 +93,8 @@ func Test_addressSpace_GetWithoutWait(t *testing.T) { } } -// Tests that AddressSpace.Update only updates the size when it is larger. -func Test_addressSpace_update(t *testing.T) { +// Tests that Space.UpdateAddressSpace only updates the size when it is larger. +func TestSpace_UpdateAddressSpace(t *testing.T) { as := NewAddressSpace() expectedSize := uint8(42) @@ -113,8 +113,9 @@ func Test_addressSpace_update(t *testing.T) { } } -// Tests that AddressSpace.Update sends the new size to all registered channels. -func Test_addressSpace_update_GetAndChannels(t *testing.T) { +// Tests that Space.UpdateAddressSpace sends the new size to all registered +// channels. +func TestSpace_UpdateAddressSpace_GetAndChannels(t *testing.T) { as := NewAddressSpace() var wg sync.WaitGroup expectedSize := uint8(42) @@ -122,13 +123,15 @@ func Test_addressSpace_update_GetAndChannels(t *testing.T) { // Start threads that are waiting for an Update wait := []chan uint8{make(chan uint8), make(chan uint8), make(chan uint8)} for _, waitChan := range wait { - go func(waitChan chan uint8) { waitChan <- as.GetAddressSpace() }(waitChan) + go func(waitChan chan uint8) { + waitChan <- as.GetAddressSpace() + }(waitChan) } // Wait on threads for i, waitChan := range wait { + wg.Add(1) go func(i int, waitChan chan uint8) { - wg.Add(1) defer wg.Done() select { @@ -137,7 +140,7 @@ func Test_addressSpace_update_GetAndChannels(t *testing.T) { t.Errorf("Thread %d received unexpected size."+ "\nexpected: %d\nreceived: %d", i, expectedSize, size) } - case <-time.NewTimer(20 * time.Millisecond).C: + case <-time.After(25 * time.Millisecond): t.Errorf("Timed out waiting for get to return on thread %d.", i) } }(i, waitChan) @@ -152,21 +155,21 @@ func Test_addressSpace_update_GetAndChannels(t *testing.T) { chanID = strconv.Itoa(i) notifyChannels[chanID], err = as.RegisterAddressSpaceNotification(chanID) if err != nil { - t.Errorf("Failed to regisdter channel: %+v", err) + t.Errorf("Failed to reigster channel: %+v", err) } } // Wait for new size on channels for chanID, notifyChan := range notifyChannels { + wg.Add(1) go func(chanID string, notifyChan chan uint8) { - wg.Add(1) defer wg.Done() select { case size := <-notifyChan: - t.Errorf("Received size %d on channel %s when it should not have.", - size, chanID) - case <-time.NewTimer(20 * time.Millisecond).C: + t.Errorf("Received size %d on channel %s when it should not "+ + "have.", size, chanID) + case <-time.After(20 * time.Millisecond): } }(chanID, notifyChan) } @@ -186,25 +189,26 @@ func Test_addressSpace_update_GetAndChannels(t *testing.T) { // Wait for new size on channels for chanID, notifyChan := range notifyChannels { + wg.Add(1) go func(chanID string, notifyChan chan uint8) { - wg.Add(1) defer wg.Done() select { case size := <-notifyChan: if size != expectedSize { t.Errorf("Failed to receive expected size on channel %s."+ - "\nexpected: %d\nreceived: %d", chanID, expectedSize, size) + "\nexpected: %d\nreceived: %d", + chanID, expectedSize, size) } - case <-time.NewTimer(20 * time.Millisecond).C: + case <-time.After(20 * time.Millisecond): t.Errorf("Timed out waiting on channel %s", chanID) } }(chanID, notifyChan) } // Wait for timeout on unregistered channel + wg.Add(1) go func() { - wg.Add(1) defer wg.Done() select { @@ -223,9 +227,9 @@ func Test_addressSpace_update_GetAndChannels(t *testing.T) { wg.Wait() } -// Tests that a channel created by AddressSpace.RegisterNotification receives -// the expected size when triggered. -func Test_addressSpace_RegisterNotification(t *testing.T) { +// Tests that a channel created by Space.RegisterAddressSpaceNotification +// receives the expected size when triggered. +func TestSpace_RegisterAddressSpaceNotification(t *testing.T) { as := NewAddressSpace() expectedSize := uint8(42) @@ -244,7 +248,7 @@ func Test_addressSpace_RegisterNotification(t *testing.T) { t.Errorf("received wrong size on channel."+ "\nexpected: %d\nreceived: %d", expectedSize, size) } - case <-time.NewTimer(10 * time.Millisecond).C: + case <-time.After(10 * time.Millisecond): t.Error("Timed out waiting on channel.") } }() @@ -257,9 +261,9 @@ func Test_addressSpace_RegisterNotification(t *testing.T) { } } -// Tests that when AddressSpace.UnregisterAddressSpaceNotification unregisters a channel, -// it no longer can be triggered from the map. -func Test_addressSpace_UnregisterNotification(t *testing.T) { +// Tests that when Space.UnregisterAddressSpaceNotification unregisters a +// channel and that it no longer can be triggered from the map. +func TestSpace_UnregisterAddressSpaceNotification(t *testing.T) { as := NewAddressSpace() expectedSize := uint8(42) diff --git a/network/cmixMessageBuffer.go b/network/cmixMessageBuffer.go index f7b36ca132c28abf6fd93905b789f6187abdcdcc..0a916515bbe966992e90d43b3c7be233a095b999 100644 --- a/network/cmixMessageBuffer.go +++ b/network/cmixMessageBuffer.go @@ -125,7 +125,8 @@ func LoadCmixMessageBuffer(kv *versioned.KV, key string) (*CmixMessageBuffer, er return &CmixMessageBuffer{mb: mb}, nil } -func (cmb *CmixMessageBuffer) Add(msg format.Message, recipient *id.ID, params CMIXParams) { +func (cmb *CmixMessageBuffer) Add(msg format.Message, recipient *id.ID, + params CMIXParams) { paramBytes, err := json.Marshal(params) if err != nil { jww.FATAL.Panicf("Failed to JSON marshal CMIXParams: %+v", err) diff --git a/network/cmixMessageBuffer_test.go b/network/cmixMessageBuffer_test.go index 43c97639b4c183d4caa00744ee7d95442a1dbdd7..b098518d258389609b281d49ca24cafc6c6a8660 100644 --- a/network/cmixMessageBuffer_test.go +++ b/network/cmixMessageBuffer_test.go @@ -94,7 +94,8 @@ func Test_cmixMessageBuffer_Smoke(t *testing.T) { testMsgs, ids, _ := makeTestCmixMessages(2) // Create new buffer - cmb, err := NewOrLoadCmixMessageBuffer(versioned.NewKV(make(ekv.Memstore)), "testKey") + kv := versioned.NewKV(make(ekv.Memstore)) + cmb, err := NewOrLoadCmixMessageBuffer(kv, "testKey") if err != nil { t.Errorf("Failed to make new cmixMessageHandler: %+v", err) } diff --git a/network/critical.go b/network/critical.go index dde56c99dcc19f105d4db68045aea25252c924da..b077c9d545f8e65c8109335912c60f2247352d51 100644 --- a/network/critical.go +++ b/network/critical.go @@ -72,7 +72,8 @@ func (c *critical) runCriticalMessages(stop *stoppable.Single) { } } -func (c *critical) handle(msg format.Message, recipient *id.ID, rid id.Round, rtnErr error) { +func (c *critical) handle( + msg format.Message, recipient *id.ID, rid id.Round, rtnErr error) { if rtnErr != nil { c.Failed(msg, recipient) } else { diff --git a/network/follow.go b/network/follow.go index 958f0fa11b38209c22848f7c75748fc4247a4c6e..4353d9c5374d9e437ad0ebbd399cd6565a1165ff 100644 --- a/network/follow.go +++ b/network/follow.go @@ -54,7 +54,8 @@ const ( // followNetworkComms is a comms interface to make testing easier. type followNetworkComms interface { GetHost(hostId *id.ID) (*connect.Host, bool) - SendPoll(host *connect.Host, message *pb.GatewayPoll) (*pb.GatewayPollResponse, error) + SendPoll(host *connect.Host, message *pb.GatewayPoll) ( + *pb.GatewayPollResponse, error) } // followNetwork polls the network to get updated on the state of nodes, the @@ -107,10 +108,12 @@ func (m *manager) followNetwork(report ClientErrorReport, // follow executes each iteration of the follower. func (m *manager) follow(report ClientErrorReport, rng csprng.Source, - comms followNetworkComms, stop *stoppable.Single, abandon func(round id.Round)) { + comms followNetworkComms, stop *stoppable.Single, + abandon func(round id.Round)) { // Get the identity we will poll for - identity, err := m.GetEphemeralIdentity(rng, m.Space.GetAddressSpaceWithoutWait()) + identity, err := m.GetEphemeralIdentity( + rng, m.Space.GetAddressSpaceWithoutWait()) if err != nil { jww.FATAL.Panicf( "Failed to get an identity, this should be impossible: %+v", err) @@ -212,7 +215,8 @@ func (m *manager) follow(report ClientErrorReport, rng csprng.Source, for _, update := range pollResp.Updates { // Ignore irrelevant updates - if update.State != uint32(states.COMPLETED) && update.State != uint32(states.FAILED) { + if update.State != uint32(states.COMPLETED) && + update.State != uint32(states.FAILED) { continue } @@ -229,9 +233,9 @@ func (m *manager) follow(report ClientErrorReport, rng csprng.Source, } // Mutate the update to indicate failure due to a ClientError - // FIXME: Should be able to trigger proper type of round event - // FIXME: without mutating the RoundInfo. Signature also needs verified - // FIXME: before keys are deleted + // FIXME: Should be able to trigger proper type of round + // event without mutating the RoundInfo. Signature also + // needs verified before keys are deleted. update.State = uint32(states.FAILED) // trigger a reregistration with the node @@ -308,7 +312,9 @@ func (m *manager) follow(report ClientErrorReport, rng csprng.Source, // Approximate the earliest possible round that messages could be // received on this ID by using an estimate of how many rounds the // network runs per second - roundsDelta := uint(time.Now().Sub(identity.StartValid) / time.Second * estimatedRoundsPerSecond) + timeSinceStartValid := netTime.Now().Sub(identity.StartValid) + roundsDelta := + uint(timeSinceStartValid / time.Second * estimatedRoundsPerSecond) if roundsDelta < m.param.KnownRoundsThreshold { roundsDelta = m.param.KnownRoundsThreshold } @@ -341,12 +347,12 @@ func (m *manager) follow(report ClientErrorReport, rng csprng.Source, gwRoundsState.RangeUnchecked( updatedEarliestRound, m.param.KnownRoundsThreshold, roundChecker) - jww.DEBUG.Printf("Processed RangeUnchecked, Oldest: %d, firstUnchecked: %d, "+ - "last Checked: %d, threshold: %d, NewEarliestRemaining: %d, NumWithMessages: %d, "+ - "NumUnknown: %d", updatedEarliestRound, - gwRoundsState.GetFirstUnchecked(), gwRoundsState.GetLastChecked(), - m.param.KnownRoundsThreshold, earliestRemaining, - len(roundsWithMessages), len(roundsUnknown)) + jww.DEBUG.Printf("Processed RangeUnchecked, Oldest: %d, "+ + "firstUnchecked: %d, last Checked: %d, threshold: %d, "+ + "NewEarliestRemaining: %d, NumWithMessages: %d, NumUnknown: %d", + updatedEarliestRound, gwRoundsState.GetFirstUnchecked(), + gwRoundsState.GetLastChecked(), m.param.KnownRoundsThreshold, + earliestRemaining, len(roundsWithMessages), len(roundsUnknown)) _, _, changed := identity.ER.Set(earliestRemaining) if changed { diff --git a/network/gateway/hostPool.go b/network/gateway/hostPool.go index d6e56a778f43dc9316b4ef871f1441c3cd00d207..d605c7190fec5111fb1e7b47f8e5c9eea3ddb793 100644 --- a/network/gateway/hostPool.go +++ b/network/gateway/hostPool.go @@ -671,7 +671,7 @@ func (h *HostPool) updateConns() error { return nil } -// convertNdfToMap takes ndf.Gateways and puts their IDs into a map object. +// convertNdfToMap takes NDF.Gateways and puts their IDs into a map object. func convertNdfToMap(ndf *ndf.NetworkDefinition) (map[id.ID]int, error) { result := make(map[id.ID]int) if ndf == nil { diff --git a/network/gateway/hostpool_test.go b/network/gateway/hostpool_test.go index d1c1f8db04a7af910bda0aea40dce982808e1568..9c60be66153f6d19cc7fffc8e57fb8d032e460d5 100644 --- a/network/gateway/hostpool_test.go +++ b/network/gateway/hostpool_test.go @@ -8,7 +8,7 @@ package gateway import ( - "fmt" + "github.com/pkg/errors" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/storage" "gitlab.com/elixxir/comms/network" @@ -29,7 +29,7 @@ func TestMain(m *testing.M) { } // Unit test -func TestNewHostPool(t *testing.T) { +func Test_newHostPool(t *testing.T) { manager := newMockManager() rng := fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) testNdf := getTestNdf(t) @@ -38,18 +38,17 @@ func TestNewHostPool(t *testing.T) { params := DefaultPoolParams() params.MaxPoolSize = uint32(len(testNdf.Gateways)) - // Pull all gateways from ndf into host manager + // Pull all gateways from NDF into host manager for _, gw := range testNdf.Gateways { gwId, err := id.Unmarshal(gw.ID) if err != nil { - t.Errorf("Failed to unmarshal ID in mock ndf: %v", err) + t.Errorf("Failed to unmarshal ID in mock NDF: %+v", err) } // Add mock gateway to manager _, err = manager.AddHost(gwId, "", nil, connect.GetDefaultHostParams()) if err != nil { - t.Errorf("Could not add mock host to manager: %v", err) - t.FailNow() + t.Fatalf("Could not add mock host to manager: %+v", err) } } @@ -63,7 +62,7 @@ func TestNewHostPool(t *testing.T) { } // Tests that the hosts are loaded from storage, if they exist. -func TestNewHostPool_HostListStore(t *testing.T) { +func Test_newHostPool_HostListStore(t *testing.T) { manager := newMockManager() rng := fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) testNdf := getTestNdf(t) @@ -105,7 +104,7 @@ func TestNewHostPool_HostListStore(t *testing.T) { } } -// Unit test +// Unit test. func TestHostPool_ManageHostPool(t *testing.T) { manager := newMockManager() rng := fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) @@ -117,30 +116,30 @@ func TestHostPool_ManageHostPool(t *testing.T) { params := DefaultPoolParams() params.MaxPoolSize = uint32(len(testNdf.Gateways)) - // Pull all gateways from ndf into host manager + // Pull all gateways from NDF into host manager for _, gw := range testNdf.Gateways { gwId, err := id.Unmarshal(gw.ID) if err != nil { - t.Errorf("Failed to unmarshal ID in mock ndf: %v", err) + t.Errorf("Failed to unmarshal ID in mock NDF: %+v", err) } // Add mock gateway to manager - _, err = manager.AddHost(gwId, gw.Address, nil, connect.GetDefaultHostParams()) + _, err = manager.AddHost( + gwId, gw.Address, nil, connect.GetDefaultHostParams()) if err != nil { - t.Errorf("Could not add mock host to manager: %v", err) - t.FailNow() + t.Fatalf("Could not add mock host to manager: %+v", err) } } // Call the constructor - testPool, err := newHostPool(params, rng, testNdf, manager, - testStorage, addGwChan) + testPool, err := newHostPool( + params, rng, testNdf, manager, testStorage, addGwChan) if err != nil { - t.Fatalf("Failed to create mock host pool: %v", err) + t.Fatalf("Failed to create mock host pool: %+v", err) } - // Construct a list of new gateways/nodes to add to ndf + // Construct a list of new gateways/nodes to add to the NDF newGatewayLen := len(testNdf.Gateways) newGateways := make([]ndf.Gateway, newGatewayLen) newNodes := make([]ndf.Node, newGatewayLen) @@ -148,15 +147,15 @@ func TestHostPool_ManageHostPool(t *testing.T) { // Construct gateways gwId := id.NewIdFromUInt(uint64(100+i), id.Gateway, t) newGateways[i] = ndf.Gateway{ID: gwId.Bytes()} + // Construct nodes nodeId := gwId.DeepCopy() nodeId.SetType(id.Node) newNodes[i] = ndf.Node{ID: nodeId.Bytes(), Status: ndf.Active} - } + // Update the NDF, removing some gateways at a cutoff newNdf := getTestNdf(t) - // Update the ndf, removing some gateways at a cutoff newNdf.Gateways = newGateways newNdf.Nodes = newNodes @@ -174,8 +173,8 @@ func TestHostPool_ManageHostPool(t *testing.T) { } } -// Full happy path test -func TestHostPool_ReplaceHost(t *testing.T) { +// Full happy path test. +func TestHostPool_replaceHost(t *testing.T) { manager := newMockManager() testNdf := getTestNdf(t) newIndex := uint32(20) @@ -193,10 +192,10 @@ func TestHostPool_ReplaceHost(t *testing.T) { /* "Replace" a host with no entry */ - // Pull a gateway ID from the ndf + // Pull a gateway ID from the NDF gwIdOne, err := id.Unmarshal(testNdf.Gateways[0].ID) if err != nil { - t.Errorf("Failed to unmarshal ID in mock ndf: %v", err) + t.Errorf("Failed to unmarshal ID in mock NDF: %+v", err) } // Add mock gateway to manager @@ -231,10 +230,10 @@ func TestHostPool_ReplaceHost(t *testing.T) { /* Replace the initial host with a new host */ - // Pull a different gateway ID from the ndf + // Pull a different gateway ID from the NDF gwIdTwo, err := id.Unmarshal(testNdf.Gateways[1].ID) if err != nil { - t.Errorf("Failed to unmarshal ID in mock ndf: %v", err) + t.Errorf("Failed to unmarshal ID in mock NDF: %+v", err) } // Add second mock gateway to manager @@ -295,7 +294,7 @@ func TestHostPool_ReplaceHost(t *testing.T) { } // Error path, could not get host -func TestHostPool_ReplaceHost_Error(t *testing.T) { +func TestHostPool_replaceHost_Error(t *testing.T) { manager := newMockManager() // Construct a manager (bypass business logic in constructor) @@ -338,18 +337,18 @@ func TestHostPool_ForceReplace(t *testing.T) { testNdf.Gateways = append(testNdf.Gateways, newGateway) testNdf.Nodes = append(testNdf.Nodes, newNode) - // Pull all gateways from ndf into host manager + // Pull all gateways from NDF into host manager for _, gw := range testNdf.Gateways { gwId, err := id.Unmarshal(gw.ID) if err != nil { - t.Errorf("Failed to unmarshal ID in mock ndf: %v", err) + t.Errorf("Failed to unmarshal ID in mock NDF: %+v", err) } // Add mock gateway to manager - _, err = manager.AddHost(gwId, gw.Address, nil, connect.GetDefaultHostParams()) + _, err = manager.AddHost( + gwId, gw.Address, nil, connect.GetDefaultHostParams()) if err != nil { - t.Errorf("Could not add mock host to manager: %v", err) - t.FailNow() + t.Fatalf("Could not add mock host to manager: %+v", err) } } @@ -366,19 +365,19 @@ func TestHostPool_ForceReplace(t *testing.T) { gw := testNdf.Gateways[i] gwId, err := id.Unmarshal(gw.ID) if err != nil { - t.Fatalf("Failed to unmarshal ID in mock ndf: %v", err) + t.Fatalf("Failed to unmarshal ID in mock NDF: %+v", err) } err = testPool.replaceHost(gwId, i) if err != nil { - t.Fatalf("Failed to replace host in set-up: %v", err) + t.Fatalf("Failed to replace host in set-up: %+v", err) } } oldGatewayIndex := 0 oldHost := testPool.hostList[oldGatewayIndex] - // Force replace the gateway at a given index + // Force the replacement of the gateway at a given index err = testPool.replaceHost(testPool.selectGateway(), uint32(oldGatewayIndex)) if err != nil { t.Errorf("Failed to force replace: %v", err) @@ -396,8 +395,8 @@ func TestHostPool_ForceReplace(t *testing.T) { } -// Unit test -func TestHostPool_CheckReplace(t *testing.T) { +// Unit test. +func TestHostPool_checkReplace(t *testing.T) { manager := newMockManager() rng := fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) testNdf := getTestNdf(t) @@ -408,58 +407,58 @@ func TestHostPool_CheckReplace(t *testing.T) { params := DefaultPoolParams() params.MaxPoolSize = uint32(len(testNdf.Gateways)) - 5 - // Pull all gateways from ndf into host manager + // Pull all gateways from NDF into host manager for _, gw := range testNdf.Gateways { gwId, err := id.Unmarshal(gw.ID) if err != nil { - t.Errorf("Failed to unmarshal ID in mock ndf: %v", err) + t.Errorf("Failed to unmarshal ID in mock NDF: %+v", err) } + // Add mock gateway to manager - _, err = manager.AddHost(gwId, gw.Address, nil, connect.GetDefaultHostParams()) + _, err = manager.AddHost( + gwId, gw.Address, nil, connect.GetDefaultHostParams()) if err != nil { - t.Errorf("Could not add mock host to manager: %v", err) - t.FailNow() + t.Fatalf("Could not add mock host to manager: %+v", err) } - } // Call the constructor - testPool, err := newHostPool(params, rng, testNdf, manager, - testStorage, addGwChan) + testPool, err := newHostPool( + params, rng, testNdf, manager, testStorage, addGwChan) if err != nil { - t.Fatalf("Failed to create mock host pool: %v", err) + t.Fatalf("Failed to create mock host pool: %+v", err) } // Call check replace oldGatewayIndex := 0 oldHost := testPool.hostList[oldGatewayIndex] - expectedError := fmt.Errorf(errorsList[0]) + expectedError := errors.Errorf(errorsList[0]) wasReplaced, err := testPool.checkReplace(oldHost.GetId(), expectedError) if err != nil { - t.Errorf("Failed to check replace: %v", err) + t.Errorf("Failed to check replace: %+v", err) } if !wasReplaced { - t.Errorf("Expected to replace") + t.Error("Expected to replace.") } // Ensure that old gateway has been removed from the map if _, ok := testPool.hostMap[*oldHost.GetId()]; ok { - t.Errorf("Expected old host to be removed from map") + t.Error("Expected old host to be removed from map.") } // Ensure we are disconnected from the old host if isConnected, _ := oldHost.Connected(); isConnected { - t.Errorf("Failed to disconnect from old host %s", oldHost) + t.Errorf("Failed to disconnect from old host %s.", oldHost) } // Check that an error not in the global list results in a no-op goodGatewayIndex := 0 goodGateway := testPool.hostList[goodGatewayIndex] - unexpectedErr := fmt.Errorf("not in global error list") + unexpectedErr := errors.Errorf("not in global error list") wasReplaced, err = testPool.checkReplace(oldHost.GetId(), unexpectedErr) if err != nil { - t.Errorf("Failed to check replace: %v", err) + t.Errorf("Failed to check replace: %+v", err) } if wasReplaced { t.Errorf("Expected not to replace") @@ -477,7 +476,7 @@ func TestHostPool_CheckReplace(t *testing.T) { } -// Unit test +// Unit test. func TestHostPool_UpdateNdf(t *testing.T) { manager := newMockManager() testNdf := getTestNdf(t) @@ -512,14 +511,15 @@ func TestHostPool_UpdateNdf(t *testing.T) { // Update pool with the new Ndf hostPool.UpdateNdf(newNdf) - // Check that the host pool's ndf has been modified properly - if len(newNdf.Nodes) != len(hostPool.ndf.Nodes) || len(newNdf.Gateways) != len(hostPool.ndf.Gateways) { - t.Errorf("Host pool ndf not updated to new ndf.") + // Check that the host pool's NDF has been modified properly + if len(newNdf.Nodes) != len(hostPool.ndf.Nodes) || + len(newNdf.Gateways) != len(hostPool.ndf.Gateways) { + t.Errorf("Host pool NDF not updated to new NDF.") } } // Full test -func TestHostPool_GetPreferred(t *testing.T) { +func TestHostPool_getPreferred(t *testing.T) { manager := newMockManager() rng := fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) testNdf := getTestNdf(t) @@ -528,19 +528,20 @@ func TestHostPool_GetPreferred(t *testing.T) { params := DefaultPoolParams() params.PoolSize = uint32(len(testNdf.Gateways)) - // Pull all gateways from ndf into host manager + // Pull all gateways from NDF into host manager hostMap := make(map[id.ID]bool, 0) targets := make([]*id.ID, 0) for _, gw := range testNdf.Gateways { gwId, err := id.Unmarshal(gw.ID) if err != nil { - t.Fatalf("Failed to unmarshal ID in mock ndf: %v", err) + t.Fatalf("Failed to unmarshal ID in mock NDF: %+v", err) } // Add mock gateway to manager - _, err = manager.AddHost(gwId, gw.Address, nil, connect.GetDefaultHostParams()) + _, err = manager.AddHost( + gwId, gw.Address, nil, connect.GetDefaultHostParams()) if err != nil { - t.Fatalf("Could not add mock host to manager: %v", err) + t.Fatalf("Could not add mock host to manager: %+v", err) } hostMap[*gwId] = true @@ -589,8 +590,8 @@ func TestHostPool_GetPreferred(t *testing.T) { } -// Unit test -func TestHostPool_GetAny(t *testing.T) { +// Unit test. +func TestHostPool_getAny(t *testing.T) { manager := newMockManager() rng := fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) testNdf := getTestNdf(t) @@ -599,17 +600,18 @@ func TestHostPool_GetAny(t *testing.T) { params := DefaultPoolParams() params.MaxPoolSize = uint32(len(testNdf.Gateways)) - // Pull all gateways from ndf into host manager + // Pull all gateways from NDF into host manager for _, gw := range testNdf.Gateways { gwId, err := id.Unmarshal(gw.ID) if err != nil { - t.Fatalf("Failed to unmarshal ID in mock ndf: %v", err) + t.Fatalf("Failed to unmarshal ID in mock NDF: %+v", err) } // Add mock gateway to manager - _, err = manager.AddHost(gwId, gw.Address, nil, connect.GetDefaultHostParams()) + _, err = manager.AddHost( + gwId, gw.Address, nil, connect.GetDefaultHostParams()) if err != nil { - t.Fatalf("Could not add mock host to manager: %v", err) + t.Fatalf("Could not add mock host to manager: %+v", err) } } @@ -646,7 +648,7 @@ func TestHostPool_GetAny(t *testing.T) { } // Unit test -func TestHostPool_ForceAdd(t *testing.T) { +func TestHostPool_forceAdd(t *testing.T) { manager := newMockManager() rng := fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) testNdf := getTestNdf(t) @@ -655,17 +657,18 @@ func TestHostPool_ForceAdd(t *testing.T) { params := DefaultPoolParams() params.PoolSize = uint32(len(testNdf.Gateways)) - // Pull all gateways from ndf into host manager + // Pull all gateways from NDF into host manager for _, gw := range testNdf.Gateways { gwId, err := id.Unmarshal(gw.ID) if err != nil { - t.Fatalf("Failed to unmarshal ID in mock ndf: %v", err) + t.Fatalf("Failed to unmarshal ID in mock NDF: %+v", err) } // Add mock gateway to manager - _, err = manager.AddHost(gwId, gw.Address, nil, connect.GetDefaultHostParams()) + _, err = manager.AddHost( + gwId, gw.Address, nil, connect.GetDefaultHostParams()) if err != nil { - t.Fatalf("Could not add mock host to manager: %v", err) + t.Fatalf("Could not add mock host to manager: %+v", err) } } @@ -697,8 +700,8 @@ func TestHostPool_ForceAdd(t *testing.T) { } } -// Unit test which only adds information to ndf -func TestHostPool_UpdateConns_AddGateways(t *testing.T) { +// Unit test which only adds information to NDF. +func TestHostPool_updateConns_AddGateways(t *testing.T) { manager := newMockManager() rng := fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) testNdf := getTestNdf(t) @@ -707,15 +710,16 @@ func TestHostPool_UpdateConns_AddGateways(t *testing.T) { params := DefaultPoolParams() params.MaxPoolSize = uint32(len(testNdf.Gateways)) - // Pull all gateways from ndf into host manager + // Pull all gateways from NDF into host manager for _, gw := range testNdf.Gateways { gwId, err := id.Unmarshal(gw.ID) if err != nil { - t.Fatalf("Failed to unmarshal ID in mock ndf: %v", err) + t.Fatalf("Failed to unmarshal ID in mock NDF: %+v", err) } // Add mock gateway to manager - _, err = manager.AddHost(gwId, gw.Address, nil, connect.GetDefaultHostParams()) + _, err = manager.AddHost( + gwId, gw.Address, nil, connect.GetDefaultHostParams()) if err != nil { t.Fatalf("Could not add mock host to manager: %v", err) } @@ -729,7 +733,7 @@ func TestHostPool_UpdateConns_AddGateways(t *testing.T) { t.Fatalf("Failed to create mock host pool: %v", err) } - // Construct a list of new gateways/nodes to add to ndf + // Construct a list of new gateways/nodes to add to NDF newGatewayLen := 10 newGateways := make([]ndf.Gateway, newGatewayLen) newNodes := make([]ndf.Node, newGatewayLen) @@ -744,7 +748,7 @@ func TestHostPool_UpdateConns_AddGateways(t *testing.T) { } - // Update the ndf + // Update the NDF newNdf := getTestNdf(t) newNdf.Gateways = append(newNdf.Gateways, newGateways...) newNdf.Nodes = append(newNdf.Nodes, newNodes...) @@ -771,8 +775,8 @@ func TestHostPool_UpdateConns_AddGateways(t *testing.T) { } -// Unit test which only adds information to ndf -func TestHostPool_UpdateConns_RemoveGateways(t *testing.T) { +// Unit test which only adds information to NDF. +func TestHostPool_updateConns_RemoveGateways(t *testing.T) { manager := newMockManager() rng := fastRNG.NewStreamGenerator(1, 1, csprng.NewSystemRNG) testNdf := getTestNdf(t) @@ -781,18 +785,18 @@ func TestHostPool_UpdateConns_RemoveGateways(t *testing.T) { params := DefaultPoolParams() params.MaxPoolSize = uint32(len(testNdf.Gateways)) - // Pull all gateways from ndf into host manager + // Pull all gateways from NDF into host manager for _, gw := range testNdf.Gateways { gwId, err := id.Unmarshal(gw.ID) if err != nil { - t.Errorf("Failed to unmarshal ID in mock ndf: %v", err) + t.Errorf("Failed to unmarshal ID in mock NDF: %+v", err) } // Add mock gateway to manager - _, err = manager.AddHost(gwId, gw.Address, nil, connect.GetDefaultHostParams()) + _, err = manager.AddHost( + gwId, gw.Address, nil, connect.GetDefaultHostParams()) if err != nil { - t.Errorf("Could not add mock host to manager: %v", err) - t.FailNow() + t.Fatalf("Could not add mock host to manager: %+v", err) } } @@ -804,7 +808,7 @@ func TestHostPool_UpdateConns_RemoveGateways(t *testing.T) { t.Fatalf("Failed to create mock host pool: %v", err) } - // Construct a list of new gateways/nodes to add to ndf + // Construct a list of new gateways/nodes to add to NDF newGatewayLen := len(testNdf.Gateways) newGateways := make([]ndf.Gateway, newGatewayLen) newNodes := make([]ndf.Node, newGatewayLen) @@ -819,7 +823,7 @@ func TestHostPool_UpdateConns_RemoveGateways(t *testing.T) { } - // Update the ndf, replacing old data entirely + // Update the NDF, replacing old data entirely newNdf := getTestNdf(t) newNdf.Gateways = newGateways newNdf.Nodes = newNodes @@ -844,8 +848,8 @@ func TestHostPool_UpdateConns_RemoveGateways(t *testing.T) { } } -// Unit test -func TestHostPool_AddGateway(t *testing.T) { +// Unit test. +func TestHostPool_addGateway(t *testing.T) { manager := newMockManager() testNdf := getTestNdf(t) newIndex := uint32(20) @@ -869,7 +873,7 @@ func TestHostPool_AddGateway(t *testing.T) { gwId, err := id.Unmarshal(testNdf.Gateways[ndfIndex].ID) if err != nil { - t.Errorf("Failed to unmarshal ID in mock ndf: %v", err) + t.Errorf("Failed to unmarshal ID in mock NDF: %+v", err) } hostPool.addGateway(gwId, ndfIndex) @@ -881,7 +885,7 @@ func TestHostPool_AddGateway(t *testing.T) { } // Unit test -func TestHostPool_RemoveGateway(t *testing.T) { +func TestHostPool_removeGateway(t *testing.T) { manager := newMockManager() testNdf := getTestNdf(t) newIndex := uint32(20) @@ -903,7 +907,7 @@ func TestHostPool_RemoveGateway(t *testing.T) { gwId, err := id.Unmarshal(testNdf.Gateways[ndfIndex].ID) if err != nil { - t.Errorf("Failed to unmarshal ID in mock ndf: %v", err) + t.Errorf("Failed to unmarshal ID in mock NDF: %+v", err) } // Manually add host information diff --git a/network/gateway/sender.go b/network/gateway/sender.go index 3725aa873f9bca04b5a31de934c8865624dcdf68..1ccff1fb8b0c18550cfad2dd7db48d04aaae89d0 100644 --- a/network/gateway/sender.go +++ b/network/gateway/sender.go @@ -23,7 +23,8 @@ import ( "time" ) -// Sender Object used for sending that wraps the HostPool for providing destinations +// Sender object is used for sending that wraps the HostPool for providing +// destinations. type Sender interface { SendToAny(sendFunc func(host *connect.Host) (interface{}, error), stop *stoppable.Single) (interface{}, error) @@ -55,35 +56,40 @@ func NewSender(poolParams PoolParams, rng *fastRNG.StreamGenerator, // SendToAny call given sendFunc to any Host in the HostPool, attempting with up // to numProxies destinations. -func (s *sender) SendToAny(sendFunc func(host *connect.Host) (interface{}, error), +func (s *sender) SendToAny(sendFunc func(*connect.Host) (interface{}, error), stop *stoppable.Single) (interface{}, error) { proxies := s.getAny(s.poolParams.ProxyAttempts, nil) for proxy := range proxies { result, err := sendFunc(proxies[proxy]) if stop != nil && !stop.IsRunning() { - return nil, errors.Errorf(stoppable.ErrMsg, stop.Name(), "SendToAny") + return nil, + errors.Errorf(stoppable.ErrMsg, stop.Name(), "SendToAny") } else if err == nil { return result, nil } else { // Now we must check whether the Host should be replaced - replaced, checkReplaceErr := s.checkReplace(proxies[proxy].GetId(), err) + replaced, checkReplaceErr := s.checkReplace( + proxies[proxy].GetId(), err) if replaced { - jww.WARN.Printf("Unable to SendToAny, replaced a proxy %s with error %s", - proxies[proxy].GetId().String(), err.Error()) + jww.WARN.Printf("Unable to SendToAny, replaced a proxy %s "+ + "with error %s", proxies[proxy].GetId(), err.Error()) } else { if checkReplaceErr != nil { - jww.WARN.Printf("Unable to SendToAny via %s: %s. Unable to replace host: %+v", - proxies[proxy].GetId().String(), err.Error(), checkReplaceErr) + jww.WARN.Printf("Unable to SendToAny via %s: %s."+ + "Unable to replace host: %+v", + proxies[proxy].GetId(), err.Error(), checkReplaceErr) } else { - jww.WARN.Printf("Unable to SendToAny via %s: %s. Did not replace host.", - proxies[proxy].GetId().String(), err.Error()) + jww.WARN.Printf("Unable to SendToAny via %s: %s."+ + "Did not replace host.", + proxies[proxy].GetId(), err.Error()) } } // End for non-retryable errors if !strings.Contains(err.Error(), RetryableError) { - return nil, errors.WithMessage(err, "Received error with SendToAny") + return nil, + errors.WithMessage(err, "Received error with SendToAny") } } } @@ -117,28 +123,33 @@ func (s *sender) SendToPreferred(targets []*id.ID, sendFunc SendToPreferredFunc, remainingTimeout := timeout - netTime.Since(startTime) result, err := sendFunc(targetHosts[i], targets[i], remainingTimeout) if stop != nil && !stop.IsRunning() { - return nil, errors.Errorf(stoppable.ErrMsg, stop.Name(), "SendToPreferred") + return nil, errors.Errorf( + stoppable.ErrMsg, stop.Name(), "SendToPreferred") } else if err == nil { return result, nil } else { // Now we must check whether the Host should be replaced replaced, checkReplaceErr := s.checkReplace(targetHosts[i].GetId(), err) if replaced { - jww.WARN.Printf("Unable to SendToPreferred first pass via %s, replaced a proxy %s with error %s", + jww.WARN.Printf("Unable to SendToPreferred first pass via %s, "+ + "replaced a proxy %s with error %s", targets[i], targetHosts[i].GetId(), err.Error()) } else { if checkReplaceErr != nil { - jww.WARN.Printf("Unable to SendToPreferred first pass %s via %s: %s. Unable to replace host: %+v", - targets[i], targetHosts[i].GetId(), err.Error(), checkReplaceErr) + jww.WARN.Printf("Unable to SendToPreferred first pass %s "+ + "via %s: %s. Unable to replace host: %+v", targets[i], + targetHosts[i].GetId(), err.Error(), checkReplaceErr) } else { - jww.WARN.Printf("Unable to SendToPreferred first pass %s via %s: %s. Did not replace host.", + jww.WARN.Printf("Unable to SendToPreferred first pass %s "+ + "via %s: %s. Did not replace host.", targets[i], targetHosts[i].GetId(), err.Error()) } } // End for non-retryable errors if !strings.Contains(err.Error(), RetryableError) { - return nil, errors.WithMessage(err, "Received error with SendToPreferred") + return nil, errors.WithMessage( + err, "Received error with SendToPreferred") } } } @@ -152,8 +163,8 @@ func (s *sender) SendToPreferred(targets []*id.ID, sendFunc SendToPreferredFunc, // Build a map of bad proxies badProxies := make(map[string]interface{}) - // Iterate between each target's list of proxies, using the next target for each proxy - + // Iterate between each target's list of proxies, using the next target for + // each proxy for proxyIdx := uint32(0); proxyIdx < s.poolParams.ProxyAttempts; proxyIdx++ { for targetIdx := range proxies { // Return an error if the timeout duration is reached @@ -166,8 +177,8 @@ func (s *sender) SendToPreferred(targets []*id.ID, sendFunc SendToPreferredFunc, targetProxies := proxies[targetIdx] if !(int(proxyIdx) < len(targetProxies)) { jww.WARN.Printf("Failed to send to proxy %d on target %d (%s) "+ - "due to not enough proxies (only %d), skipping attempt", proxyIdx, - targetIdx, target, len(targetProxies)) + "due to not enough proxies (only %d), skipping attempt", + proxyIdx, targetIdx, target, len(targetProxies)) continue } proxy := targetProxies[proxyIdx] @@ -180,36 +191,40 @@ func (s *sender) SendToPreferred(targets []*id.ID, sendFunc SendToPreferredFunc, remainingTimeout := timeout - netTime.Since(startTime) result, err := sendFunc(proxy, target, remainingTimeout) if stop != nil && !stop.IsRunning() { - return nil, errors.Errorf(stoppable.ErrMsg, stop.Name(), "SendToPreferred") + return nil, errors.Errorf( + stoppable.ErrMsg, stop.Name(), "SendToPreferred") } else if err == nil { return result, nil } else if strings.Contains(err.Error(), RetryableError) { // Retry of the proxy could not communicate - jww.INFO.Printf("Unable to SendToPreferred second pass %s via %s: non-fatal error received, retrying: %s", + jww.INFO.Printf("Unable to SendToPreferred second pass %s "+ + "via %s: non-fatal error received, retrying: %s", target, proxy, err) continue - } else if err == nil { - return result, nil } else { - // Now we must check whether the Host should be replaced + // Check whether the Host should be replaced replaced, checkReplaceErr := s.checkReplace(proxy.GetId(), err) badProxies[proxy.String()] = nil if replaced { - jww.WARN.Printf("Unable to SendToPreferred second pass via %s, replaced a proxy %s with error %s", + jww.WARN.Printf("Unable to SendToPreferred second pass "+ + "via %s, replaced a proxy %s with error %s", target, proxy.GetId(), err.Error()) } else { if checkReplaceErr != nil { - jww.WARN.Printf("Unable to SendToPreferred second pass %s via %s: %s. Unable to replace host: %+v", + jww.WARN.Printf("Unable to SendToPreferred second "+ + "pass %s via %s: %s. Unable to replace host: %+v", target, proxy.GetId(), err.Error(), checkReplaceErr) } else { - jww.WARN.Printf("Unable to SendToPreferred second pass %s via %s: %s. Did not replace host.", + jww.WARN.Printf("Unable to SendToPreferred second "+ + "pass %s via %s: %s. Did not replace host.", target, proxy.GetId(), err.Error()) } } // End for non-retryable errors if !strings.Contains(err.Error(), RetryableError) { - return nil, errors.WithMessage(err, "Received error with SendToPreferred") + return nil, errors.WithMessage( + err, "Received error with SendToPreferred") } } } diff --git a/network/gateway/sender_test.go b/network/gateway/sender_test.go index 5b0a96d641f3e5f44a7c2d14085c36d79add4fb0..b6de48005f188dadc6e1f4053d80f84c53af291d 100644 --- a/network/gateway/sender_test.go +++ b/network/gateway/sender_test.go @@ -45,22 +45,24 @@ func TestSender_SendToAny(t *testing.T) { params := DefaultPoolParams() params.PoolSize = uint32(len(testNdf.Gateways)) - // Pull all gateways from ndf into host manager + // Pull all gateways from NDF into host manager for _, gw := range testNdf.Gateways { gwId, err := id.Unmarshal(gw.ID) if err != nil { - t.Fatalf("Failed to unmarshal ID in mock ndf: %v", err) + t.Fatalf("Failed to unmarshal ID in mock NDF: %+v", err) } // Add mock gateway to manager - _, err = manager.AddHost(gwId, gw.Address, nil, connect.GetDefaultHostParams()) + _, err = manager.AddHost( + gwId, gw.Address, nil, connect.GetDefaultHostParams()) if err != nil { - t.Fatalf("Could not add mock host to manager: %v", err) + t.Fatalf("Could not add mock host to manager: %+v", err) } } - senderFace, err := NewSender(params, rng, testNdf, manager, testStorage, addGwChan) + senderFace, err := NewSender( + params, rng, testNdf, manager, testStorage, addGwChan) s := senderFace.(*sender) if err != nil { t.Fatalf("Failed to create mock sender: %v", err) @@ -70,12 +72,12 @@ func TestSender_SendToAny(t *testing.T) { for index, gw := range testNdf.Gateways { gwId, err := id.Unmarshal(gw.ID) if err != nil { - t.Fatalf("Failed to unmarshal ID in mock ndf: %v", err) + t.Fatalf("Failed to unmarshal ID in mock NDF: %+v", err) } err = s.replaceHost(gwId, uint32(index)) if err != nil { - t.Fatalf("Failed to replace host in set-up: %v", err) + t.Fatalf("Failed to replace host in set-up: %+v", err) } } @@ -117,17 +119,18 @@ func TestSender_SendToPreferred(t *testing.T) { // (self contain to code specific in sendPreferred) params.ProxyAttempts = 0 - // Pull all gateways from ndf into host manager + // Pull all gateways from NDF into host manager for _, gw := range testNdf.Gateways { gwId, err := id.Unmarshal(gw.ID) if err != nil { - t.Fatalf("Failed to unmarshal ID in mock ndf: %v", err) + t.Fatalf("Failed to unmarshal ID in mock NDF: %+v", err) } // Add mock gateway to manager - _, err = manager.AddHost(gwId, gw.Address, nil, connect.GetDefaultHostParams()) + _, err = manager.AddHost( + gwId, gw.Address, nil, connect.GetDefaultHostParams()) if err != nil { - t.Fatalf("Could not add mock host to manager: %v", err) + t.Fatalf("Could not add mock host to manager: %+v", err) } } @@ -168,7 +171,8 @@ func TestSender_SendToPreferred(t *testing.T) { // Ensure we are disconnected from the old host if isConnected, _ := preferredHost.Connected(); isConnected { - t.Errorf("ForceReplace error: Failed to disconnect from old host %s", preferredHost) + t.Errorf("ForceReplace error: Failed to disconnect from old host %s", + preferredHost) } // get a new host to test on @@ -184,12 +188,12 @@ func TestSender_SendToPreferred(t *testing.T) { // Check the host has not been replaced if _, ok := s.hostMap[*preferredHost.GetId()]; !ok { - t.Errorf("Host %s should not have been removed due on an unknown error", preferredHost) + t.Errorf("Host %s should not have been removed due on an unknown error", + preferredHost) } // Ensure we are disconnected from the old host if isConnected, _ := preferredHost.Connected(); isConnected { t.Errorf("Should not disconnect from %s", preferredHost) } - } diff --git a/network/gateway/storeHostList_test.go b/network/gateway/storeHostList_test.go index 3e0a905225af9c8265cd491910ec45fc68bd6480..a0fe45c34acfa906d0f3c53b73bdbab4e40022fb 100644 --- a/network/gateway/storeHostList_test.go +++ b/network/gateway/storeHostList_test.go @@ -25,7 +25,7 @@ import ( // Tests that a host list saved by Store.Store matches the host list returned // by Store.Get. -func TestStore_Store_Get(t *testing.T) { +func Test_saveHostList_getHostList(t *testing.T) { // Init list to store list := []*id.ID{ id.NewIdFromString("histID_1", id.Node, t), @@ -60,7 +60,7 @@ func TestStore_Store_Get(t *testing.T) { // Error path: tests that Store.Get returns an error if no host list is // saved in storage. -func TestStore_Get_StorageError(t *testing.T) { +func Test_getHostList_StorageError(t *testing.T) { // Init storage testStorage := storage.InitTestingSession(t) diff --git a/network/gateway/utils_test.go b/network/gateway/utils_test.go index f1b47786aa75bc8195a07a7694769a7994bc2f17..d9dc77cf5674127e3e30a3ecd0896dcdba16816f 100644 --- a/network/gateway/utils_test.go +++ b/network/gateway/utils_test.go @@ -8,7 +8,6 @@ package gateway import ( - "fmt" "github.com/pkg/errors" "gitlab.com/xx_network/comms/connect" "gitlab.com/xx_network/primitives/id" @@ -144,15 +143,18 @@ func getTestNdf(face interface{}) *ndf.NetworkDefinition { const happyPathReturn = "happyPathReturn" -func SendToPreferredHappyPath(*connect.Host, *id.ID, time.Duration) (interface{}, error) { +func SendToPreferredHappyPath(*connect.Host, *id.ID, time.Duration) ( + interface{}, error) { return happyPathReturn, nil } -func SendToPreferredKnownError(*connect.Host, *id.ID, time.Duration) (interface{}, error) { +func SendToPreferredKnownError(*connect.Host, *id.ID, time.Duration) ( + interface{}, error) { return nil, errors.Errorf(errorsList[0]) } -func SendToPreferredUnknownError(*connect.Host, *id.ID, time.Duration) (interface{}, error) { +func SendToPreferredUnknownError(*connect.Host, *id.ID, time.Duration) ( + interface{}, error) { return nil, errors.Errorf("Unexpected error: Oopsie") } @@ -161,17 +163,19 @@ func SendToAnyHappyPath(*connect.Host) (interface{}, error) { } func SendToAnyKnownError(*connect.Host) (interface{}, error) { - return nil, fmt.Errorf(errorsList[0]) + return nil, errors.Errorf(errorsList[0]) } func SendToAnyUnknownError(*connect.Host) (interface{}, error) { return nil, errors.Errorf("Unexpected error: Oopsie") } -func SendToSpecificHappyPath(_ *connect.Host, _ *id.ID) (interface{}, bool, error) { +func SendToSpecificHappyPath(_ *connect.Host, _ *id.ID) ( + interface{}, bool, error) { return happyPathReturn, false, nil } -func SendToSpecificAbort(_ *connect.Host, _ *id.ID) (interface{}, bool, error) { - return nil, true, fmt.Errorf(errorsList[0]) +func SendToSpecificAbort(_ *connect.Host, _ *id.ID) ( + interface{}, bool, error) { + return nil, true, errors.Errorf(errorsList[0]) } diff --git a/network/health/tracker_test.go b/network/health/tracker_test.go index 7d3684b9aa7af384e7bf62c8e6a7549785ee8bd2..1c2a44aa71d4cc6a4ae30d15575c83113601edcf 100644 --- a/network/health/tracker_test.go +++ b/network/health/tracker_test.go @@ -9,16 +9,17 @@ package health import ( "gitlab.com/elixxir/comms/network" + "sync/atomic" "testing" "time" ) // Happy path smoke test. -func TestNewTracker(t *testing.T) { +func Test_newTracker(t *testing.T) { // Initialize required variables timeout := 250 * time.Millisecond trkr := newTracker(timeout) - counter := 2 // First signal is "false/unhealthy" + counter := int64(2) // First signal is "false/unhealthy" positiveHb := network.Heartbeat{ HasWaitingRound: true, IsRoundComplete: true, @@ -28,9 +29,9 @@ func TestNewTracker(t *testing.T) { listenChan := make(chan bool, 10) listenFunc := func(isHealthy bool) { if isHealthy { - counter++ + atomic.AddInt64(&counter, 1) } else { - counter-- + atomic.AddInt64(&counter, -1) } } trkr.AddHealthCallback(listenFunc) @@ -38,9 +39,9 @@ func TestNewTracker(t *testing.T) { go func() { for isHealthy := range listenChan { if isHealthy { - counter++ + atomic.AddInt64(&counter, 1) } else { - counter-- + atomic.AddInt64(&counter, -1) } } }() @@ -52,12 +53,12 @@ func TestNewTracker(t *testing.T) { } // Send a positive health heartbeat - expectedCount := 2 + expectedCount := int64(2) trkr.heartbeat <- positiveHb // Wait for the heartbeat to register for i := 0; i < 4; i++ { - if trkr.IsHealthy() && counter == expectedCount { + if trkr.IsHealthy() && atomic.LoadInt64(&counter) == expectedCount { break } else { time.Sleep(50 * time.Millisecond) @@ -75,8 +76,9 @@ func TestNewTracker(t *testing.T) { } // Verify the heartbeat triggered the listening chan/func - if counter != expectedCount { - t.Errorf("Expected counter to be %d, got %d", expectedCount, counter) + if atomic.LoadInt64(&counter) != expectedCount { + t.Errorf("Expected counter to be %d, got %d", + expectedCount, atomic.LoadInt64(&counter)) } // Wait out the timeout @@ -94,7 +96,8 @@ func TestNewTracker(t *testing.T) { } // Verify the timeout triggered the listening chan/func - if counter != expectedCount { - t.Errorf("Expected counter to be %d, got %d", expectedCount, counter) + if atomic.LoadInt64(&counter) != expectedCount { + t.Errorf("Expected counter to be %d, got %d", + expectedCount, atomic.LoadInt64(&counter)) } } diff --git a/network/historical/historical_test.go b/network/historical/historical_test.go index 676fb1c75ef0b47ff980325ddf01c411509c8ec7..7157b26e342ec3eff03ab9610493bdb33f365bbb 100644 --- a/network/historical/historical_test.go +++ b/network/historical/historical_test.go @@ -8,6 +8,7 @@ package historical import ( + "sync" "testing" "time" @@ -38,10 +39,12 @@ func TestHistoricalRounds(t *testing.T) { if err != nil { t.Errorf("Failed to look up historical round: %+v", err) } - time.Sleep(501 * time.Millisecond) + time.Sleep(750 * time.Millisecond) - if sender.sendCnt != 1 { - t.Errorf("Did not send as expected") + sendCnt := sender.getSendCnt() + if sendCnt != 1 { + t.Errorf("Did not send as expected.\nexpected: %d\nreceived: %d", + 1, sendCnt) } // Case 2: make round requests up to m.params.MaxHistoricalRounds @@ -56,9 +59,9 @@ func TestHistoricalRounds(t *testing.T) { time.Sleep(10 * time.Millisecond) - if sender.sendCnt != 2 { + if sender.getSendCnt() != 2 { t.Errorf("Unexpected send count.\nexpected: %d\nreceived: %d", - 2, sender.sendCnt) + 2, sender.getSendCnt()) } err = stopper.Close() @@ -70,7 +73,7 @@ func TestHistoricalRounds(t *testing.T) { } } -func TestProcessHistoricalRoundsResponse(t *testing.T) { +func Test_processHistoricalRoundsResponse(t *testing.T) { params := GetDefaultParams() badRR := roundRequest{ rid: id.Round(41), @@ -91,9 +94,12 @@ func TestProcessHistoricalRoundsResponse(t *testing.T) { } x := false callbackCalled := &x + var callbackCalledMux sync.Mutex goodRR := roundRequest{ rid: id.Round(43), RoundResultCallback: func(info *pb.RoundInfo, success bool) { + callbackCalledMux.Lock() + defer callbackCalledMux.Unlock() *callbackCalled = true }, numAttempts: 0, @@ -120,9 +126,11 @@ func TestProcessHistoricalRoundsResponse(t *testing.T) { time.Sleep(5 * time.Millisecond) + callbackCalledMux.Lock() if !*callbackCalled { t.Errorf("expected callback to be called") } + callbackCalledMux.Unlock() } // Test structure implementations. @@ -138,6 +146,13 @@ func (t *testRoundsComms) RequestHistoricalRounds(*connect.Host, type testGWSender struct { sendCnt int + sync.RWMutex +} + +func (t *testGWSender) getSendCnt() int { + t.RLock() + defer t.RUnlock() + return t.sendCnt } func (t *testGWSender) SendToAny(func(host *connect.Host) (interface{}, error), @@ -146,7 +161,9 @@ func (t *testGWSender) SendToAny(func(host *connect.Host) (interface{}, error), infos := make([]*pb.RoundInfo, 1) infos[0] = nil m := &pb.HistoricalRoundsResponse{Rounds: infos} + t.Lock() t.sendCnt += 1 + t.Unlock() return m, nil } diff --git a/network/identity/receptionID/IdentityUse.go b/network/identity/receptionID/IdentityUse.go index be8040ad91d461925cf23de9efbeb6600a31757f..e97a2553866ad70f2eb5b84999fa617ee92df4a6 100644 --- a/network/identity/receptionID/IdentityUse.go +++ b/network/identity/receptionID/IdentityUse.go @@ -18,16 +18,18 @@ type IdentityUse struct { CR *store.CheckedRounds } +// GoString returns a string representations of all the values in the +// IdentityUse. This function adheres to the fmt.GoStringer interface. func (iu IdentityUse) GoString() string { - str := make([]string, 0, 7) - - str = append(str, "Identity:"+iu.Identity.GoString()) - str = append(str, "StartValid:"+iu.StartValid.String()) - str = append(str, "EndValid:"+iu.EndValid.String()) - str = append(str, "Fake:"+strconv.FormatBool(iu.Fake)) - str = append(str, "UR:"+fmt.Sprintf("%+v", iu.UR)) - str = append(str, "ER:"+fmt.Sprintf("%+v", iu.ER)) - str = append(str, "CR:"+fmt.Sprintf("%+v", iu.CR)) + str := []string{ + "Identity:" + iu.Identity.GoString(), + "StartValid:" + iu.StartValid.String(), + "EndValid:" + iu.EndValid.String(), + "Fake:" + strconv.FormatBool(iu.Fake), + "UR:" + fmt.Sprintf("%+v", iu.UR), + "ER:" + fmt.Sprintf("%+v", iu.ER), + "CR:" + fmt.Sprintf("%+v", iu.CR), + } return "{" + strings.Join(str, ", ") + "}" } diff --git a/network/identity/receptionID/identity.go b/network/identity/receptionID/identity.go index 7d3e46ffe601a299e911caaac949ee82d813d9f1..ae421452a0b1a2386487de635c6e49b9b0490945 100644 --- a/network/identity/receptionID/identity.go +++ b/network/identity/receptionID/identity.go @@ -81,10 +81,14 @@ func (i Identity) delete(kv *versioned.KV) error { return kv.Delete(identityStorageKey, identityStorageVersion) } +// String returns a string representations of the ephemeral ID and source ID of +// the Identity. This function adheres to the fmt.Stringer interface. func (i Identity) String() string { return strconv.FormatInt(i.EphId.Int64(), 16) + " " + i.Source.String() } +// GoString returns a string representations of all the values in the Identity. +// This function adheres to the fmt.GoStringer interface. func (i Identity) GoString() string { str := []string{ "EphId:" + strconv.FormatInt(i.EphId.Int64(), 16), diff --git a/network/identity/receptionID/identity_test.go b/network/identity/receptionID/identity_test.go index 9a75e9b22f74d42cec859f12c60ac08d547cc2f8..61c038def3eb2e432ffa9bab4a7534f6d20b4c04 100644 --- a/network/identity/receptionID/identity_test.go +++ b/network/identity/receptionID/identity_test.go @@ -11,7 +11,7 @@ import ( "time" ) -func TestIdentity_EncodeDecode(t *testing.T) { +func TestIdentity_store_loadIdentity(t *testing.T) { kv := versioned.NewKV(make(ekv.Memstore)) r := Identity{ EphemeralIdentity: EphemeralIdentity{ @@ -42,7 +42,7 @@ func TestIdentity_EncodeDecode(t *testing.T) { } } -func TestIdentity_Delete(t *testing.T) { +func TestIdentity_delete(t *testing.T) { kv := versioned.NewKV(make(ekv.Memstore)) r := Identity{ EphemeralIdentity: EphemeralIdentity{ diff --git a/network/identity/receptionID/registration_test.go b/network/identity/receptionID/registration_test.go index 3da5dc71fb84befcd1ca42ff9c9a55f2256b64b8..b93665dc29d7265c36a2f95391c6500826ca095b 100644 --- a/network/identity/receptionID/registration_test.go +++ b/network/identity/receptionID/registration_test.go @@ -17,17 +17,21 @@ import ( // idu, _ := generateFakeIdentity(rng, 15, timestamp) // id := idu.Identity // kv := versioned.NewKV(make(ekv.Memstore)) - +// // id.End = time.Time{} // id.ExtraChecks = 0 - +// +// expectedErr := "Cannot create a registration for an identity which has " + +// "expired" +// // _, err := newRegistration(id, kv) -// if err == nil || !strings.Contains(err.Error(), "Cannot create a registration for an identity which has expired") { -// t.Error("Registration creation succeeded with expired identity.") +// if err == nil || !strings.Contains(err.Error(), expectedErr) { +// t.Errorf("Registration creation succeeded with expired identity." + +// "\nexpected: %s\nreceived: %+v", expectedErr, err) // } // } -func TestNewRegistration_Ephemeral(t *testing.T) { +func Test_newRegistration_Ephemeral(t *testing.T) { // Generate an identity for use rng := rand.New(rand.NewSource(42)) timestamp := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) @@ -46,11 +50,12 @@ func TestNewRegistration_Ephemeral(t *testing.T) { } if _, err = reg.kv.Get(identityStorageKey, 0); err == nil { - t.Error("Ephemeral identity stored the identity when it should not have.") + t.Error( + "Ephemeral identity stored the identity when it should not have.") } } -func TestNewRegistration_Persistent(t *testing.T) { +func Test_newRegistration_Persistent(t *testing.T) { // Generate an identity for use rng := rand.New(rand.NewSource(42)) timestamp := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) @@ -74,7 +79,7 @@ func TestNewRegistration_Persistent(t *testing.T) { } } -func TestLoadRegistration(t *testing.T) { +func Test_loadRegistration(t *testing.T) { // Generate an identity for use rng := rand.New(rand.NewSource(42)) timestamp := time.Date(2009, 11, 17, 20, 34, 58, 651387237, time.UTC) diff --git a/network/identity/receptionID/store/checkedRounds_test.go b/network/identity/receptionID/store/checkedRounds_test.go index 803b824de8a0662830b375cf609b4f96c0cd15a4..f3423efaa8868639df807efadd430ed3c8464530 100644 --- a/network/identity/receptionID/store/checkedRounds_test.go +++ b/network/identity/receptionID/store/checkedRounds_test.go @@ -11,13 +11,14 @@ import ( "testing" ) -// Happy path. -func Test_newCheckedRounds(t *testing.T) { +// Happy path of NewCheckedRounds. +func TestNewCheckedRounds(t *testing.T) { maxRounds := 230 kv := versioned.NewKV(make(ekv.Memstore)) // Create a new BlockStore for storing the round IDs to storage - store, err := utility.NewBlockStore(itemsPerBlock, maxRounds/itemsPerBlock+1, kv) + store, err := utility.NewBlockStore( + itemsPerBlock, maxRounds/itemsPerBlock+1, kv) if err != nil { t.Errorf("Failed to create new BlockStore: %+v", err) } @@ -32,11 +33,11 @@ func Test_newCheckedRounds(t *testing.T) { received, err := NewCheckedRounds(maxRounds, kv) if err != nil { - t.Errorf("NewCheckedRounds() returned an error: %+v", err) + t.Errorf("NewCheckedRounds returned an error: %+v", err) } if !reflect.DeepEqual(expected, received) { - t.Errorf("NewCheckedRounds() did not return the exepcted CheckedRounds."+ + t.Errorf("NewCheckedRounds did not return the exepcted CheckedRounds."+ "\nexpected: %+v\nreceived: %+v", expected, received) } } @@ -48,7 +49,7 @@ func TestCheckedRounds_SaveCheckedRounds_TestLoadCheckedRounds(t *testing.T) { kv := versioned.NewKV(make(ekv.Memstore)) cr, err := NewCheckedRounds(50, kv) if err != nil { - t.Errorf("failed to make new CheckedRounds: %+v", err) + t.Errorf("Failed to make new CheckedRounds: %+v", err) } for i := id.Round(0); i < 100; i++ { cr.Check(i) @@ -56,14 +57,14 @@ func TestCheckedRounds_SaveCheckedRounds_TestLoadCheckedRounds(t *testing.T) { err = cr.SaveCheckedRounds() if err != nil { - t.Errorf("SaveCheckedRounds() returned an error: %+v", err) + t.Errorf("SaveCheckedRounds returned an error: %+v", err) } cr.Prune() newCR, err := LoadCheckedRounds(50, kv) if err != nil { - t.Errorf("LoadCheckedRounds() returned an error: %+v", err) + t.Errorf("LoadCheckedRounds returned an error: %+v", err) } if !reflect.DeepEqual(cr, newCR) { @@ -83,39 +84,40 @@ func TestCheckedRounds_Next(t *testing.T) { for i := id.Round(0); i < 10; i++ { round, exists := cr.Next() if !exists { - t.Error("Next() returned false when there should be more IDs.") + t.Error("Next returned false when there should be more IDs.") } rounds[i] = round } round, exists := cr.Next() if exists { - t.Errorf("Next() returned true when the list should be empty: %d", round) + t.Errorf("Next returned true when the list should be empty: %d", round) } testCR := newCheckedRounds(100, nil) testCR.unmarshal(rounds) if !reflect.DeepEqual(cr, testCR) { - t.Errorf("unmarshal() did not return the expected CheckedRounds."+ + t.Errorf("unmarshal did not return the expected CheckedRounds."+ "\nexpected: %+v\nreceived: %+v", cr, testCR) } } // Happy path. -func Test_checkedRounds_Check(t *testing.T) { +func Test_CheckedRounds_Check(t *testing.T) { cr := newCheckedRounds(100, nil) var expected []id.Round for i := id.Round(1); i < 11; i++ { if i%2 == 0 { if !cr.Check(i) { - t.Errorf("Check() returned false when the round ID should have been added (%d).", i) + t.Errorf("Check returned false when the round ID should have "+ + "been added (%d).", i) } val := cr.l.Back().Value.(id.Round) if val != i { - t.Errorf("Check() did not add the round ID to the back of the list."+ - "\nexpected: %d\nreceived: %d", i, val) + t.Errorf("Check did not add the round ID to the back of "+ + "the list.\nexpected: %d\nreceived: %d", i, val) } expected = append(expected, i) } @@ -130,10 +132,12 @@ func Test_checkedRounds_Check(t *testing.T) { result := cr.Check(i) if i%2 == 0 { if result { - t.Errorf("Check() returned true when the round ID should not have been added (%d).", i) + t.Errorf("Check returned true when the round ID should not "+ + "have been added (%d).", i) } } else if !result { - t.Errorf("Check() returned false when the round ID should have been added (%d).", i) + t.Errorf("Check returned false when the round ID should have "+ + "been added (%d).", i) } else { expected = append(expected, i) } @@ -156,16 +160,16 @@ func TestCheckedRounds_IsChecked(t *testing.T) { for i := id.Round(0); i < 100; i++ { if i%2 == 0 { if !cr.IsChecked(i) { - t.Errorf("IsChecked() falsly reported round ID %d as not checked.", i) + t.Errorf("IsChecked falsly reported round ID %d as not checked.", i) } } else if cr.IsChecked(i) { - t.Errorf("IsChecked() falsly reported round ID %d as checked.", i) + t.Errorf("IsChecked falsly reported round ID %d as checked.", i) } } } // Happy path. -func Test_checkedRounds_Prune(t *testing.T) { +func TestCheckedRounds_Prune(t *testing.T) { cr := newCheckedRounds(5, nil) for i := id.Round(0); i < 10; i++ { cr.Check(i) @@ -174,14 +178,15 @@ func Test_checkedRounds_Prune(t *testing.T) { cr.Prune() if len(cr.m) != 5 || cr.l.Len() != 5 { - t.Errorf("Prune() did not remove the correct number of round IDs."+ + t.Errorf("Prune did not remove the correct number of round IDs."+ "\nexpected: %d\nmap: %d\nlist: %d", 5, len(cr.m), cr.l.Len()) } } -// Happy path: length of the list is not too long and does not need to be pruned. -func Test_checkedRounds_Prune_NoChange(t *testing.T) { +// Happy path: length of the list is not too long and does not need to be +// pruned. +func TestCheckedRounds_Prune_NoChange(t *testing.T) { cr := newCheckedRounds(100, nil) for i := id.Round(0); i < 10; i++ { cr.Check(i) @@ -190,7 +195,7 @@ func Test_checkedRounds_Prune_NoChange(t *testing.T) { cr.Prune() if len(cr.m) != 10 || cr.l.Len() != 10 { - t.Errorf("Prune() did not remove the correct number of round IDs."+ + t.Errorf("Prune did not remove the correct number of round IDs."+ "\nexpected: %d\nmap: %d\nlist: %d", 5, len(cr.m), cr.l.Len()) } @@ -211,7 +216,7 @@ func TestCheckedRounds_unmarshal(t *testing.T) { cr.unmarshal(rounds) if !reflect.DeepEqual(expected, cr) { - t.Errorf("unmarshal() did not return the expected CheckedRounds."+ + t.Errorf("unmarshal did not return the expected CheckedRounds."+ "\nexpected: %+v\nreceived: %+v", expected, cr) } } diff --git a/network/identity/receptionID/store/unknownRounds_test.go b/network/identity/receptionID/store/unknownRounds_test.go index 8d7422239eedbfcf81c24174427a985c00a2cba3..79cb737b7773e7a5fd958cd9fe6a9d377bfe1bab 100644 --- a/network/identity/receptionID/store/unknownRounds_test.go +++ b/network/identity/receptionID/store/unknownRounds_test.go @@ -57,8 +57,8 @@ func TestNewUnknownRounds(t *testing.T) { } -// Full test -func TestUnknownRoundsStore_Iterate(t *testing.T) { +// Full test. +func TestUnknownRounds_Iterate(t *testing.T) { kv := versioned.NewKV(make(ekv.Memstore)) store := NewUnknownRounds(kv, DefaultUnknownRoundsParams()) @@ -127,7 +127,7 @@ func TestUnknownRoundsStore_Iterate(t *testing.T) { } // Unit test -func TestLoadUnknownRoundsStore(t *testing.T) { +func TestLoadUnknownRounds(t *testing.T) { kv := versioned.NewKV(make(ekv.Memstore)) store := NewUnknownRounds(kv, DefaultUnknownRoundsParams()) diff --git a/network/interface.go b/network/interface.go index 3d45710cde447dc85adf39ecbdb798ef8ff8b403..769c52d399fd0da52f9c03c50234f29b7e97d01b 100644 --- a/network/interface.go +++ b/network/interface.go @@ -215,7 +215,8 @@ type Manager interface { externally as well. */ // LookupHistoricalRound looks up the passed historical round on the network. - LookupHistoricalRound(rid id.Round, callback historical.RoundResultCallback) error + LookupHistoricalRound( + rid id.Round, callback historical.RoundResultCallback) error /* === Sender =========================================================== */ /* The sender handles sending comms to the network. It tracks connections to diff --git a/network/manager.go b/network/manager.go index 219619987291df8026b3737be5a61ca35c90e3e9..c245e8d7da17cf26f7eea1cebfd681dbe0f10cfd 100644 --- a/network/manager.go +++ b/network/manager.go @@ -158,8 +158,9 @@ func NewManager(params Params, comms *client.Comms, session storage.Session, m.session.GetReceptionID()) // Set up round handler - m.Pickup = rounds.NewPickup(params.Rounds, m.Handler.GetMessageReceptionChannel(), - m.Sender, m.Retriever, m.rng, m.instance, m.session) + m.Pickup = rounds.NewPickup( + params.Rounds, m.Handler.GetMessageReceptionChannel(), m.Sender, + m.Retriever, m.rng, m.instance, m.session) // Add the identity system m.Tracker = identity.NewOrLoadTracker(m.session, m.Space) @@ -211,7 +212,7 @@ func (m *manager) Follow(report ClientErrorReport) (stoppable.Stoppable, error) // Node Updates multi.Add(m.Registrar.StartProcesses(m.param.ParallelNodeRegistrations)) // Adding/MixCypher - // TODO-node remover + // TODO: node remover // Start the Network tracker followNetworkStopper := stoppable.NewSingle("FollowNetwork") diff --git a/network/message/fingerprints_test.go b/network/message/fingerprints_test.go index 40670107aa30847b9005da3a25817b7f3c0f3a6b..6514d2aece54eaa1f66a89365738cd3baeda4e42 100644 --- a/network/message/fingerprints_test.go +++ b/network/message/fingerprints_test.go @@ -97,7 +97,6 @@ func TestFingerprintsManager_AddFingerprint(t *testing.T) { } func TestFingerprintsManager_DeleteFingerprint(t *testing.T) { - // Construct fingerprint map fpTracker := newFingerprints(&id.ID{}) @@ -154,7 +153,8 @@ func TestFingerprintsManager_DeleteClientFingerprints(t *testing.T) { } } -// todo: consider moving this to a test utils somewhere else.. maybe in the interfaces package? +// TODO: Consider moving this to a test utils somewhere else. Maybe in the +// interfaces package? type MockMsgProcessor struct{} func NewMockMsgProcessor(face interface{}) *MockMsgProcessor { @@ -162,7 +162,8 @@ func NewMockMsgProcessor(face interface{}) *MockMsgProcessor { case *testing.T, *testing.M, *testing.B, *testing.PB: break default: - jww.FATAL.Panicf("NewMockMsgProcessor is restricted to testing only. Got %T", face) + jww.FATAL.Panicf("NewMockMsgProcessor is restricted to testing only. "+ + "Got %T", face) } return &MockMsgProcessor{} @@ -172,7 +173,7 @@ func (mock *MockMsgProcessor) MarkFingerprintUsed(_ format.Fingerprint) { return } -func (mock *MockMsgProcessor) Process(format.Message, receptionID.EphemeralIdentity, - *mixmessages.RoundInfo) { +func (mock *MockMsgProcessor) Process( + format.Message, receptionID.EphemeralIdentity, *mixmessages.RoundInfo) { return } diff --git a/network/message/handler.go b/network/message/handler.go index 687f68544c3413f452e9deda1b77d18515995f72..c8d44bc7405bd9d6ce6e9a37ee6c95f1adf32f66 100644 --- a/network/message/handler.go +++ b/network/message/handler.go @@ -87,7 +87,8 @@ func (p *handler) handleMessage(ecrMsg format.Message, bundle Bundle) bool { } else { // TODO: Delete this else block because it should not be needed. jww.INFO.Printf("checking backup %v", identity.Source) - // //if it does not exist, check against the default fingerprint for the identity + // // If it does not exist, check against the default fingerprint for the + // // identity // forMe = fingerprint2.CheckIdentityFP(ecrMsg.GetSIH(), // ecrMsgContents, preimage.MakeDefault(identity.Source)) } diff --git a/network/message/inProgress.go b/network/message/inProgress.go index dd28e71e9355d82713d08383e25bdddab0a665d7..8c61d390fa674ade341038c8403d7ec532e7c94e 100644 --- a/network/message/inProgress.go +++ b/network/message/inProgress.go @@ -16,10 +16,12 @@ import ( // Messages can arrive in the network out of order. When message handling fails // to decrypt a message, it is added to the garbled message buffer (which is -// stored on disk) and the message decryption is retried here whenever triggered. +// stored on disk) and the message decryption is retried here whenever +// triggered. // This can be triggered through the CheckInProgressMessages on the network -// handler and is used in the /keyExchange package on successful rekey triggering. +// handler and is used in the /keyExchange package on successful rekey +// triggering. // CheckInProgressMessages triggers rechecking all in progress messages if the // queue is not full Exposed on the network handler. diff --git a/network/message/inProgress_test.go b/network/message/inProgress_test.go index 70d710732ba974b49dc60bba7d6bed8c539710d3..f76d9c760094ccb0b80ab394fe59928041a810e4 100644 --- a/network/message/inProgress_test.go +++ b/network/message/inProgress_test.go @@ -21,9 +21,9 @@ func TestMain(m *testing.M) { os.Exit(m.Run()) } -func Test_pickup_CheckInProgressMessages(t *testing.T) { +func TestHandler_CheckInProgressMessages(t *testing.T) { kv := versioned.NewKV(make(ekv.Memstore)) - p := NewHandler(Params{ + h := NewHandler(Params{ MessageReceptionBuffLen: 20, MessageReceptionWorkerPoolSize: 20, MaxChecksInProcessMessage: 20, @@ -34,23 +34,23 @@ func Test_pickup_CheckInProgressMessages(t *testing.T) { cid := id.NewIdFromString("clientID", id.User, t) fp := format.NewFingerprint([]byte("test")) mp := NewMockMsgProcessor(t) - err := p.AddFingerprint(cid, fp, mp) + err := h.AddFingerprint(cid, fp, mp) if err != nil { t.Errorf("Failed to add fingerprint: %+v", err) } - p.inProcess.Add(msg, + h.inProcess.Add(msg, &pb.RoundInfo{ID: 1, Timestamps: []uint64{0, 1, 2, 3}}, receptionID.EphemeralIdentity{Source: cid}) stop := stoppable.NewSingle("stop") - go p.recheckInProgressRunner(stop) + go h.recheckInProgressRunner(stop) - p.CheckInProgressMessages() + h.CheckInProgressMessages() select { case <-time.After(1000 * time.Millisecond): t.Error("Didn't hear anything") - case <-p.messageReception: + case <-h.messageReception: t.Log("Heard something") } diff --git a/network/message/meteredCmixMessageBuffer_test.go b/network/message/meteredCmixMessageBuffer_test.go index 4bf6202f75f6ab46be032f3ef15e2fad36c8f7aa..3260db33d92c431b7d3ecd31830c182fcae2a269 100644 --- a/network/message/meteredCmixMessageBuffer_test.go +++ b/network/message/meteredCmixMessageBuffer_test.go @@ -23,7 +23,7 @@ import ( "time" ) -// Test happy path of meteredCmixMessage.SaveMessage(). +// Test happy path of meteredCmixMessageHandler.SaveMessage. func Test_meteredCmixMessageHandler_SaveMessage(t *testing.T) { // Set up test values mcmh := &meteredCmixMessageHandler{} @@ -60,7 +60,7 @@ func Test_meteredCmixMessageHandler_SaveMessage(t *testing.T) { } } -// Test happy path of meteredCmixMessage.LoadMessage(). +// Test happy path of meteredCmixMessageHandler.LoadMessage. func Test_meteredCmixMessageHandler_LoadMessage(t *testing.T) { // Set up test values mcmh := &meteredCmixMessageHandler{} @@ -85,14 +85,16 @@ func Test_meteredCmixMessageHandler_LoadMessage(t *testing.T) { testMcm := testMsg.(meteredCmixMessage) // Test if message loaded matches expected - if !bytes.Equal(msg.M, testMcm.M) || msg.Count != testMcm.Count || !msg.Timestamp.Equal(testMcm.Timestamp) { + if !bytes.Equal(msg.M, testMcm.M) || msg.Count != testMcm.Count || + !msg.Timestamp.Equal(testMcm.Timestamp) { t.Errorf("LoadMessage() returned an unexpected object (round %d)."+ - "\n\texpected: %+v\n\treceived: %+v", i, msg, testMsg.(meteredCmixMessage)) + "\n\texpected: %+v\n\treceived: %+v", i, msg, + testMsg.(meteredCmixMessage)) } } } -// Test happy path of meteredCmixMessage.DeleteMessage(). +// Test happy path of meteredCmixMessageHandler.DeleteMessage. func Test_meteredCmixMessageHandler_DeleteMessage(t *testing.T) { // Set up test values mcmh := &meteredCmixMessageHandler{} @@ -128,7 +130,8 @@ func Test_meteredCmixMessageHandler_Smoke(t *testing.T) { testMsgs := makeTestFormatMessages(2) // Create new buffer - mcmb, err := NewMeteredCmixMessageBuffer(versioned.NewKV(make(ekv.Memstore)), "testKey") + mcmb, err := NewMeteredCmixMessageBuffer( + versioned.NewKV(make(ekv.Memstore)), "testKey") if err != nil { t.Errorf("NewMeteredCmixMessageBuffer() returned an error."+ "\nexpected: %v\nrecieved: %v", nil, err) @@ -166,9 +169,10 @@ func Test_meteredCmixMessageHandler_Smoke(t *testing.T) { } } -// makeTestMeteredCmixMessage creates a list of messages with random data and the -// expected map after they are added to the buffer. -func makeTestMeteredCmixMessage(n int) ([]meteredCmixMessage, map[utility.MessageHash]struct{}) { +// makeTestMeteredCmixMessage creates a list of messages with random data and +// the expected map after they are added to the buffer. +func makeTestMeteredCmixMessage(n int) ( + []meteredCmixMessage, map[utility.MessageHash]struct{}) { mcmh := &meteredCmixMessageHandler{} prng := rand.New(rand.NewSource(netTime.Now().UnixNano())) mh := map[utility.MessageHash]struct{}{} diff --git a/network/message/services.go b/network/message/services.go index 3a9ca471dbb07fffddca01bd5ed2b86412a6c27c..44825cbb31dd56cbd6db5d4c59bf01757780435b 100644 --- a/network/message/services.go +++ b/network/message/services.go @@ -51,7 +51,7 @@ type service struct { } func NewServices() *ServicesManager { - // todo: implement me + // TODO: implement me return &ServicesManager{ tmap: make(map[id.ID]map[sih.Preimage]service), } @@ -163,7 +163,8 @@ func (sm *ServicesManager) DeleteService(clientID *id.ID, toDelete Service, if services.defaultList != nil && len(services.defaultList) > 1 { for i, p := range services.defaultList { if p == processor { - services.defaultList = append(services.defaultList[:i], services.defaultList[i+1:]...) + services.defaultList = append( + services.defaultList[:i], services.defaultList[i+1:]...) idTmap[toDelete.preimage()] = services return } diff --git a/network/nodes/mixCypher_test.go b/network/nodes/mixCypher_test.go index 14727c13832ce181ddbee9581ea0b7e3b337be6f..941ee5532b07edcb84f89888600322e8822555f5 100644 --- a/network/nodes/mixCypher_test.go +++ b/network/nodes/mixCypher_test.go @@ -20,7 +20,7 @@ import ( ) // Tests that the encrypted payloads and KMACs generated are consistent. -func TestRoundKeys_Encrypt_Consistency(t *testing.T) { +func Test_mixCypher_Encrypt_Consistency(t *testing.T) { const numKeys = 5 expectedPayload := []byte{220, 95, 160, 88, 229, 136, 42, 254, 239, 32, 57, diff --git a/network/nodes/register.go b/network/nodes/register.go index 281e76aa6a9e55a945292aec18eea6fb720bdaf0..e0d38e5801b3860d166a5e6e61f022bd97d81f90 100644 --- a/network/nodes/register.go +++ b/network/nodes/register.go @@ -69,7 +69,8 @@ func registerNodes(r *registrar, s storage.Session, stop *stoppable.Single, // Keep track of how many times this has been attempted numAttempts := uint(1) - if nunAttemptsInterface, hasValue := attempts.LoadOrStore(nidStr, numAttempts); hasValue { + if nunAttemptsInterface, hasValue := attempts.LoadOrStore( + nidStr, numAttempts); hasValue { numAttempts = nunAttemptsInterface.(uint) attempts.Store(nidStr, numAttempts+1) } diff --git a/network/nodes/registrar_test.go b/network/nodes/registrar_test.go index 85449f529c75c76257f46782a825af7840209c13..c0712c5281430bbf0c0daa2baad10be6d9d32f1f 100644 --- a/network/nodes/registrar_test.go +++ b/network/nodes/registrar_test.go @@ -68,7 +68,8 @@ func TestLoadRegistrar_Load(t *testing.T) { testR.add(nodeId, k, expectedValid, expectedKeyId) // Load the store and check its attributes - r, err := LoadRegistrar(testR.session, testR.sender, testR.comms, testR.rng, testR.c) + r, err := LoadRegistrar( + testR.session, testR.sender, testR.comms, testR.rng, testR.c) if err != nil { t.Fatalf("Unable to load store: %+v", err) } @@ -80,15 +81,17 @@ func TestLoadRegistrar_Load(t *testing.T) { keys, _ := r.GetNodeKeys(circuit) if keys.(*mixCypher).keys[0].validUntil != expectedValid { t.Errorf("Unexpected valid until value loaded from store."+ - "\n\tExpected: %v\n\tReceived: %v", expectedValid, keys.(*mixCypher).keys[0].validUntil) + "\nexpected: %v\nreceived: %v", + expectedValid, keys.(*mixCypher).keys[0].validUntil) } if !bytes.Equal(keys.(*mixCypher).keys[0].keyId, expectedKeyId) { t.Errorf("Unexpected keyID value loaded from store."+ - "\n\tExpected: %v\n\tReceived: %v", expectedKeyId, keys.(*mixCypher).keys[0].keyId) + "\nexpected: %v\nreceived: %v", + expectedKeyId, keys.(*mixCypher).keys[0].keyId) } } -func Test_registrar_GetKeys(t *testing.T) { +func Test_registrar_GetNodeKeys(t *testing.T) { r := makeTestRegistrar(t) grp := cyclic.NewGroup(large.NewInt(173), large.NewInt(2)) @@ -114,7 +117,7 @@ func Test_registrar_GetKeys(t *testing.T) { } } -func Test_registrar_GetKeys_Missing(t *testing.T) { +func Test_registrar_GetNodeKeys_Missing(t *testing.T) { r := makeTestRegistrar(t) grp := cyclic.NewGroup(large.NewInt(173), large.NewInt(2)) @@ -145,7 +148,7 @@ func Test_registrar_GetKeys_Missing(t *testing.T) { } } -func Test_registrar_Has(t *testing.T) { +func Test_registrar_HasNode(t *testing.T) { r := makeTestRegistrar(t) grp := cyclic.NewGroup(large.NewInt(173), large.NewInt(2)) diff --git a/network/nodes/storeKey_test.go b/network/nodes/storeKey_test.go deleted file mode 100644 index 8dc3a4444d9937e93587d8fddc673b8eaad0c284..0000000000000000000000000000000000000000 --- a/network/nodes/storeKey_test.go +++ /dev/null @@ -1,8 +0,0 @@ -/////////////////////////////////////////////////////////////////////////////// -// Copyright © 2020 xx network SEZC // -// // -// Use of this source code is governed by a license that can be found in the // -// LICENSE file // -/////////////////////////////////////////////////////////////////////////////// - -package nodes diff --git a/network/nodes/utils_test.go b/network/nodes/utils_test.go index 15e4373ef69a922480fc4c9454f32a1c40d01e0c..f466463570fc6c1533d9465255974f0f1a8828b4 100644 --- a/network/nodes/utils_test.go +++ b/network/nodes/utils_test.go @@ -36,7 +36,8 @@ func makeTestRegistrar(t *testing.T) *registrar { nodeChan := make(chan commNetwork.NodeGateway, InputChanLen) - r, err := LoadRegistrar(session, sender, NewMockClientComms(), rngGen, nodeChan) + r, err := LoadRegistrar( + session, sender, NewMockClientComms(), rngGen, nodeChan) if err != nil { t.Fatalf("Failed to create new registrar: %+v", err) } diff --git a/network/roundTracking.go b/network/roundTracking.go index e1f325de5f542c125fda068715a1af4e83433e1e..2d1324e94df21bdc5bc9ce819df6f79c0ced1fc1 100644 --- a/network/roundTracking.go +++ b/network/roundTracking.go @@ -5,7 +5,7 @@ //////////////////////////////////////////////////////////////////////////////// // This is an in-memory track of rounds that have been processed in this run of -// the xxdk. It only is enabled when loglevel is debug or higher. It will +// the xxDK. It only is enabled when loglevel is debug or higher. It will // accumulate all rounds and then dump on exit. Is only enabled when run through // the command line interface unless enabled explicitly in code. diff --git a/network/rounds/get.go b/network/rounds/get.go index 7fd430d19a0053f5b3d3fd838b5d7b5fa7bc797c..3f7e1efda0e4ac28bfe8f554a5b4751236bddae1 100644 --- a/network/rounds/get.go +++ b/network/rounds/get.go @@ -14,7 +14,8 @@ import ( "gitlab.com/xx_network/primitives/id" ) -func (m *manager) GetMessagesFromRound(roundID id.Round, identity receptionID.EphemeralIdentity) { +func (m *manager) GetMessagesFromRound( + roundID id.Round, identity receptionID.EphemeralIdentity) { // Get the round from the in-RAM store ri, err := m.instance.GetRound(roundID) @@ -42,8 +43,9 @@ func (m *manager) GetMessagesFromRound(roundID id.Round, identity receptionID.Ep err = m.historical.LookupHistoricalRound( roundID, func(info *pb.RoundInfo, success bool) { if !success { - // TODO: implement me + // TODO: Implement me } + // If found, send to Message Retrieval Workers m.lookupRoundMessages <- roundLookup{ RoundInfo: info, diff --git a/network/rounds/retrieve.go b/network/rounds/retrieve.go index 5891480cc5a1b84e8351c6a461417f63205244b3..0852384f739720db50cb9f91d99fa49d6ee16929 100644 --- a/network/rounds/retrieve.go +++ b/network/rounds/retrieve.go @@ -20,6 +20,7 @@ import ( "gitlab.com/elixxir/primitives/format" "gitlab.com/xx_network/comms/connect" "gitlab.com/xx_network/primitives/id" + "gitlab.com/xx_network/primitives/netTime" "time" ) @@ -161,9 +162,9 @@ func (m *manager) processMessageRetrieval(comms MessageRetrievalComms, // getMessagesFromGateway attempts to get messages from their assigned gateway // host in the round specified. If successful func (m *manager) getMessagesFromGateway(roundID id.Round, - identity receptionID.EphemeralIdentity, comms MessageRetrievalComms, gwIds []*id.ID, - stop *stoppable.Single) (message.Bundle, error) { - start := time.Now() + identity receptionID.EphemeralIdentity, comms MessageRetrievalComms, + gwIds []*id.ID, stop *stoppable.Single) (message.Bundle, error) { + start := netTime.Now() // Send to the gateways using backup proxies result, err := m.sender.SendToPreferred(gwIds, func(host *connect.Host, target *id.ID, _ time.Duration) (interface{}, error) { @@ -225,7 +226,7 @@ func (m *manager) getMessagesFromGateway(roundID id.Round, jww.INFO.Printf("Received %d messages in Round %d for %d (%s) in %s", len(msgs), roundID, identity.EphId.Int64(), identity.Source, - time.Now().Sub(start)) + netTime.Now().Sub(start)) // Build the bundle of messages to send to the message processor bundle := message.Bundle{ diff --git a/network/rounds/retrieve_test.go b/network/rounds/retrieve_test.go index 1d5a5225e88587103e03d1d2b4138bab13009b80..d45efa64ac721479bc4afe5c6ee74c831f06a2c1 100644 --- a/network/rounds/retrieve_test.go +++ b/network/rounds/retrieve_test.go @@ -24,8 +24,8 @@ import ( "time" ) -// Happy path -func TestManager_ProcessMessageRetrieval(t *testing.T) { +// Happy path. +func Test_manager_processMessageRetrieval(t *testing.T) { // General initializations connect.TestingOnlyDisableTLS = true testManager := newManager(t) @@ -87,23 +87,22 @@ func TestManager_ProcessMessageRetrieval(t *testing.T) { }() var testBundle message.Bundle - go func() { - // Receive the bundle over the channel - time.Sleep(1 * time.Second) - testBundle = <-messageBundleChan - // Close the process - err := stop.Close() - if err != nil { - t.Errorf("Failed to signal close to process: %+v", err) - } - }() + select { + case testBundle = <-messageBundleChan: + case <-time.After(30 * time.Millisecond): + t.Errorf("Timed out waiting for messageBundleChan.") + } + + err = stop.Close() + if err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } // Ensure bundle received and has expected values time.Sleep(2 * time.Second) if reflect.DeepEqual(testBundle, message.Bundle{}) { - t.Errorf("Did not receive a message bundle over the channel") - t.FailNow() + t.Fatal("Did not receive a message bundle over the channel") } if testBundle.Identity.EphId.Int64() != expectedEphID.Int64() { @@ -121,8 +120,8 @@ func TestManager_ProcessMessageRetrieval(t *testing.T) { } -// Utilize the mockComms to construct a gateway which does not have the round -func TestManager_ProcessMessageRetrieval_NoRound(t *testing.T) { +// Utilize the mockComms to construct a gateway which does not have the round. +func Test_manager_processMessageRetrieval_NoRound(t *testing.T) { // General initializations testManager := newManager(t) p := gateway.DefaultPoolParams() @@ -191,15 +190,15 @@ func TestManager_ProcessMessageRetrieval_NoRound(t *testing.T) { time.Sleep(2 * time.Second) if !reflect.DeepEqual(testBundle, message.Bundle{}) { - t.Errorf("Should not receive a message bundle, mock gateway should not return round."+ - "\n\tExpected: %v"+ - "\n\tReceived: %v", message.Bundle{}, testBundle) + t.Errorf("Should not receive a message bundle, mock gateway should "+ + "not return round.\nexpected: %+v\nreceived: %+v", + message.Bundle{}, testBundle) } } -// Test the path where there are no messages, -// simulating a false positive in a bloom filter -func TestManager_ProcessMessageRetrieval_FalsePositive(t *testing.T) { +// Test the path where there are no messages. Simulating a false positive in a +// bloom filter. +func Test_manager_processMessageRetrieval_FalsePositive(t *testing.T) { // General initializations testManager := newManager(t) roundId := id.Round(5) @@ -271,14 +270,14 @@ func TestManager_ProcessMessageRetrieval_FalsePositive(t *testing.T) { // Ensure no bundle was received due to false positive test time.Sleep(2 * time.Second) if !reflect.DeepEqual(testBundle, message.Bundle{}) { - t.Errorf("Received a message bundle over the channel, should receive empty message list") - t.FailNow() + t.Fatal("Received a message bundle over the channel, should receive " + + "empty message list") } } -// Ensure that the quit chan closes the program, on an otherwise happy path -func TestManager_ProcessMessageRetrieval_Quit(t *testing.T) { +// Ensure that the quit chan closes the program, on an otherwise happy path. +func Test_manager_processMessageRetrieval_Quit(t *testing.T) { // General initializations testManager := newManager(t) roundId := id.Round(5) @@ -341,14 +340,14 @@ func TestManager_ProcessMessageRetrieval_Quit(t *testing.T) { time.Sleep(1 * time.Second) // Ensure no bundle was received due to quiting process early if !reflect.DeepEqual(testBundle, message.Bundle{}) { - t.Errorf("Received a message bundle over the channel, process should have quit before reception") - t.FailNow() + t.Fatal("Received a message bundle over the channel, process should " + + "have quit before reception") } } -// Path in which multiple error comms are encountered before a happy path comms -func TestManager_ProcessMessageRetrieval_MultipleGateways(t *testing.T) { +// Path in which multiple error comms are encountered before a happy path comms. +func Test_manager_processMessageRetrieval_MultipleGateways(t *testing.T) { // General initializations testManager := newManager(t) roundId := id.Round(5) @@ -390,8 +389,10 @@ func TestManager_ProcessMessageRetrieval_MultipleGateways(t *testing.T) { Source: requestGateway, } - // Create a list of ID's in which some error gateways must be contacted before the happy path - idList := [][]byte{errorGateway.Bytes(), errorGateway.Bytes(), requestGateway.Bytes()} + // Create a list of ID's in which some error gateways must be contacted + // before the happy path + idList := [][]byte{ + errorGateway.Bytes(), errorGateway.Bytes(), requestGateway.Bytes()} roundInfo := &pb.RoundInfo{ ID: uint64(roundId), @@ -406,37 +407,34 @@ func TestManager_ProcessMessageRetrieval_MultipleGateways(t *testing.T) { }() + // Receive the bundle over the channel var testBundle message.Bundle - go func() { - // Receive the bundle over the channel - time.Sleep(1 * time.Second) - testBundle = <-messageBundleChan + select { + case testBundle = <-messageBundleChan: + case <-time.After(30 * time.Millisecond): + t.Errorf("Timed out waiting for messageBundleChan.") + } - // Close the process - if err := stop.Close(); err != nil { - t.Errorf("Failed to signal close to process: %+v", err) - } - }() + // Close the process + err := stop.Close() + if err != nil { + t.Errorf("Failed to signal close to process: %+v", err) + } - // Ensure that expected bundle is still received from happy comm - // despite initial errors + // Ensure that expected bundle is still received from happy comm despite + // initial errors time.Sleep(2 * time.Second) if reflect.DeepEqual(testBundle, message.Bundle{}) { - t.Errorf("Did not receive a message bundle over the channel") - t.FailNow() + t.Fatal("Did not receive a message bundle over the channel.") } if testBundle.Identity.EphId.Int64() != expectedEphID.Int64() { - t.Errorf("Unexpected address ID in bundle."+ - "\n\tExpected: %v"+ - "\n\tReceived: %v", expectedEphID, testBundle.Identity.EphId) + t.Errorf("Unexpected address ID in bundle.\nexpected: %v\nreceived: %v", + expectedEphID, testBundle.Identity.EphId) } if !bytes.Equal(expectedPayload, testBundle.Messages[0].GetPayloadA()) { - t.Errorf("Unexpected address ID in bundle."+ - "\n\tExpected: %v"+ - "\n\tReceived: %v", expectedPayload, testBundle.Messages[0].GetPayloadA()) - + t.Errorf("Unexpected address ID in bundle.\nexpected: %v\nreceived: %v", + expectedPayload, testBundle.Messages[0].GetPayloadA()) } - } diff --git a/network/rounds/store/store.go b/network/rounds/store/store.go index 5d6a52017e3b6945386e14a29aff2e233d2c57b9..6310d581beba91fde2db1bcf985b9059bfbdf4de 100644 --- a/network/rounds/store/store.go +++ b/network/rounds/store/store.go @@ -112,7 +112,7 @@ func (s *UncheckedRoundStore) GetRound(rid id.Round, recipient *id.ID, return rnd, exists } -func (s *UncheckedRoundStore) GetList(*testing.T) map[roundIdentity]UncheckedRound { +func (s *UncheckedRoundStore) GetList(_ *testing.T) map[roundIdentity]UncheckedRound { s.mux.RLock() defer s.mux.RUnlock() return s.list diff --git a/network/rounds/store/uncheckedRounds.go b/network/rounds/store/uncheckedRounds.go index 1177c1ef6ba5d6fde0f4d7b8380834e1a3d5dc78..326fff5c2e9432240de17647cfab452ee9696307 100644 --- a/network/rounds/store/uncheckedRounds.go +++ b/network/rounds/store/uncheckedRounds.go @@ -110,7 +110,7 @@ func (r *UncheckedRound) unmarshal(kv *versioned.KV, buff *bytes.Buffer) error { sourceId, err := id.Unmarshal(buff.Next(id.ArrIDLen)) if err != nil { return errors.WithMessagef(err, - "Failed to unmarshal round identity.source of %d", r.Id) + "Failed to unmarshal round Identity source of round %d", r.Id) } r.Source = sourceId diff --git a/network/rounds/store/uncheckedRounds_test.go b/network/rounds/store/uncheckedRounds_test.go index f8ae0becc2667b8ce327d30b0d7eab2da2a224a2..0e8ba1393ddadd70943ed08048885e33b28832f6 100644 --- a/network/rounds/store/uncheckedRounds_test.go +++ b/network/rounds/store/uncheckedRounds_test.go @@ -33,7 +33,7 @@ func TestNewUncheckedStore(t *testing.T) { t.Fatalf("NewUncheckedStore returned an error: %+v", err) } - // Compare manually created object with NewUnknownRoundsStore + // Compare manually created object with UncheckedRoundStore if !reflect.DeepEqual(testStore, store) { t.Fatalf("NewUncheckedStore returned incorrect Store."+ "\nexpected: %+v\nreceived: %+v", testStore, store) @@ -42,7 +42,8 @@ func TestNewUncheckedStore(t *testing.T) { rid := id.Round(1) roundInfo := &pb.RoundInfo{ID: uint64(rid)} recipient := id.NewIdFromString("recipientID", id.User, t) - ephID, _, _, _ := ephemeral.GetId(recipient, id.ArrIDLen, netTime.Now().UnixNano()) + ephID, _, _, _ := ephemeral.GetId( + recipient, id.ArrIDLen, netTime.Now().UnixNano()) uncheckedRound := UncheckedRound{ Info: roundInfo, LastCheck: netTime.Now(), @@ -72,7 +73,7 @@ func TestNewUncheckedStore(t *testing.T) { } } -// Unit test +// Unit test. func TestLoadUncheckedStore(t *testing.T) { kv := versioned.NewKV(make(ekv.Memstore)) @@ -113,7 +114,7 @@ func TestLoadUncheckedStore(t *testing.T) { } } -// Unit test +// Unit test. func TestUncheckedRoundStore_AddRound(t *testing.T) { kv := versioned.NewKV(make(ekv.Memstore)) @@ -138,7 +139,7 @@ func TestUncheckedRoundStore_AddRound(t *testing.T) { } } -// Unit test +// Unit test. func TestUncheckedRoundStore_GetRound(t *testing.T) { kv := versioned.NewKV(make(ekv.Memstore)) @@ -299,7 +300,7 @@ func TestUncheckedRoundStore_GetList(t *testing.T) { } -// Unit test +// Unit test. func TestUncheckedRoundStore_IncrementCheck(t *testing.T) { kv := versioned.NewKV(make(ekv.Memstore)) @@ -382,7 +383,8 @@ func TestUncheckedRoundStore_Remove(t *testing.T) { source := id.NewIdFromUInt(uint64(removedRound), id.User, t) err = testStore.Remove(removedRound, source, ephId) if err != nil { - t.Errorf("Could not have removed round %d from storage: %v", removedRound, err) + t.Errorf("Could not have removed round %d from storage: %+v", + removedRound, err) } // Check that round was removed @@ -395,6 +397,7 @@ func TestUncheckedRoundStore_Remove(t *testing.T) { unknownRound := id.Round(numRounds + 5) err = testStore.Remove(unknownRound, source, ephId) if err == nil { - t.Errorf("Should not have removed round %d which is not in storage", unknownRound) + t.Errorf("Should not have removed round %d which is not in storage", + unknownRound) } } diff --git a/network/rounds/unchecked_test.go b/network/rounds/unchecked_test.go index 4dbb174f3a19b71faec0b8112bbca0f8a4121808..3f609ad00f7e184f7f2d854bc892db77f09bc8e7 100644 --- a/network/rounds/unchecked_test.go +++ b/network/rounds/unchecked_test.go @@ -22,7 +22,7 @@ import ( "time" ) -// Happy path +// Happy path. func TestUncheckedRoundScheduler(t *testing.T) { // General initializations connect.TestingOnlyDisableTLS = true @@ -64,7 +64,8 @@ func TestUncheckedRoundScheduler(t *testing.T) { } // Add round to check - err := testManager.unchecked.AddRound(roundId, roundInfo, requestGateway, expectedEphID) + err := testManager.unchecked.AddRound( + roundId, roundInfo, requestGateway, expectedEphID) if err != nil { t.Fatalf("Could not add round to session: %v", err) } @@ -85,9 +86,7 @@ func TestUncheckedRoundScheduler(t *testing.T) { } if testBundle.Identity.EphId.Int64() != expectedEphID.Int64() { - t.Errorf("Unexpected address ID in bundle."+ - "\n\tExpected: %v"+ - "\n\tReceived: %v", expectedEphID, testBundle.Identity.EphId) + t.Errorf("Unexpected address ID in bundle.\nexpected: %v\nreceived: %v", + expectedEphID, testBundle.Identity.EphId) } - } diff --git a/network/sendCmix.go b/network/sendCmix.go index c67dbcaca2e3fb7cde1a1991db289c83836c52e2..382f55fbdc80b6527da3d466c9a9323ce402da91 100644 --- a/network/sendCmix.go +++ b/network/sendCmix.go @@ -159,9 +159,9 @@ func sendCmixHelper(sender gateway.Sender, msg format.Message, recipient *id.ID, containsBlacklisted := false if cmixParams.BlacklistedNodes != nil { for _, nodeId := range bestRound.Topology { - var nid *id.ID + var nid id.ID copy(nid[:], nodeId) - if _, isBlacklisted := cmixParams.BlacklistedNodes[*nid]; isBlacklisted { + if _, isBlacklisted := cmixParams.BlacklistedNodes[nid]; isBlacklisted { containsBlacklisted = true break } diff --git a/network/sendManyCmix.go b/network/sendManyCmix.go index 3b7819336158d0dba70cd9bc690ffab31f8cdeba..35b4b7615f4da31bf71c99b8154ec6c91355a4c9 100644 --- a/network/sendManyCmix.go +++ b/network/sendManyCmix.go @@ -126,8 +126,9 @@ func sendManyCmixHelper(sender gateway.Sender, recipientString, msgDigests := messageListToStrings(msgs) - jww.INFO.Printf("[SendManyCMIX-%s]Looking for round to send cMix messages to [%s] "+ - "(msgDigest: %s)", param.DebugTag, recipientString, msgDigests) + jww.INFO.Printf("[SendManyCMIX-%s] Looking for round to send cMix "+ + "messages to [%s] (msgDigest: %s)", param.DebugTag, recipientString, + msgDigests) stream := rng.GetStream() defer stream.Close() @@ -142,15 +143,17 @@ func sendManyCmixHelper(sender gateway.Sender, elapsed := netTime.Since(timeStart) if elapsed > param.Timeout { - jww.INFO.Printf("[SendManyCMIX-%s]No rounds to send to %s (msgDigest: %s) were found "+ - "before timeout %s", param.DebugTag, recipientString, msgDigests, param.Timeout) + jww.INFO.Printf("[SendManyCMIX-%s] No rounds to send to %s "+ + "(msgDigest: %s) were found before timeout %s", param.DebugTag, + recipientString, msgDigests, param.Timeout) return 0, []ephemeral.Id{}, errors.New("sending cMix message timed out") } if numRoundTries > 0 { - jww.INFO.Printf("[SendManyCMIX-%s]Attempt %d to find round to send message to %s "+ - "(msgDigest: %s)", param.DebugTag, numRoundTries+1, recipientString, msgDigests) + jww.INFO.Printf("[SendManyCMIX-%s] Attempt %d to find round to "+ + "send message to %s (msgDigest: %s)", param.DebugTag, + numRoundTries+1, recipientString, msgDigests) } remainingTime := param.Timeout - elapsed @@ -176,8 +179,8 @@ func sendManyCmixHelper(sender gateway.Sender, } } if containsBlacklisted { - jww.WARN.Printf("[SendManyCMIX-%s]Round %d contains blacklisted nodes, skipping...", - param.DebugTag, bestRound.ID) + jww.WARN.Printf("[SendManyCMIX-%s] Round %d contains blacklisted "+ + "nodes, skipping...", param.DebugTag, bestRound.ID) continue } @@ -185,9 +188,11 @@ func sendManyCmixHelper(sender gateway.Sender, firstGateway, roundKeys, err := processRound( registrar, bestRound, recipientString, msgDigests) if err != nil { - jww.INFO.Printf("[SendManyCMIX-%s]error processing round: %v", param.DebugTag, err) - jww.WARN.Printf("[SendManyCMIX-%s]SendManyCMIX failed to process round %d "+ - "(will retry): %+v", param.DebugTag, bestRound.ID, err) + jww.INFO.Printf("[SendManyCMIX-%s] Error processing round: %v", + param.DebugTag, err) + jww.WARN.Printf("[SendManyCMIX-%s] SendManyCMIX failed to "+ + "process round %d (will retry): %+v", param.DebugTag, + bestRound.ID, err) continue } @@ -195,14 +200,15 @@ func sendManyCmixHelper(sender gateway.Sender, slots := make([]*pb.GatewaySlot, len(msgs)) encMsgs := make([]format.Message, len(msgs)) ephemeralIDs := make([]ephemeral.Id, len(msgs)) - stream := rng.GetStream() + stream = rng.GetStream() for i, msg := range msgs { slots[i], encMsgs[i], ephemeralIDs[i], err = buildSlotMessage( msg.Message, msg.Recipient, firstGateway, stream, senderId, bestRound, roundKeys) if err != nil { stream.Close() - jww.INFO.Printf("[SendManyCMIX-%s]error building slot received: %v", param.DebugTag, err) + jww.INFO.Printf("[SendManyCMIX-%s] Error building slot "+ + "received: %v", param.DebugTag, err) return 0, []ephemeral.Id{}, errors.Errorf("failed to build "+ "slot message for %s: %+v", msg.Recipient, err) } @@ -258,13 +264,16 @@ func sendManyCmixHelper(sender gateway.Sender, // If the comm errors or the message fails to send, continue retrying if err != nil { if !strings.Contains(err.Error(), unrecoverableError) { - jww.ERROR.Printf("[SendManyCMIX-%s]SendManyCMIX failed to send to EphIDs [%s] "+ - "(sources: %s) on round %d, trying a new round %+v", - param.DebugTag, ephemeralIDsString, recipientString, bestRound.ID, err) - jww.INFO.Printf("[SendManyCMIX-%s]error received, continuing: %v", param.DebugTag, err) + jww.ERROR.Printf("[SendManyCMIX-%s] SendManyCMIX failed to "+ + "send to EphIDs [%s] (sources: %s) on round %d, trying "+ + "a new round %+v", param.DebugTag, ephemeralIDsString, + recipientString, bestRound.ID, err) + jww.INFO.Printf("[SendManyCMIX-%s] Error received, "+ + "continuing: %v", param.DebugTag, err) continue } else { - jww.INFO.Printf("[SendManyCMIX-%s]Error received: %v", param.DebugTag, err) + jww.INFO.Printf("[SendManyCMIX-%s] Error received: %v", + param.DebugTag, err) } return 0, []ephemeral.Id{}, err } @@ -272,15 +281,18 @@ func sendManyCmixHelper(sender gateway.Sender, // Return if it sends properly gwSlotResp := result.(*pb.GatewaySlotResponse) if gwSlotResp.Accepted { - m := fmt.Sprintf("[SendManyCMIX-%s]Successfully sent to EphIDs %s (sources: [%s]) "+ - "in round %d (msgDigest: %s)", param.DebugTag, ephemeralIDsString, recipientString, bestRound.ID, msgDigests) + m := fmt.Sprintf("[SendManyCMIX-%s] Successfully sent to EphIDs "+ + "%s (sources: [%s]) in round %d (msgDigest: %s)", + param.DebugTag, ephemeralIDsString, recipientString, + bestRound.ID, msgDigests) jww.INFO.Print(m) events.Report(1, "MessageSendMany", "Metric", m) return id.Round(bestRound.ID), ephemeralIDs, nil } else { - jww.FATAL.Panicf("[SendManyCMIX-%s]Gateway %s returned no error, but failed to "+ - "accept message when sending to EphIDs [%s] (%s) on round %d", param.DebugTag, - firstGateway, ephemeralIDsString, recipientString, bestRound.ID) + jww.FATAL.Panicf("[SendManyCMIX-%s] Gateway %s returned no "+ + "error, but failed to accept message when sending to EphIDs "+ + "[%s] (%s) on round %d", param.DebugTag, firstGateway, + ephemeralIDsString, recipientString, bestRound.ID) } }