From 829562d0bf4e2bf49032e8d51d1b5f16169160a8 Mon Sep 17 00:00:00 2001 From: Mark Rushakoff Date: Wed, 1 Mar 2023 11:00:47 -0500 Subject: [PATCH] test: fix flakiness due to duplicate denoms On CI, with an unrelated change, we observed TestGRPCQuerySpendableBalances failing with this message: deterministic_test.go:156: [rapid] panic after 26 tests: invalid coin set 1A..,1A..: duplicate denomination A.. This occurred at: https://github.com/cosmos/cosmos-sdk/actions/runs/4304610752/jobs/7505884172#step:5:42 As noted in the message, this was reproducible with passing -rapid.seed=17310147078983591311. Instead of several independent calls to producing a string with rapid, prefer rapid.SliceOfNDistinct in order to ensure that all denoms are unique, thereby avoiding the panic in sdk.NewCoins. --- tests/integration/bank/keeper/deterministic_test.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tests/integration/bank/keeper/deterministic_test.go b/tests/integration/bank/keeper/deterministic_test.go index 177006bdaeda..1f419eeaba22 100644 --- a/tests/integration/bank/keeper/deterministic_test.go +++ b/tests/integration/bank/keeper/deterministic_test.go @@ -155,12 +155,13 @@ func TestGRPCQuerySpendableBalances(t *testing.T) { rapid.Check(t, func(rt *rapid.T) { addr := testdata.AddressGenerator(rt).Draw(rt, "address") - numCoins := rapid.IntRange(1, 10).Draw(rt, "num-count") - coins := make(sdk.Coins, 0, numCoins) - for i := 0; i < numCoins; i++ { + // Denoms must be unique, otherwise sdk.NewCoins will panic. + denoms := rapid.SliceOfNDistinct(rapid.StringMatching(denomRegex), 1, 10, rapid.ID[string]).Draw(rt, "denoms") + coins := make(sdk.Coins, 0, len(denoms)) + for _, denom := range denoms { coin := sdk.NewCoin( - rapid.StringMatching(denomRegex).Draw(rt, "denom"), + denom, sdk.NewInt(rapid.Int64Min(1).Draw(rt, "amount")), ) @@ -171,7 +172,7 @@ func TestGRPCQuerySpendableBalances(t *testing.T) { err := banktestutil.FundAccount(f.bankKeeper, f.ctx, addr, coins) assert.NilError(t, err) - req := banktypes.NewQuerySpendableBalancesRequest(addr, testdata.PaginationGenerator(rt, uint64(numCoins)).Draw(rt, "pagination")) + req := banktypes.NewQuerySpendableBalancesRequest(addr, testdata.PaginationGenerator(rt, uint64(len(denoms))).Draw(rt, "pagination")) testdata.DeterministicIterations(f.ctx, t, req, f.queryClient.SpendableBalances, 0, true) })