Skip to content

Commit

Permalink
Fix testing.Eventually cases to not fail the test if an assertion fails
Browse files Browse the repository at this point in the history
  • Loading branch information
muXxer committed Mar 28, 2024
1 parent 2d7ab67 commit 4a27031
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func TestSpendParallel(t *testing.T) {
parallelPendingTasks.WaitIsZero()

lo.ForEach(lo.Keys(parallelSpenders), func(SpendAlias string) {
assert.EqualValuesf(t, sequentialSpenders[SpendAlias].PreferredInstead().ID, parallelSpenders[SpendAlias].PreferredInstead().ID, "parallel Spend %s prefers %s, but sequential Spend prefers %s", SpendAlias, parallelSpenders[SpendAlias].PreferredInstead().ID, sequentialSpenders[SpendAlias].PreferredInstead().ID)
require.EqualValuesf(t, sequentialSpenders[SpendAlias].PreferredInstead().ID, parallelSpenders[SpendAlias].PreferredInstead().ID, "parallel Spend %s prefers %s, but sequential Spend prefers %s", SpendAlias, parallelSpenders[SpendAlias].PreferredInstead().ID, sequentialSpenders[SpendAlias].PreferredInstead().ID)
})

assertCorrectOrder(t, lo.Values(sequentialSpenders)...)
Expand Down
4 changes: 3 additions & 1 deletion pkg/testsuite/attestations.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ func (t *TestSuite) AssertAttestationsForSlot(slot iotago.SlotIndex, blocks []*b
//nolint:revive
err = attestationTree.Stream(func(key iotago.AccountID, att *iotago.Attestation) error {
blockID, err := att.BlockID()
require.NoError(t.Testing, err)
if err != nil {
return ierrors.Wrapf(err, "failed to stream attestationTree: %s, slot: %d", node.Name, slot)
}
storedAttestations = append(storedAttestations, blockID)

return nil
Expand Down
9 changes: 5 additions & 4 deletions pkg/testsuite/storage_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package testsuite
import (
"context"

"github.com/stretchr/testify/require"

"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/iota-core/pkg/testsuite/mock"
iotago "github.com/iotaledger/iota.go/v4"
Expand Down Expand Up @@ -56,14 +54,17 @@ func (t *TestSuite) AssertCommitmentSlotIndexExists(slot iotago.SlotIndex, clien
for _, client := range clients {
t.Eventually(func() error {
latestCommitment, err := client.CommitmentByID(context.Background(), iotago.EmptyCommitmentID)
require.NoError(t.Testing, err)
if err != nil {
return ierrors.Errorf("AssertCommitmentSlotIndexExists: %s: error loading latest commitment: %w", client.Name(), err)
}

if latestCommitment.Slot < slot {
return ierrors.Errorf("AssertCommitmentSlotIndexExists: %s: commitment with at least %v not found in settings.LatestCommitment()", client.Name(), slot)
}

cm, err := client.CommitmentBySlot(context.Background(), slot)
if err != nil {
return ierrors.Errorf("AssertCommitmentSlotIndexExists: %s: expected %v, got error %v", client.Name(), slot, err)
return ierrors.Errorf("AssertCommitmentSlotIndexExists: %s: expected %v, got error %w", client.Name(), slot, err)
}

if cm == nil {
Expand Down
9 changes: 6 additions & 3 deletions pkg/testsuite/sybilprotection.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package testsuite

import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/iotaledger/hive.go/ierrors"
"github.com/iotaledger/hive.go/lo"
Expand Down Expand Up @@ -89,7 +88,9 @@ func (t *TestSuite) AssertSybilProtectionRegisteredValidators(epoch iotago.Epoch
candidateIDs := lo.Map(candidates, func(candidate *api.ValidatorResponse) string {
return candidate.AddressBech32
})
require.NoError(t.Testing, err)
if err != nil {
return ierrors.Wrapf(err, "AssertSybilProtectionRegisteredValidators: %s: failed to get registered validators in epoch %d", node.Name, epoch)
}

if !assert.ElementsMatch(t.fakeTesting, expectedAccounts, candidateIDs) {
return ierrors.Errorf("AssertSybilProtectionRegisteredValidators: %s: expected %s, got %s", node.Name, expectedAccounts, candidateIDs)
Expand All @@ -113,7 +114,9 @@ func (t *TestSuite) AssertSybilProtectionCandidates(epoch iotago.EpochIndex, exp
candidateIDs := lo.Map(candidates, func(candidate *accounts.AccountData) iotago.AccountID {
return candidate.ID
})
require.NoError(t.Testing, err)
if err != nil {
return ierrors.Wrapf(err, "AssertSybilProtectionCandidates: %s: failed to get eligible validators in epoch %d", node.Name, epoch)
}

if !assert.ElementsMatch(t.fakeTesting, expectedAccounts, candidateIDs) {
return ierrors.Errorf("AssertSybilProtectionCandidates: %s: expected %s, got %s", node.Name, expectedAccounts, candidateIDs)
Expand Down
3 changes: 2 additions & 1 deletion pkg/testsuite/testsuite.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ func WithWalletBlockIssuanceCredits(blockIssuanceCredits iotago.BlockIssuanceCre
}

type TestSuite struct {
Testing *testing.T
Testing *testing.T
// we use the fake testing so that actual tests don't fail if an assertion fails
fakeTesting *testing.T
network *mock.Network

Expand Down
2 changes: 0 additions & 2 deletions pkg/testsuite/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ func (t *TestSuite) AssertTransaction(transaction *iotago.Transaction, node *moc
return ierrors.Errorf("AssertTransaction: %s: expected Transaction type %T, got %T", node.Name, transaction, loadedTransactionMetadata.Transaction())
}

// TODO: fix this in another PR
// if !assert.Equal(t.fakeTesting, transaction.Outputs, typedTransaction.Outputs) {
api := t.DefaultWallet().Client.APIForSlot(transactionID.Slot())
expected, _ := api.Encode(transaction.Outputs)
actual, _ := api.Encode(typedTransaction.Outputs)
Expand Down
15 changes: 12 additions & 3 deletions tools/docker-network/tests/dockerframework.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ func (n *Node) AccountAddress(t *testing.T) *iotago.AccountAddress {

type DockerTestFramework struct {
Testing *testing.T
// we use the fake testing so that actual tests don't fail if an assertion fails
fakeTesting *testing.T

nodes map[string]*Node
nodesLock syncutils.RWMutex
Expand All @@ -88,6 +90,7 @@ type DockerTestFramework struct {
func NewDockerTestFramework(t *testing.T, opts ...options.Option[DockerTestFramework]) *DockerTestFramework {
return options.Apply(&DockerTestFramework{
Testing: t,
fakeTesting: &testing.T{},
nodes: make(map[string]*Node),
wallet: NewDockerWallet(t),
optsWaitForSync: 5 * time.Minute,
Expand Down Expand Up @@ -223,13 +226,19 @@ func (d *DockerTestFramework) WaitUntilFaucetHealthy() {

d.Eventually(func() error {
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, d.optsFaucetURL+"/health", nil)
require.NoError(d.Testing, err)
if err != nil {
return err
}

res, err := http.DefaultClient.Do(req)
require.NoError(d.Testing, err)
if err != nil {
return err
}
defer res.Body.Close()

require.Equal(d.Testing, http.StatusOK, res.StatusCode)
if res.StatusCode != http.StatusOK {
return ierrors.Errorf("faucet is not healthy, status code: %d", res.StatusCode)
}

return nil
}, true)
Expand Down
5 changes: 3 additions & 2 deletions tools/docker-network/tests/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"strconv"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/iotaledger/hive.go/ierrors"
Expand Down Expand Up @@ -70,8 +71,8 @@ func (d *DockerTestFramework) AssertIndexerAccount(account *mock.AccountData) {
return err
}

require.EqualValues(d.Testing, account.OutputID, *outputID)
require.EqualValues(d.Testing, account.Output, output)
assert.EqualValues(d.fakeTesting, account.OutputID, *outputID)
assert.EqualValues(d.fakeTesting, account.Output, output)

return nil
})
Expand Down

0 comments on commit 4a27031

Please sign in to comment.