Skip to content
Snippets Groups Projects
Commit c9e22d7c authored by Josh Brooks's avatar Josh Brooks
Browse files

Fix testing issues and clean up code

parent 597afd47
No related branches found
No related tags found
No related merge requests found
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
package api package api
import ( import (
"fmt"
jww "github.com/spf13/jwalterweatherman" jww "github.com/spf13/jwalterweatherman"
"gitlab.com/elixxir/client/globals" "gitlab.com/elixxir/client/globals"
"gitlab.com/elixxir/client/network/gateway" "gitlab.com/elixxir/client/network/gateway"
...@@ -92,7 +91,6 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration, ...@@ -92,7 +91,6 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration,
roundsResults[rnd] = Succeeded roundsResults[rnd] = Succeeded
} else if states.Round(roundInfo.State) == states.FAILED { } else if states.Round(roundInfo.State) == states.FAILED {
roundsResults[rnd] = Failed roundsResults[rnd] = Failed
fmt.Printf("Round %d considered %v\n", roundInfo.ID, roundInfo.State)
allRoundsSucceeded = false allRoundsSucceeded = false
} else { } else {
// If in progress, add a channel monitoring its status // If in progress, add a channel monitoring its status
...@@ -128,11 +126,9 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration, ...@@ -128,11 +126,9 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration,
// Create the results timer // Create the results timer
timer := time.NewTimer(timeout) timer := time.NewTimer(timeout)
for { for {
fmt.Printf("looping at most: %v\n", numResults)
// If we know about all rounds, return // If we know about all rounds, return
if numResults == 0 { if numResults == 0 {
fmt.Printf("passing to report the following: %v\n", allRoundsSucceeded)
roundCallback.Report(allRoundsSucceeded, false, roundsResults) roundCallback.Report(allRoundsSucceeded, false, roundsResults)
return return
} }
...@@ -140,11 +136,9 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration, ...@@ -140,11 +136,9 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration,
// Wait for info about rounds or the timeout to occur // Wait for info about rounds or the timeout to occur
select { select {
case <-timer.C: case <-timer.C:
fmt.Printf("timed out\n")
roundCallback.Report(false, true, roundsResults) roundCallback.Report(false, true, roundsResults)
return return
case roundReport := <-sendResults: case roundReport := <-sendResults:
fmt.Printf("roundReport: %v\n", roundReport)
numResults-- numResults--
// Skip if the round is nil (unknown from historical rounds) // Skip if the round is nil (unknown from historical rounds)
// they default to timed out, so correct behavior is preserved // they default to timed out, so correct behavior is preserved
...@@ -154,12 +148,10 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration, ...@@ -154,12 +148,10 @@ func (c *Client) getRoundResults(roundList []id.Round, timeout time.Duration,
// If available, denote the result // If available, denote the result
roundId := id.Round(roundReport.RoundInfo.ID) roundId := id.Round(roundReport.RoundInfo.ID)
if states.Round(roundReport.RoundInfo.State) == states.COMPLETED { if states.Round(roundReport.RoundInfo.State) == states.COMPLETED {
fmt.Printf("round %d marked successful\n", roundId)
roundsResults[roundId] = Succeeded roundsResults[roundId] = Succeeded
} else { } else {
roundsResults[roundId] = Failed roundsResults[roundId] = Failed
allRoundsSucceeded = false allRoundsSucceeded = false
fmt.Printf("Round [unknown] considered [failed]\n")
} }
} }
...@@ -197,7 +189,6 @@ func (c *Client) getHistoricalRounds(msg *pb.HistoricalRounds, ...@@ -197,7 +189,6 @@ func (c *Client) getHistoricalRounds(msg *pb.HistoricalRounds,
// Process historical rounds, sending back to the caller thread // Process historical rounds, sending back to the caller thread
for _, ri := range resp.Rounds { for _, ri := range resp.Rounds {
fmt.Printf("received rounds from gateway: %v\n", ri)
sendResults <- ds.EventReturn{ sendResults <- ds.EventReturn{
ri, ri,
false, false,
......
...@@ -11,26 +11,12 @@ import ( ...@@ -11,26 +11,12 @@ import (
ds "gitlab.com/elixxir/comms/network/dataStructures" ds "gitlab.com/elixxir/comms/network/dataStructures"
"gitlab.com/elixxir/primitives/states" "gitlab.com/elixxir/primitives/states"
"gitlab.com/xx_network/primitives/id" "gitlab.com/xx_network/primitives/id"
"os"
"testing" "testing"
"time" "time"
) )
const numRounds = 10 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 // Happy path
func TestClient_GetRoundResults(t *testing.T) { func TestClient_GetRoundResults(t *testing.T) {
// Populate a round list to request // Populate a round list to request
...@@ -51,13 +37,17 @@ func TestClient_GetRoundResults(t *testing.T) { ...@@ -51,13 +37,17 @@ 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 := &Client{} client, err := newTestingClient(t)
*client = *testClient if err != nil {
t.Errorf("Failed in setup: %v", err)
}
// Call the round results // Call the round results
receivedRCB := NewMockRoundCB() receivedRCB := NewMockRoundCB()
err := client.getRoundResults(roundList, time.Duration(10)*time.Millisecond, err = client.getRoundResults(roundList, time.Duration(10)*time.Millisecond,
receivedRCB, sendResults, NewNoHistoricalRoundsComm()) receivedRCB, sendResults, NewNoHistoricalRoundsComm())
if err != nil { if err != nil {
t.Errorf("Error in happy path: %v", err) t.Errorf("Error in happy path: %v", err)
...@@ -100,23 +90,21 @@ func TestClient_GetRoundResults_FailedRounds(t *testing.T) { ...@@ -100,23 +90,21 @@ func TestClient_GetRoundResults_FailedRounds(t *testing.T) {
} }
if i == numRounds-2 { if i == numRounds-2 {
result.RoundInfo.State = uint32(states.FAILED) 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 //// Create a new copy of the test client for this test
client := &Client{} client, err := newTestingClient(t)
*client = *testClient if err != nil {
t.Errorf("Failed in setup: %v", err)
}
// Call the round results // Call the round results
receivedRCB := NewMockRoundCB() receivedRCB := NewMockRoundCB()
err := client.getRoundResults(roundList, time.Duration(10)*time.Millisecond, err = client.getRoundResults(roundList, time.Duration(10)*time.Millisecond,
receivedRCB, sendResults, NewNoHistoricalRoundsComm()) receivedRCB, sendResults, NewNoHistoricalRoundsComm())
if err != nil { if err != nil {
t.Errorf("Error in happy path: %v", err) t.Errorf("Error in happy path: %v", err)
...@@ -125,8 +113,8 @@ func TestClient_GetRoundResults_FailedRounds(t *testing.T) { ...@@ -125,8 +113,8 @@ func TestClient_GetRoundResults_FailedRounds(t *testing.T) {
// Sleep to allow the report to come through the pipeline // Sleep to allow the report to come through the pipeline
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
// If no rounds have timed out or no round failed, this test has failed // If no rounds have failed, this test has failed
if !receivedRCB.timedOut || receivedRCB.allRoundsSucceeded { if receivedRCB.allRoundsSucceeded {
t.Errorf("Expected some rounds to fail and others to timeout. "+ t.Errorf("Expected some rounds to fail and others to timeout. "+
"\n\tTimedOut: %v"+ "\n\tTimedOut: %v"+
"\n\tallRoundsSucceeded: %v", receivedRCB.timedOut, receivedRCB.allRoundsSucceeded) "\n\tallRoundsSucceeded: %v", receivedRCB.timedOut, receivedRCB.allRoundsSucceeded)
...@@ -134,40 +122,6 @@ func TestClient_GetRoundResults_FailedRounds(t *testing.T) { ...@@ -134,40 +122,6 @@ 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 // 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 // Populate a round list to request
...@@ -181,7 +135,9 @@ func TestClient_GetRoundResults_HistoricalRounds(t *testing.T) { ...@@ -181,7 +135,9 @@ func TestClient_GetRoundResults_HistoricalRounds(t *testing.T) {
for i := 0; i < numRounds; i++ { for i := 0; i < numRounds; i++ {
// Skip sending rounds intended for historical rounds comm // Skip sending rounds intended for historical rounds comm
if i == failedHistoricalRoundID || if i == failedHistoricalRoundID ||
i == completedHistoricalRoundID {continue} i == completedHistoricalRoundID {
continue
}
sendResults <- ds.EventReturn{ sendResults <- ds.EventReturn{
RoundInfo: &pb.RoundInfo{ RoundInfo: &pb.RoundInfo{
...@@ -192,10 +148,13 @@ func TestClient_GetRoundResults_HistoricalRounds(t *testing.T) { ...@@ -192,10 +148,13 @@ func TestClient_GetRoundResults_HistoricalRounds(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 := &Client{} client, err := newTestingClient(t)
*client = *testClient if err != nil {
t.Errorf("Failed in setup: %v", err)
}
// Overpopulate the round buffer, ensuring a circle back of the ring buffer // Overpopulate the round buffer, ensuring a circle back of the ring buffer
...@@ -208,19 +167,59 @@ func TestClient_GetRoundResults_HistoricalRounds(t *testing.T) { ...@@ -208,19 +167,59 @@ func TestClient_GetRoundResults_HistoricalRounds(t *testing.T) {
// Call the round results // Call the round results
receivedRCB := NewMockRoundCB() receivedRCB := NewMockRoundCB()
err := client.getRoundResults(roundList, time.Duration(10)*time.Millisecond, err = client.getRoundResults(roundList, time.Duration(10)*time.Millisecond,
receivedRCB, sendResults, NewHistoricalRoundsComm()) receivedRCB, sendResults, NewHistoricalRoundsComm())
if err != nil { if err != nil {
t.Errorf("Error in happy path: %v", err) t.Errorf("Error in happy path: %v", err)
} }
// Sleep to allow the report to come through the pipeline // 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 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. "+ t.Errorf("Unexpected round failures in happy path. "+
"Expected all rounds to succeed with no timeouts."+ "Expected all rounds to succeed with no timeouts."+
"\n\tTimedOut: %v"+ "\n\tTimedOut: %v", receivedRCB.timedOut)
"\n\tallRoundsSucceeded: %v", receivedRCB.timedOut, receivedRCB.allRoundsSucceeded)
} }
} }
...@@ -7,7 +7,6 @@ ...@@ -7,7 +7,6 @@
package api package api
import ( import (
"fmt"
"gitlab.com/elixxir/client/interfaces" "gitlab.com/elixxir/client/interfaces"
"gitlab.com/elixxir/client/interfaces/message" "gitlab.com/elixxir/client/interfaces/message"
"gitlab.com/elixxir/client/interfaces/params" "gitlab.com/elixxir/client/interfaces/params"
...@@ -36,8 +35,6 @@ func NewMockRoundCB() *mockRoundCallback { ...@@ -36,8 +35,6 @@ func NewMockRoundCB() *mockRoundCallback {
// Report simply stores the passed in values in the structure // Report simply stores the passed in values in the structure
func (mrc *mockRoundCallback) Report(allRoundsSucceeded, timedOut bool, func (mrc *mockRoundCallback) Report(allRoundsSucceeded, timedOut bool,
rounds map[id.Round]RoundResult) { rounds map[id.Round]RoundResult) {
fmt.Printf("allRoundsSucceeded: %v\n", allRoundsSucceeded)
fmt.Printf("timedOut: %v\n", timedOut)
mrc.allRoundsSucceeded = allRoundsSucceeded mrc.allRoundsSucceeded = allRoundsSucceeded
mrc.timedOut = timedOut mrc.timedOut = timedOut
......
...@@ -58,7 +58,6 @@ func newTestingClient(face interface{}) (*Client, error) { ...@@ -58,7 +58,6 @@ func newTestingClient(face interface{}) (*Client, error) {
Manager: commsManager, Manager: commsManager,
} }
thisInstance, err := network.NewInstanceTesting(instanceComms, def, def, nil, nil, face) thisInstance, err := network.NewInstanceTesting(instanceComms, def, def, nil, nil, face)
if err != nil { if err != nil {
return nil, nil return nil, nil
...@@ -149,5 +148,4 @@ func signRoundInfo(ri *pb.RoundInfo) error { ...@@ -149,5 +148,4 @@ func signRoundInfo(ri *pb.RoundInfo) error {
return signature.Sign(ri, ourPrivateKey) return signature.Sign(ri, ourPrivateKey)
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment