Skip to content

Commit a101573

Browse files
authored
Merge pull request #1651 from lightninglabs/itest-robust-AssertBalanceByID
itest: convert AssertBalanceByID to eventually-style call
2 parents fa97f8f + 0676ae8 commit a101573

File tree

6 files changed

+58
-19
lines changed

6 files changed

+58
-19
lines changed

.github/workflows/main.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,7 @@ jobs:
321321
go-version: '${{ env.GO_VERSION }}'
322322

323323
- name: run itest
324-
run: make itest-parallel dbbackend=postgres
324+
run: make itest-parallel dbbackend=postgres tranches=4
325325

326326
- name: Zip log files on failure
327327
if: ${{ failure() }}

asset/asset.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,11 @@ type Witness struct {
737737
// the witness should be encoded or not.
738738
func (w *Witness) encodeRecords(encodeType EncodeType) []tlv.Record {
739739
var records []tlv.Record
740+
741+
if w == nil {
742+
return records
743+
}
744+
740745
if w.PrevID != nil {
741746
records = append(records, NewWitnessPrevIDRecord(&w.PrevID))
742747
}
@@ -771,6 +776,10 @@ func (w *Witness) DecodeRecords() []tlv.Record {
771776

772777
// Encode encodes an asset witness into a TLV stream.
773778
func (w *Witness) Encode(writer io.Writer) error {
779+
if w == nil {
780+
return fmt.Errorf("cannot encode nil witness")
781+
}
782+
774783
stream, err := tlv.NewStream(w.EncodeRecords()...)
775784
if err != nil {
776785
return err

asset/records.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,10 @@ func NewWitnessPrevIDRecord(prevID **PrevID) tlv.Record {
287287

288288
func NewWitnessTxWitnessRecord(witness *wire.TxWitness) tlv.Record {
289289
recordSize := func() uint64 {
290+
if witness == nil {
291+
return 0
292+
}
293+
290294
return uint64((*witness).SerializeSize())
291295
}
292296
return tlv.MakeDynamicRecord(

itest/addrs_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,15 @@ func sendAsset(t *harnessTest, sender *tapdHarness,
10131013
options.sendAssetRequest.SkipProofCourierPingCheck = true
10141014
}
10151015

1016+
// The SubscribeSendEvents call returns immediately, but the server may
1017+
// still be setting up its event stream. If we emit an event too soon,
1018+
// it may be lost. We wait briefly to allow the server time to complete
1019+
// setup.
1020+
//
1021+
// TODO(ffranr): Remove this once the server supports buffering or
1022+
// replaying early events for new subscribers.
1023+
time.Sleep(time.Second)
1024+
10161025
// Kick off the send asset request.
10171026
resp, err := sender.SendAsset(ctxt, &options.sendAssetRequest)
10181027
if options.errText != "" {

itest/assertions.go

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,26 +1362,43 @@ func AssertAssetGenesis(t *testing.T, expected, actual *taprpc.GenesisInfo) {
13621362
func AssertBalanceByID(t *testing.T, client taprpc.TaprootAssetsClient,
13631363
id []byte, amt uint64) {
13641364

1365-
ctxb := context.Background()
1366-
balancesResp, err := client.ListBalances(
1367-
ctxb, &taprpc.ListBalancesRequest{
1368-
GroupBy: &taprpc.ListBalancesRequest_AssetId{
1369-
AssetId: true,
1365+
require.Eventually(t, func() bool {
1366+
ctxb := context.Background()
1367+
balancesResp, err := client.ListBalances(
1368+
ctxb, &taprpc.ListBalancesRequest{
1369+
GroupBy: &taprpc.ListBalancesRequest_AssetId{
1370+
AssetId: true,
1371+
},
1372+
AssetFilter: id,
1373+
ScriptKeyType: allScriptKeysQuery,
13701374
},
1371-
AssetFilter: id,
1372-
ScriptKeyType: allScriptKeysQuery,
1373-
},
1374-
)
1375-
require.NoError(t, err)
1375+
)
1376+
if err != nil {
1377+
t.Logf("error listing balances: %v", err)
1378+
return false
1379+
}
13761380

1377-
balance, ok := balancesResp.AssetBalances[hex.EncodeToString(id)]
1378-
if amt == 0 {
1379-
require.False(t, ok)
1380-
return
1381-
}
1381+
// nolint: lll
1382+
balance, ok := balancesResp.AssetBalances[hex.EncodeToString(id)]
1383+
if amt == 0 {
1384+
require.False(t, ok)
1385+
return true
1386+
}
13821387

1383-
require.True(t, ok)
1384-
require.Equal(t, amt, balance.Balance)
1388+
if !ok {
1389+
t.Logf("balance for asset not found (asset_id=%x)", id)
1390+
return false
1391+
}
1392+
1393+
if balance.Balance != amt {
1394+
t.Logf("balance mismatch for asset (asset_id=%x, "+
1395+
"expected=%d, actual=%d)", id, amt,
1396+
balance.Balance)
1397+
return false
1398+
}
1399+
1400+
return true
1401+
}, defaultWaitTimeout, time.Second)
13851402
}
13861403

13871404
// AssertBalanceByGroup asserts that the balance of a single asset group

itest/test_list_on_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ var allTestCases = []*testCase{
290290
test: testGetInfo,
291291
},
292292
{
293-
name: "burn test",
293+
name: "burn assets",
294294
test: testBurnAssets,
295295
},
296296
{

0 commit comments

Comments
 (0)