diff --git a/api/messages.go b/api/messages.go index 870311ef31348cb17a14a18e41288df6e16fd0f1..933af975b831491ff22fd7cf6e311f040e77e5aa 100644 --- a/api/messages.go +++ b/api/messages.go @@ -7,7 +7,6 @@ package api import ( - "fmt" jww "github.com/spf13/jwalterweatherman" "gitlab.com/elixxir/client/globals" "gitlab.com/elixxir/client/network/gateway" @@ -92,7 +91,6 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration, roundsResults[rnd] = Succeeded } else if states.Round(roundInfo.State) == states.FAILED { roundsResults[rnd] = Failed - fmt.Printf("Round %d considered %v\n", roundInfo.ID, roundInfo.State) allRoundsSucceeded = false } else { // If in progress, add a channel monitoring its status @@ -128,11 +126,9 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration, // Create the results timer timer := time.NewTimer(timeout) for { - fmt.Printf("looping at most: %v\n", numResults) // If we know about all rounds, return if numResults == 0 { - fmt.Printf("passing to report the following: %v\n", allRoundsSucceeded) roundCallback.Report(allRoundsSucceeded, false, roundsResults) return } @@ -140,11 +136,9 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration, // Wait for info about rounds or the timeout to occur select { case <-timer.C: - fmt.Printf("timed out\n") roundCallback.Report(false, true, roundsResults) return case roundReport := <-sendResults: - fmt.Printf("roundReport: %v\n", roundReport) numResults-- // Skip if the round is nil (unknown from historical rounds) // they default to timed out, so correct behavior is preserved @@ -154,12 +148,10 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration, // If available, denote the result roundId := id.Round(roundReport.RoundInfo.ID) if states.Round(roundReport.RoundInfo.State) == states.COMPLETED { - fmt.Printf("round %d marked successful\n", roundId) roundsResults[roundId] = Succeeded } else { roundsResults[roundId] = Failed allRoundsSucceeded = false - fmt.Printf("Round [unknown] considered [failed]\n") } } @@ -197,7 +189,6 @@ func (c *Client) getHistoricalRounds(msg *pb.HistoricalRounds, // Process historical rounds, sending back to the caller thread for _, ri := range resp.Rounds { - fmt.Printf("received rounds from gateway: %v\n", ri) sendResults <- ds.EventReturn{ ri, false, diff --git a/api/messages_test.go b/api/messages_test.go index a643e5ef97512b77297387ca605e0f498c8df3f9..6dacd0963062c1ca6e28686c5893f8ba35d758c5 100644 --- a/api/messages_test.go +++ b/api/messages_test.go @@ -11,26 +11,12 @@ import ( ds "gitlab.com/elixxir/comms/network/dataStructures" "gitlab.com/elixxir/primitives/states" "gitlab.com/xx_network/primitives/id" - "os" "testing" "time" ) const numRounds = 10 -var testClient *Client - -func TestMain(m *testing.M) { - var err error - testClient, err = newTestingClient(m) - t := testing.T{} - if err != nil { - t.Errorf("Failed in setup: %v", err) - } - - os.Exit(m.Run()) -} - // Happy path func TestClient_GetRoundResults(t *testing.T) { // Populate a round list to request @@ -51,13 +37,17 @@ func TestClient_GetRoundResults(t *testing.T) { } } - // Create a new copy of the test client for this test - client := &Client{} - *client = *testClient + //// 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, + err = client.getRoundResults(roundList, time.Duration(10)*time.Millisecond, receivedRCB, sendResults, NewNoHistoricalRoundsComm()) if err != nil { t.Errorf("Error in happy path: %v", err) @@ -100,23 +90,21 @@ func TestClient_GetRoundResults_FailedRounds(t *testing.T) { } if i == numRounds-2 { result.RoundInfo.State = uint32(states.FAILED) - sendResults <- result - } else if i == numRounds-1 { - result.TimedOut = true - sendResults <- result - } else { - sendResults <- result } + sendResults <- result + } - // Create a new copy of the test client for this test - client := &Client{} - *client = *testClient + //// 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, + err = client.getRoundResults(roundList, time.Duration(10)*time.Millisecond, receivedRCB, sendResults, NewNoHistoricalRoundsComm()) if err != nil { t.Errorf("Error in happy path: %v", err) @@ -125,8 +113,8 @@ func TestClient_GetRoundResults_FailedRounds(t *testing.T) { // Sleep to allow the report to come through the pipeline time.Sleep(2 * time.Second) - // If no rounds have timed out or no round failed, this test has failed - if !receivedRCB.timedOut || receivedRCB.allRoundsSucceeded { + // If no rounds have failed, this test has failed + if receivedRCB.allRoundsSucceeded { t.Errorf("Expected some rounds to fail and others to timeout. "+ "\n\tTimedOut: %v"+ "\n\tallRoundsSucceeded: %v", receivedRCB.timedOut, receivedRCB.allRoundsSucceeded) @@ -134,42 +122,8 @@ func TestClient_GetRoundResults_FailedRounds(t *testing.T) { } -// Force some timeouts by not populating the entire results channel -func TestClient_GetRoundResults_Timeout(t *testing.T) { - // Populate a round list to request - var roundList []id.Round - for i := 0; i < numRounds; i++ { - roundList = append(roundList, id.Round(i)) - } - - // Generate a results which never sends (empty chan) - sendResults := make(chan ds.EventReturn) - - // Create a new copy of the test client for this test - client := &Client{} - *client = *testClient - - // Call the round results - receivedRCB := NewMockRoundCB() - err := client.getRoundResults(roundList, time.Duration(10)*time.Millisecond, - receivedRCB, sendResults, NewNoHistoricalRoundsComm()) - if err != nil { - t.Errorf("Error in happy path: %v", err) - } - // Sleep to allow the report to come through the pipeline - time.Sleep(2*time.Second) - - // 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."+ - "\n\tTimedOut: %v", receivedRCB.timedOut) - } - -} - // Use the historical rounds interface which actually sends back rounds -func TestClient_GetRoundResults_HistoricalRounds(t *testing.T) { +func TestClient_GetRoundResults_HistoricalRounds(t *testing.T) { // Populate a round list to request var roundList []id.Round for i := 0; i < numRounds; i++ { @@ -181,7 +135,9 @@ func TestClient_GetRoundResults_HistoricalRounds(t *testing.T) { for i := 0; i < numRounds; i++ { // Skip sending rounds intended for historical rounds comm if i == failedHistoricalRoundID || - i == completedHistoricalRoundID {continue} + i == completedHistoricalRoundID { + continue + } sendResults <- ds.EventReturn{ RoundInfo: &pb.RoundInfo{ @@ -192,14 +148,17 @@ func TestClient_GetRoundResults_HistoricalRounds(t *testing.T) { } } - // Create a new copy of the test client for this test - client := &Client{} - *client = *testClient + client, err := newTestingClient(t) + if err != nil { + 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++ { + for i := 1; i <= ds.RoundInfoBufLen+completedHistoricalRoundID+1; i++ { ri := &pb.RoundInfo{ID: uint64(i)} signRoundInfo(ri) client.network.GetInstance().RoundUpdate(ri) @@ -208,19 +167,59 @@ func TestClient_GetRoundResults_HistoricalRounds(t *testing.T) { // Call the round results receivedRCB := NewMockRoundCB() - err := client.getRoundResults(roundList, time.Duration(10)*time.Millisecond, + err = client.getRoundResults(roundList, time.Duration(10)*time.Millisecond, receivedRCB, sendResults, NewHistoricalRoundsComm()) if err != nil { t.Errorf("Error in happy path: %v", err) } + // Sleep to allow the report to come through the pipeline - time.Sleep(2*time.Second) + time.Sleep(2 * time.Second) // If no round failed, this test has failed - if receivedRCB.allRoundsSucceeded { + if receivedRCB.allRoundsSucceeded { + t.Errorf("Expected historical rounds to have a failure.") + } +} + +// Force some timeouts by not populating the entire results channel +func TestClient_GetRoundResults_Timeout(t *testing.T) { + // Populate a round list to request + var roundList []id.Round + for i := 0; i < numRounds; i++ { + roundList = append(roundList, id.Round(i)) + } + + // Create a broken channel which will never send, + // forcing a timeout + var sendResults chan ds.EventReturn + sendResults = nil + + // 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, + receivedRCB, sendResults, NewNoHistoricalRoundsComm()) + if err != nil { + t.Errorf("Error in happy path: %v", err) + } + + // Sleep to allow the report to come through the pipeline + time.Sleep(2 * time.Second) + + // 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."+ - "\n\tTimedOut: %v"+ - "\n\tallRoundsSucceeded: %v", receivedRCB.timedOut, receivedRCB.allRoundsSucceeded) + "\n\tTimedOut: %v", receivedRCB.timedOut) } -} \ No newline at end of file + +} diff --git a/api/utilsInterfaces_test.go b/api/utilsInterfaces_test.go index e496fc7f762c4de24f552bd9c858dd569fce64c4..cb3675cc7ec1d9abdb908295e167eb2c482f7623 100644 --- a/api/utilsInterfaces_test.go +++ b/api/utilsInterfaces_test.go @@ -7,7 +7,6 @@ package api import ( - "fmt" "gitlab.com/elixxir/client/interfaces" "gitlab.com/elixxir/client/interfaces/message" "gitlab.com/elixxir/client/interfaces/params" @@ -36,8 +35,6 @@ func NewMockRoundCB() *mockRoundCallback { // Report simply stores the passed in values in the structure func (mrc *mockRoundCallback) Report(allRoundsSucceeded, timedOut bool, rounds map[id.Round]RoundResult) { - fmt.Printf("allRoundsSucceeded: %v\n", allRoundsSucceeded) - fmt.Printf("timedOut: %v\n", timedOut) mrc.allRoundsSucceeded = allRoundsSucceeded mrc.timedOut = timedOut diff --git a/api/utils_test.go b/api/utils_test.go index dee8810ee4e037ea7e463138447002b3855b76ec..74360610803c2107181f87efa0f29363d6da3f80 100644 --- a/api/utils_test.go +++ b/api/utils_test.go @@ -58,7 +58,6 @@ func newTestingClient(face interface{}) (*Client, error) { Manager: commsManager, } - thisInstance, err := network.NewInstanceTesting(instanceComms, def, def, nil, nil, face) if err != nil { return nil, nil @@ -84,16 +83,16 @@ func getNDF(face interface{}) *ndf.NetworkDefinition { Registration: ndf.Registration{ TlsCertificate: string(cert), }, - Nodes: []ndf.Node { + Nodes: []ndf.Node{ { - ID: nodeID.Bytes(), + ID: nodeID.Bytes(), Address: "", TlsCertificate: string(cert), }, }, - Gateways: []ndf.Gateway { + Gateways: []ndf.Gateway{ { - ID: nodeID.Bytes(), + ID: nodeID.Bytes(), Address: "", TlsCertificate: string(cert), }, @@ -149,5 +148,4 @@ func signRoundInfo(ri *pb.RoundInfo) error { return signature.Sign(ri, ourPrivateKey) - -} \ No newline at end of file +}