Skip to content

Commit a354337

Browse files
committed
fix(itest): retry AssertBalances for 10 seconds
avoids race condition when a balance change is still in-progress
1 parent cf2a5e5 commit a354337

File tree

1 file changed

+47
-32
lines changed

1 file changed

+47
-32
lines changed

itest/assertions.go

Lines changed: 47 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"context"
66
"encoding/hex"
7+
"errors"
78
"fmt"
89
"reflect"
910
"sort"
@@ -2174,6 +2175,17 @@ func AssertBalances(t *testing.T, client taprpc.TaprootAssetsClient,
21742175

21752176
t.Helper()
21762177

2178+
err := wait.NoError(func() error {
2179+
return assertBalances(t, client, balance, opts...)
2180+
}, 10*time.Second)
2181+
if err != nil {
2182+
t.Fatal(err)
2183+
}
2184+
}
2185+
2186+
func assertBalances(t *testing.T, client taprpc.TaprootAssetsClient,
2187+
balance uint64, opts ...BalanceOption) error {
2188+
21772189
config := &balanceConfig{}
21782190
for _, opt := range opts {
21792191
opt(config)
@@ -2231,7 +2243,9 @@ func AssertBalances(t *testing.T, client taprpc.TaprootAssetsClient,
22312243
GroupKeyFilter: config.groupKey,
22322244
},
22332245
)
2234-
require.NoError(t, err)
2246+
if err != nil {
2247+
return err
2248+
}
22352249

22362250
// Spent assets (burns/tombstones) are never included in the balance,
22372251
// even if we specifically query for their type. So we skip the balance
@@ -2242,11 +2256,9 @@ func AssertBalances(t *testing.T, client taprpc.TaprootAssetsClient,
22422256
*config.scriptKeyType != asset.ScriptKeyTombstone)) &&
22432257
len(config.scriptKey) == 0
22442258
if checkBalance {
2245-
require.Equal(
2246-
t, balance, balanceSum(assetIDBalances, false),
2247-
"asset balance, wanted %d, got: %v", balance,
2248-
toJSON(t, assetIDBalances),
2249-
)
2259+
if balance != balanceSum(assetIDBalances, false) {
2260+
return fmt.Errorf("asset balance, wanted %d, got: %v", balance, toJSON(t, assetIDBalances))
2261+
}
22502262
}
22512263

22522264
// Next, we do the same but grouped by group keys (if requested, since
@@ -2261,18 +2273,17 @@ func AssertBalances(t *testing.T, client taprpc.TaprootAssetsClient,
22612273
GroupKeyFilter: config.groupKey,
22622274
},
22632275
)
2264-
require.NoError(t, err)
2276+
if err != nil {
2277+
return err
2278+
}
22652279

22662280
// Spent assets (burns/tombstones) are never included in the
22672281
// balance, even if we specifically query for their type. So we
22682282
// skip the balance check in that case.
22692283
if checkBalance {
2270-
require.Equalf(
2271-
t, config.groupedAssetBalance,
2272-
balanceSum(assetGroupBalances, true),
2273-
"grouped balance, wanted %d, got: %v", balance,
2274-
toJSON(t, assetGroupBalances),
2275-
)
2284+
if config.groupedAssetBalance != balanceSum(assetGroupBalances, true) {
2285+
return fmt.Errorf("grouped balance, wanted %d, got: %v", balance, toJSON(t, assetGroupBalances))
2286+
}
22762287
}
22772288
}
22782289

@@ -2282,7 +2293,9 @@ func AssertBalances(t *testing.T, client taprpc.TaprootAssetsClient,
22822293
IncludeLeased: config.includeLeased,
22832294
ScriptKeyType: rpcTypeQuery,
22842295
})
2285-
require.NoError(t, err)
2296+
if err != nil {
2297+
return err
2298+
}
22862299

22872300
var (
22882301
totalBalance uint64
@@ -2320,26 +2333,26 @@ func AssertBalances(t *testing.T, client taprpc.TaprootAssetsClient,
23202333
totalBalance += a.Amount
23212334
numUtxos++
23222335
}
2323-
require.Equalf(
2324-
t, balance, totalBalance, "ListAssets balance, wanted %d, "+
2325-
"got: %v", balance, toJSON(t, assetList),
2326-
)
2336+
if balance != totalBalance {
2337+
return fmt.Errorf("ListAssets balance, wanted %d, got: %v", balance, toJSON(t, assetList))
2338+
}
23272339

23282340
// The number of UTXOs means asset outputs in this case. We check the
23292341
// BTC-level UTXOs below as well, but that's just to make sure the
23302342
// output of that RPC lists the same assets.
23312343
if config.numAssetUtxos > 0 {
2332-
require.Equal(
2333-
t, config.numAssetUtxos, numUtxos, "ListAssets num "+
2334-
"asset utxos",
2335-
)
2344+
if config.numAssetUtxos != numUtxos {
2345+
return errors.New("ListAssets num asset utxos")
2346+
}
23362347
}
23372348

23382349
utxoList, err := client.ListUtxos(ctxt, &taprpc.ListUtxosRequest{
23392350
IncludeLeased: config.includeLeased,
23402351
ScriptKeyType: rpcTypeQuery,
23412352
})
2342-
require.NoError(t, err)
2353+
if err != nil {
2354+
return err
2355+
}
23432356

23442357
// Make sure the ListUtxos call returns the same number of (asset) UTXOs
23452358
// as the ListAssets call.
@@ -2386,20 +2399,22 @@ func AssertBalances(t *testing.T, client taprpc.TaprootAssetsClient,
23862399
numUtxos++
23872400
}
23882401
}
2389-
require.Equal(t, balance, totalBalance, "ListUtxos balance")
2402+
if balance != totalBalance {
2403+
return errors.New("ListUtxos balance")
2404+
}
23902405

23912406
if config.numAnchorUtxos > 0 {
2392-
require.Equal(
2393-
t, config.numAnchorUtxos, numAnchorUtxos, "num anchor "+
2394-
"utxos",
2395-
)
2407+
if config.numAnchorUtxos != numAnchorUtxos {
2408+
return errors.New("num anchor utxos")
2409+
}
23962410
}
23972411
if config.numAssetUtxos > 0 {
2398-
require.Equal(
2399-
t, config.numAssetUtxos, numUtxos, "ListUtxos num "+
2400-
"asset utxos",
2401-
)
2412+
if config.numAssetUtxos != numUtxos {
2413+
return errors.New("ListUtxos num asset utxos")
2414+
}
24022415
}
2416+
2417+
return nil
24032418
}
24042419

24052420
func assertGroups(t *testing.T, client taprpc.TaprootAssetsClient,

0 commit comments

Comments
 (0)