Skip to content

Commit f4ac224

Browse files
fix(fabricx): Fix flaky iou test
Signed-off-by: Marcus Brandenburger <bur@zurich.ibm.com>
1 parent a042a1d commit f4ac224

File tree

1 file changed

+37
-10
lines changed

1 file changed

+37
-10
lines changed

integration/fabricx/iou/commands_test.go

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,27 @@ SPDX-License-Identifier: Apache-2.0
77
package iou_test
88

99
import (
10+
"context"
11+
"time"
12+
1013
"github.com/hyperledger-labs/fabric-smart-client/integration"
1114
views2 "github.com/hyperledger-labs/fabric-smart-client/integration/fabric/common/views"
1215
"github.com/hyperledger-labs/fabric-smart-client/integration/fabric/iou/views"
1316
"github.com/hyperledger-labs/fabric-smart-client/integration/nwo/common"
1417
. "github.com/onsi/gomega"
1518
)
1619

20+
const timeout = 2 * time.Minute
21+
1722
func CreateIOU(ii *integration.Infrastructure, identityLabel string, amount uint, approver string) (string, error) {
1823
return CreateIOUWithBorrower(ii, "borrower", identityLabel, amount, approver)
1924
}
2025

2126
func CreateIOUWithBorrower(ii *integration.Infrastructure, borrower, identityLabel string, amount uint, approver string) (string, error) {
22-
res, err := ii.Client(borrower).CallView(
27+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
28+
defer cancel()
29+
30+
res, err := ii.Client(borrower).CallViewWithContext(ctx,
2331
"create", common.JSONMarshall(&views.Create{
2432
Amount: amount,
2533
Identity: identityLabel,
@@ -36,39 +44,58 @@ func CreateIOUWithBorrower(ii *integration.Infrastructure, borrower, identityLab
3644
}
3745

3846
func CheckState(ii *integration.Infrastructure, partyID, iouStateID string, expected int) {
39-
res, err := ii.CLI(partyID).CallView("query", common.JSONMarshall(&views.Query{LinearID: iouStateID}))
47+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
48+
defer cancel()
49+
50+
res, err := ii.CLI(partyID).CallViewWithContext(ctx, "query", common.JSONMarshall(&views.Query{LinearID: iouStateID}))
4051
Expect(err).NotTo(HaveOccurred())
4152
Expect(common.JSONUnmarshalInt(res)).To(BeEquivalentTo(expected))
4253
}
4354

44-
func UpdateIOU(ii *integration.Infrastructure, iouStateID string, amount uint, approver string, errs ...string) {
45-
UpdateIOUWithBorrower(ii, "borrower", iouStateID, amount, approver, errs...)
55+
func UpdateIOU(ii *integration.Infrastructure, iouStateID string, amount uint, approver string, expectedErrs ...string) {
56+
UpdateIOUWithBorrower(ii, "borrower", iouStateID, amount, approver, expectedErrs...)
4657
}
4758

48-
func UpdateIOUWithBorrower(ii *integration.Infrastructure, borrower, iouStateID string, amount uint, approver string, errs ...string) {
49-
txIDBoxed, err := ii.Client(borrower).CallView("update",
59+
func UpdateIOUWithBorrower(ii *integration.Infrastructure, borrower, iouStateID string, amount uint, approver string, expectedErrs ...string) {
60+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
61+
defer cancel()
62+
63+
txIDBoxed, err := ii.Client(borrower).CallViewWithContext(ctx, "update",
5064
common.JSONMarshall(&views.Update{
5165
LinearID: iouStateID,
5266
Amount: amount,
5367
Approver: ii.Identity(approver),
5468
}),
5569
)
56-
if len(errs) > 0 {
70+
if len(expectedErrs) > 0 {
71+
// check that we got the expected errors (if defined) from the update view
5772
errStr := err.Error()
5873
Expect(err).To(HaveOccurred())
59-
for _, s := range errs {
74+
for _, s := range expectedErrs {
6075
Expect(errStr).To(ContainSubstring(s))
6176
}
6277
return
6378
}
6479

6580
Expect(err).NotTo(HaveOccurred())
81+
82+
// next check lender has observed the tx
6683
txID := common.JSONUnmarshalString(txIDBoxed)
67-
_, err = ii.Client("lender").CallView("finality", common.JSONMarshall(views2.Finality{TxID: txID}))
84+
CheckFinalityWithLender(ii, "lender", txID)
85+
}
86+
87+
func CheckFinalityWithLender(ii *integration.Infrastructure, lender string, txID string) {
88+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
89+
defer cancel()
90+
91+
_, err := ii.Client(lender).CallViewWithContext(ctx, "finality", common.JSONMarshall(views2.Finality{TxID: txID}))
6892
Expect(err).NotTo(HaveOccurred())
6993
}
7094

7195
func InitApprover(ii *integration.Infrastructure, approver string) {
72-
_, err := ii.Client(approver).CallView("init", nil)
96+
ctx, cancel := context.WithTimeout(context.Background(), timeout)
97+
defer cancel()
98+
99+
_, err := ii.Client(approver).CallViewWithContext(ctx, "init", nil)
73100
Expect(err).NotTo(HaveOccurred())
74101
}

0 commit comments

Comments
 (0)