From a84ec4bddb788c8f7c6d9af5e955a23e09dda9b8 Mon Sep 17 00:00:00 2001 From: josh <josh@elixxir.io> Date: Thu, 18 Feb 2021 09:48:23 -0800 Subject: [PATCH] General testing code clean up --- api/messages_test.go | 31 +++++++++++++++---------------- api/utilsInterfaces_test.go | 25 +++++++++++-------------- 2 files changed, 26 insertions(+), 30 deletions(-) diff --git a/api/messages_test.go b/api/messages_test.go index 6dacd0963..c4e85d129 100644 --- a/api/messages_test.go +++ b/api/messages_test.go @@ -37,14 +37,12 @@ func TestClient_GetRoundResults(t *testing.T) { } } - //// Create a new copy of the test client for this test + // Create a new copy of the test client for this test client, err := newTestingClient(t) if err != nil { t.Errorf("Failed in setup: %v", err) } - - // Call the round results receivedRCB := NewMockRoundCB() err = client.getRoundResults(roundList, time.Duration(10)*time.Millisecond, @@ -96,7 +94,7 @@ func TestClient_GetRoundResults_FailedRounds(t *testing.T) { } - //// Create a new copy of the test client for this test + // Create a new copy of the test client for this test client, err := newTestingClient(t) if err != nil { t.Errorf("Failed in setup: %v", err) @@ -115,7 +113,7 @@ func TestClient_GetRoundResults_FailedRounds(t *testing.T) { // If no rounds have failed, this test has failed if receivedRCB.allRoundsSucceeded { - t.Errorf("Expected some rounds to fail and others to timeout. "+ + t.Errorf("Expected some rounds to fail. "+ "\n\tTimedOut: %v"+ "\n\tallRoundsSucceeded: %v", receivedRCB.timedOut, receivedRCB.allRoundsSucceeded) } @@ -154,14 +152,17 @@ func TestClient_GetRoundResults_HistoricalRounds(t *testing.T) { t.Errorf("Failed in setup: %v", err) } - - - // Overpopulate the round buffer, ensuring a circle back of the ring buffer for i := 1; i <= ds.RoundInfoBufLen+completedHistoricalRoundID+1; i++ { ri := &pb.RoundInfo{ID: uint64(i)} - signRoundInfo(ri) - client.network.GetInstance().RoundUpdate(ri) + if err = signRoundInfo(ri); err != nil { + t.Errorf("Failed to sign round in set up: %v", err) + } + + err = client.network.GetInstance().RoundUpdate(ri) + if err != nil { + t.Errorf("Failed to upsert round in set up: %v", err) + } } @@ -178,7 +179,9 @@ func TestClient_GetRoundResults_HistoricalRounds(t *testing.T) { // If no round failed, this test has failed if receivedRCB.allRoundsSucceeded { - t.Errorf("Expected historical rounds to have a failure.") + t.Errorf("Expected historical rounds to have round failures"+ + "\n\tTimedOut: %v"+ + "\n\tallRoundsSucceeded: %v", receivedRCB.timedOut, receivedRCB.allRoundsSucceeded) } } @@ -201,9 +204,6 @@ func TestClient_GetRoundResults_Timeout(t *testing.T) { t.Errorf("Failed in setup: %v", err) } - - - // Call the round results receivedRCB := NewMockRoundCB() err = client.getRoundResults(roundList, time.Duration(10)*time.Millisecond, @@ -217,8 +217,7 @@ func TestClient_GetRoundResults_Timeout(t *testing.T) { // If no rounds have timed out , this test has failed if !receivedRCB.timedOut { - t.Errorf("Unexpected round failures in happy path. "+ - "Expected all rounds to succeed with no timeouts."+ + t.Errorf("Expected all rounds to timeout with no valid round reporter."+ "\n\tTimedOut: %v", receivedRCB.timedOut) } diff --git a/api/utilsInterfaces_test.go b/api/utilsInterfaces_test.go index cb3675cc7..be3000b56 100644 --- a/api/utilsInterfaces_test.go +++ b/api/utilsInterfaces_test.go @@ -28,6 +28,7 @@ type mockRoundCallback struct { rounds map[id.Round]RoundResult } +// Construction for mockRoundCallback func NewMockRoundCB() *mockRoundCallback { return &mockRoundCallback{} } @@ -41,9 +42,10 @@ func (mrc *mockRoundCallback) Report(allRoundsSucceeded, timedOut bool, mrc.rounds = rounds } -// Generate a mock comm which returns no historical round data +// Mock comm struct which returns no historical round data type noHistoricalRounds struct{} +// Constructor for noHistoricalRounds func NewNoHistoricalRoundsComm() *noHistoricalRounds { return &noHistoricalRounds{} } @@ -53,6 +55,8 @@ func (ht *noHistoricalRounds) RequestHistoricalRounds(host *connect.Host, message *pb.HistoricalRounds) (*pb.HistoricalRoundsResponse, error) { return nil, nil } + +// Built for interface adherence func (ht *noHistoricalRounds) GetHost(hostId *id.ID) (*connect.Host, bool) { return nil, false } @@ -60,16 +64,19 @@ func (ht *noHistoricalRounds) GetHost(hostId *id.ID) (*connect.Host, bool) { // Generate a mock comm which returns some historical round data type historicalRounds struct{} +// Constructor for historicalRounds comm interface func NewHistoricalRoundsComm() *historicalRounds { return &historicalRounds{} } -// Return one successful and one failed mock round +// Round IDs to return on mock historicalRounds comm const failedHistoricalRoundID = 7 const completedHistoricalRoundID = 8 +// Mock comms endpoint which returns historical rounds func (ht *historicalRounds) RequestHistoricalRounds(host *connect.Host, message *pb.HistoricalRounds) (*pb.HistoricalRoundsResponse, error) { + // Return one successful and one failed mock round failedRound := &pb.RoundInfo{ ID: failedHistoricalRoundID, State: uint32(states.FAILED), @@ -85,6 +92,7 @@ func (ht *historicalRounds) RequestHistoricalRounds(host *connect.Host, }, nil } +// Build for interface adherence func (ht *historicalRounds) GetHost(hostId *id.ID) (*connect.Host, bool) { return nil, true } @@ -94,45 +102,34 @@ type testNetworkManagerGeneric struct { instance *network.Instance } +/* Below methods built for interface adherence */ func (t *testNetworkManagerGeneric) GetHealthTracker() interfaces.HealthTracker { return nil } - func (t *testNetworkManagerGeneric) Follow() (stoppable.Stoppable, error) { return nil, nil } - func (t *testNetworkManagerGeneric) CheckGarbledMessages() { return } - func (t *testNetworkManagerGeneric) SendE2E(m message.Send, p params.E2E) ( []id.Round, cE2e.MessageID, error) { rounds := []id.Round{id.Round(0), id.Round(1), id.Round(2)} return rounds, cE2e.MessageID{}, nil } - func (t *testNetworkManagerGeneric) SendUnsafe(m message.Send, p params.Unsafe) ([]id.Round, error) { - return nil, nil } - func (t *testNetworkManagerGeneric) SendCMIX(message format.Message, rid *id.ID, p params.CMIX) (id.Round, ephemeral.Id, error) { - return id.Round(0), ephemeral.Id{}, nil - } - func (t *testNetworkManagerGeneric) GetInstance() *network.Instance { return t.instance - } - func (t *testNetworkManagerGeneric) RegisterWithPermissioning(string) ([]byte, error) { return nil, nil } - func (t *testNetworkManagerGeneric) GetRemoteVersion() (string, error) { return "test", nil } -- GitLab