Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds polling on the nodes' state instead of sleeping #109

Merged
merged 3 commits into from
May 10, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 37 additions & 5 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func getIntegrationTest(numNodes, numVotes int) func(*testing.T) {
err = closeElection(m, electionID, adminID)
require.NoError(t, err)

time.Sleep(time.Second * 1)
waitForStatus(types.Closed, electionFac, electionID, nodes, t, numNodes, 2*time.Second)

// ##### SHUFFLE BALLOTS #####
t.Logf("initializing shuffle")
Expand All @@ -124,7 +124,8 @@ func getIntegrationTest(numNodes, numVotes int) func(*testing.T) {
err = sActor.Shuffle(electionID)
require.NoError(t, err)

time.Sleep(time.Second * 1)
waitForStatus(types.ShuffledBallots, electionFac, electionID, nodes, t,
numNodes, 2*time.Second*time.Duration(numNodes))

// ##### SUBMIT PUBLIC SHARES #####
t.Logf("submitting public shares")
Expand All @@ -134,9 +135,10 @@ func getIntegrationTest(numNodes, numVotes int) func(*testing.T) {
err = actor.ComputePubshares()
require.NoError(t, err)

// ##### DECRYPT BALLOTS #####
time.Sleep(time.Millisecond * 5000 * time.Duration(numNodes))
waitForStatus(types.PubSharesSubmitted, electionFac, electionID, nodes, t,
numNodes, 6*time.Second*time.Duration(numNodes))

// ##### DECRYPT BALLOTS #####
t.Logf("decrypting")

election, err = getElection(electionFac, electionID, nodes[0].GetOrdering())
Expand All @@ -145,7 +147,8 @@ func getIntegrationTest(numNodes, numVotes int) func(*testing.T) {
err = decryptBallots(m, actor, election)
require.NoError(t, err)

time.Sleep(time.Second * 1)
waitForStatus(types.ResultAvailable, electionFac, electionID, nodes, t,
numNodes, 500*time.Millisecond*time.Duration(numVotes))

t.Logf("get vote proof")
election, err = getElection(electionFac, electionID, nodes[0].GetOrdering())
Expand Down Expand Up @@ -570,3 +573,32 @@ func closeNodes(t *testing.T, nodes []dVotingCosiDela) {
func encodeID(ID string) types.ID {
return types.ID(base64.StdEncoding.EncodeToString([]byte(ID)))
}

// waitForStatus polls the node until they all updated to the expected status
// for the given election. An error is raised if the timeout expires.
func waitForStatus(status types.Status, electionFac types.ElectionFactory, electionID []byte,
nodes []dVotingCosiDela, t *testing.T, numNodes int, timeOut time.Duration) {
// setup a timer to fail the test in case we never reach the status
timer := time.NewTimer(timeOut)
go func() {
<-timer.C
t.Errorf("timed out while waiting for status %d", status)
}()

loop := true
for loop {
loop = false
for i := 0; i < numNodes; i++ {
election, err := getElection(electionFac, electionID, nodes[i].GetOrdering())
require.NoError(t, err)

if election.Status != status {
loop = true
}
}
if loop {
time.Sleep(time.Millisecond * 100)
}
}
timer.Stop()
}