Skip to content

Commit

Permalink
tests: flaky tests fixes (#5487)
Browse files Browse the repository at this point in the history
  • Loading branch information
algorandskiy authored Jun 22, 2023
1 parent 7a282c8 commit f5ad6a5
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ ALWAYS:
# build our fork of libsodium, placing artifacts into crypto/lib/ and crypto/include/
crypto/libs/$(OS_TYPE)/$(ARCH)/lib/libsodium.a:
mkdir -p crypto/copies/$(OS_TYPE)/$(ARCH)
cp -R crypto/libsodium-fork crypto/copies/$(OS_TYPE)/$(ARCH)/libsodium-fork
cp -R crypto/libsodium-fork/. crypto/copies/$(OS_TYPE)/$(ARCH)/libsodium-fork
cd crypto/copies/$(OS_TYPE)/$(ARCH)/libsodium-fork && \
./autogen.sh --prefix $(SRCPATH)/crypto/libs/$(OS_TYPE)/$(ARCH) && \
./configure --disable-shared --prefix="$(SRCPATH)/crypto/libs/$(OS_TYPE)/$(ARCH)" && \
Expand Down
45 changes: 45 additions & 0 deletions data/ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ package data
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"sync"
"testing"

Expand Down Expand Up @@ -625,10 +628,52 @@ func TestLedgerErrorValidate(t *testing.T) {
for more {
select {
case err := <-errChan:
if strings.Contains(err.Error(), "before dbRound") {
// handle race eval errors like "round 1933 before dbRound 1934"
// see explanation in unexpectedMessages
re := regexp.MustCompile(`round (\d+) before dbRound (\d+)`)
result := re.FindStringSubmatch(err.Error())
require.NotNil(t, result)
require.Len(t, result, 3)
evalRound, err1 := strconv.Atoi(result[1])
require.NoError(t, err1)
dbRound, err1 := strconv.Atoi(result[2])
require.NoError(t, err1)
require.GreaterOrEqual(t, int(l.Latest()), dbRound+int(cfg.MaxAcctLookback))
require.Less(t, evalRound, dbRound)
err = nil
}
require.NoError(t, err)
case <-expectedMessages:
// only debug messages should be reported
case um := <-unexpectedMessages:
if strings.Contains(um, "before dbRound") {
// EnsureBlock might log the following:
// data.EnsureBlock: could not write block 774 to the ledger: round 773 before dbRound 774
// it happens because of simultaneous EnsureValidatedBlock and EnsureBlock calls
// that pass round check and then EnsureBlock yields after StartEvaluator.
// Meanwhile EnsureValidatedBlock finishes and adds the block to the ledger.
// After that trackersDB commit happen and account data get flushed.
// The EnsureBlock goroutine then tries to evaluate a first transaction and fails because
// the trackerDB advanced further.
// This is okay to ignore if
// - attempted round is less or equal than dbRound
// - ledger latest round is greater than dbRound + cfg.MaxAcctLookback
re := regexp.MustCompile(`could not write block (\d+) to the ledger: round (\d+) before dbRound (\d+)`)
result := re.FindStringSubmatch(um)
require.NotNil(t, result)
require.Len(t, result, 4)
attemptedRound, err := strconv.Atoi(result[1])
require.NoError(t, err)
evalRound, err := strconv.Atoi(result[2])
require.NoError(t, err)
dbRound, err := strconv.Atoi(result[3])
require.NoError(t, err)
require.Equal(t, attemptedRound, evalRound+1)
require.LessOrEqual(t, attemptedRound, dbRound)
require.GreaterOrEqual(t, int(l.Latest()), dbRound+int(cfg.MaxAcctLookback))
um = ""
}
require.Empty(t, um, um)
default:
more = false
Expand Down
16 changes: 14 additions & 2 deletions tools/network/dnssec/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,26 @@ func TestSrvSort(t *testing.T) {
arr := make([]*net.SRV, 0, 7)
arr = append(arr, &net.SRV{Priority: 4, Weight: 1})
arr = append(arr, &net.SRV{Priority: 3, Weight: 1})
arr = append(arr, &net.SRV{Priority: 1, Weight: 200})
arr = append(arr, &net.SRV{Priority: 1, Weight: 0xFFFF}) // max possible value to increase the ordering probability
arr = append(arr, &net.SRV{Priority: 1, Weight: 1})
arr = append(arr, &net.SRV{Priority: 1, Weight: 1})
arr = append(arr, &net.SRV{Priority: 1, Weight: 1})
arr = append(arr, &net.SRV{Priority: 1, Weight: 1})

retryCounter := 0
retry:
srvRecArray(arr).sortAndRand()
a.Equal(net.SRV{Priority: 1, Weight: 200}, *arr[0])
if (*arr[0] != net.SRV{Priority: 1, Weight: 0xFFFF}) {
// there is a small chance that a random number from 0 to max uint15 would be 0 or 1
// in this case the first element of the resulting sequence would be with weight of 1 and not the highest possible.
// if this happens, we will try again since it is expected time to time.
if retryCounter > 1 {
a.Fail("The first element of the resulting sequence should be with the highest possible weight at least in one of 3 attempts")
}
retryCounter++
goto retry
}
a.Equal(net.SRV{Priority: 1, Weight: 0xFFFF}, *arr[0])
a.Equal(net.SRV{Priority: 1, Weight: 1}, *arr[1])
a.Equal(net.SRV{Priority: 1, Weight: 1}, *arr[2])
a.Equal(net.SRV{Priority: 1, Weight: 1}, *arr[3])
Expand Down

0 comments on commit f5ad6a5

Please sign in to comment.