Skip to content

Commit 1e496bd

Browse files
authored
Merge pull request #341 from perun-network/concurrent-client-test
Client tests: fail on error
2 parents 5d89bb3 + 4a71c1b commit 1e496bd

18 files changed

+174
-163
lines changed

client/appchannel_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"math/big"
2020
"testing"
2121

22-
"github.com/stretchr/testify/assert"
2322
"perun.network/go-perun/channel"
2423
chtest "perun.network/go-perun/channel/test"
2524
"perun.network/go-perun/client"
@@ -32,7 +31,7 @@ import (
3231
func TestProgression(t *testing.T) {
3332
rng := pkgtest.Prng(t)
3433

35-
setups := NewSetups(rng, []string{"Paul", "Paula"})
34+
setups, errs := NewSetups(rng, []string{"Paul", "Paula"})
3635
roles := [2]clienttest.Executer{
3736
clienttest.NewPaul(t, setups[0]),
3837
clienttest.NewPaula(t, setups[1]),
@@ -53,6 +52,5 @@ func TestProgression(t *testing.T) {
5352

5453
ctx, cancel := context.WithTimeout(context.Background(), twoPartyTestTimeout)
5554
defer cancel()
56-
err := clienttest.ExecuteTwoPartyTest(ctx, roles, execConfig)
57-
assert.NoError(t, err)
55+
clienttest.ExecuteTwoPartyTest(ctx, t, roles, execConfig, errs)
5856
}

client/client_persistence_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ func TestPersistencePetraRobert(t *testing.T) {
2727
ctx, cancel := context.WithTimeout(context.Background(), twoPartyTestTimeout)
2828
defer cancel()
2929

30-
runAliceBobTest(ctx, t, func(rng *rand.Rand) (setups []ctest.RoleSetup, roles [2]ctest.Executer) {
31-
setups = NewSetupsPersistence(t, rng, []string{"Petra", "Robert"})
30+
runAliceBobTest(ctx, t, func(rng *rand.Rand) (setups []ctest.RoleSetup, roles [2]ctest.Executer, errs chan error) {
31+
setups, errs = NewSetupsPersistence(t, rng, []string{"Petra", "Robert"})
3232
roles = [2]ctest.Executer{
3333
ctest.NewPetra(t, setups[0]),
3434
ctest.NewRobert(t, setups[1]),
@@ -37,11 +37,11 @@ func TestPersistencePetraRobert(t *testing.T) {
3737
})
3838
}
3939

40-
func NewSetupsPersistence(t *testing.T, rng *rand.Rand, names []string) []ctest.RoleSetup {
40+
func NewSetupsPersistence(t *testing.T, rng *rand.Rand, names []string) ([]ctest.RoleSetup, chan error) {
4141
t.Helper()
42-
setups := NewSetups(rng, names)
42+
setups, errs := NewSetups(rng, names)
4343
for i := range names {
4444
setups[i].PR = chprtest.NewPersistRestorer(t)
4545
}
46-
return setups
46+
return setups, errs
4747
}

client/client_role_test.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,13 @@ const (
3939
twoPartyTestTimeout = 10 * time.Second
4040
)
4141

42-
func NewSetups(rng *rand.Rand, names []string) []ctest.RoleSetup {
42+
func NewSetups(rng *rand.Rand, names []string) ([]ctest.RoleSetup, chan error) {
4343
var (
4444
bus = wiretest.NewSerializingLocalBus()
4545
n = len(names)
4646
setup = make([]ctest.RoleSetup, n)
4747
backend = ctest.NewMockBackend(rng, "1337")
48+
errs = make(chan error)
4849
)
4950

5051
for i := 0; i < n; i++ {
@@ -63,20 +64,21 @@ func NewSetups(rng *rand.Rand, names []string) []ctest.RoleSetup {
6364
Timeout: roleOperationTimeout,
6465
BalanceReader: backend,
6566
ChallengeDuration: 60,
67+
Errors: errs,
6668
}
6769
}
6870

69-
return setup
71+
return setup, errs
7072
}
7173

7274
type Client struct {
7375
*client.Client
7476
ctest.RoleSetup
7577
}
7678

77-
func NewClients(t *testing.T, rng *rand.Rand, names []string) []*Client {
79+
func NewClients(t *testing.T, rng *rand.Rand, names []string) ([]*Client, chan error) {
7880
t.Helper()
79-
setups := NewSetups(rng, names)
81+
setups, errs := NewSetups(rng, names)
8082
clients := make([]*Client, len(setups))
8183
for i, setup := range setups {
8284
setup.Identity = setup.Wallet.NewRandomAccount(rng)
@@ -87,14 +89,14 @@ func NewClients(t *testing.T, rng *rand.Rand, names []string) []*Client {
8789
RoleSetup: setup,
8890
}
8991
}
90-
return clients
92+
return clients, errs
9193
}
9294

93-
func runAliceBobTest(ctx context.Context, t *testing.T, setup func(*rand.Rand) ([]ctest.RoleSetup, [2]ctest.Executer)) {
95+
func runAliceBobTest(ctx context.Context, t *testing.T, setup func(*rand.Rand) ([]ctest.RoleSetup, [2]ctest.Executer, chan error)) {
9496
t.Helper()
9597
rng := test.Prng(t)
9698
for i := 0; i < 2; i++ {
97-
setups, roles := setup(rng)
99+
setups, roles, errs := setup(rng)
98100
app := client.WithoutApp()
99101
if i == 1 {
100102
app = client.WithApp(
@@ -113,7 +115,6 @@ func runAliceBobTest(ctx context.Context, t *testing.T, setup func(*rand.Rand) (
113115
TxAmounts: [2]*big.Int{big.NewInt(5), big.NewInt(3)},
114116
}
115117

116-
err := ctest.ExecuteTwoPartyTest(ctx, roles, cfg)
117-
assert.NoError(t, err)
118+
ctest.ExecuteTwoPartyTest(ctx, t, roles, cfg, errs)
118119
}
119120
}

client/dispute_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"math/big"
2020
"testing"
2121

22-
"github.com/stretchr/testify/assert"
2322
chtest "perun.network/go-perun/channel/test"
2423
"perun.network/go-perun/client"
2524
ctest "perun.network/go-perun/client/test"
@@ -33,7 +32,7 @@ func TestDispute(t *testing.T) {
3332
defer cancel()
3433

3534
const mallory, carol = 0, 1 // Indices of Mallory and Carol
36-
setups := NewSetups(rng, []string{"Mallory", "Carol"})
35+
setups, errs := NewSetups(rng, []string{"Mallory", "Carol"})
3736
roles := [2]ctest.Executer{
3837
ctest.NewMallory(t, setups[0]),
3938
ctest.NewCarol(t, setups[1]),
@@ -49,6 +48,5 @@ func TestDispute(t *testing.T) {
4948
NumPayments: [2]int{5, 0},
5049
TxAmounts: [2]*big.Int{big.NewInt(20), big.NewInt(0)},
5150
}
52-
err := ctest.ExecuteTwoPartyTest(ctx, roles, cfg)
53-
assert.NoError(t, err)
51+
ctest.ExecuteTwoPartyTest(ctx, t, roles, cfg, errs)
5452
}

client/happy_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ func TestHappyAliceBob(t *testing.T) {
2626
ctx, cancel := context.WithTimeout(context.Background(), twoPartyTestTimeout)
2727
defer cancel()
2828

29-
runAliceBobTest(ctx, t, func(rng *rand.Rand) (setups []ctest.RoleSetup, roles [2]ctest.Executer) {
30-
setups = NewSetups(rng, []string{"Alice", "Bob"})
29+
runAliceBobTest(ctx, t, func(rng *rand.Rand) (setups []ctest.RoleSetup, roles [2]ctest.Executer, errs chan error) {
30+
setups, errs = NewSetups(rng, []string{"Alice", "Bob"})
3131
roles = [2]ctest.Executer{
3232
ctest.NewAlice(t, setups[0]),
3333
ctest.NewBob(t, setups[1]),

client/subchannel_dispute_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"math/big"
2020
"testing"
2121

22-
"github.com/stretchr/testify/assert"
2322
chtest "perun.network/go-perun/channel/test"
2423
"perun.network/go-perun/client"
2524
ctest "perun.network/go-perun/client/test"
@@ -30,7 +29,7 @@ import (
3029
func TestSubChannelDispute(t *testing.T) {
3130
rng := test.Prng(t)
3231

33-
setups := NewSetups(rng, []string{"DisputeSusie", "DisputeTim"})
32+
setups, errs := NewSetups(rng, []string{"DisputeSusie", "DisputeTim"})
3433
roles := [2]ctest.Executer{
3534
ctest.NewDisputeSusie(t, setups[0]),
3635
ctest.NewDisputeTim(t, setups[1]),
@@ -50,5 +49,5 @@ func TestSubChannelDispute(t *testing.T) {
5049

5150
ctx, cancel := context.WithTimeout(context.Background(), twoPartyTestTimeout)
5251
defer cancel()
53-
assert.NoError(t, ctest.ExecuteTwoPartyTest(ctx, roles, cfg))
52+
ctest.ExecuteTwoPartyTest(ctx, t, roles, cfg, errs)
5453
}

client/subchannel_happy_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"math/big"
2020
"testing"
2121

22-
"github.com/stretchr/testify/assert"
2322
"perun.network/go-perun/apps/payment"
2423
chtest "perun.network/go-perun/channel/test"
2524
"perun.network/go-perun/client"
@@ -31,7 +30,7 @@ import (
3130
func TestSubChannelHappy(t *testing.T) {
3231
rng := test.Prng(t)
3332

34-
setups := NewSetups(rng, []string{"Susie", "Tim"})
33+
setups, errs := NewSetups(rng, []string{"Susie", "Tim"})
3534
roles := [2]ctest.Executer{
3635
ctest.NewSusie(t, setups[0]),
3736
ctest.NewTim(t, setups[1]),
@@ -63,5 +62,5 @@ func TestSubChannelHappy(t *testing.T) {
6362

6463
ctx, cancel := context.WithTimeout(context.Background(), twoPartyTestTimeout)
6564
defer cancel()
66-
assert.NoError(t, ctest.ExecuteTwoPartyTest(ctx, roles, cfg))
65+
ctest.ExecuteTwoPartyTest(ctx, t, roles, cfg, errs)
6766
}

client/test/carol.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ import (
1818
"fmt"
1919
"testing"
2020

21-
"github.com/stretchr/testify/assert"
22-
2321
"perun.network/go-perun/channel"
2422
)
2523

@@ -53,7 +51,6 @@ func (r *Carol) Execute(cfg ExecConfig) {
5351

5452
func (r *Carol) exec(_cfg ExecConfig, ch *paymentChannel, propHandler *acceptNextPropHandler) {
5553
cfg := _cfg.(*MalloryCarolExecConfig)
56-
assert := assert.New(r.t)
5754
_, them := r.Idxs(cfg.Peers())
5855

5956
// start watcher
@@ -82,7 +79,7 @@ func (r *Carol) exec(_cfg ExecConfig, ch *paymentChannel, propHandler *acceptNex
8279
r.log.Debugf("watcher refuted with the version: %v", e.Version())
8380

8481
r.log.Debug("Waiting until ready to conclude")
85-
assert.NoError(e.Timeout().Wait(r.Ctx())) // wait until ready to conclude
82+
r.RequireNoError(e.Timeout().Wait(r.Ctx())) // wait until ready to conclude
8683

8784
r.log.Debug("Settle")
8885
ch.settle() // conclude and withdraw

client/test/channel.go

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ package test
1616

1717
import (
1818
"context"
19+
"errors"
1920
"io"
2021
"math/big"
2122
"time"
2223

23-
"github.com/stretchr/testify/assert"
24-
2524
"perun.network/go-perun/channel"
2625
"perun.network/go-perun/client"
2726
"perun.network/go-perun/log"
@@ -73,7 +72,7 @@ func (ch *paymentChannel) openSubChannel(
7372

7473
prop := ch.r.SubChannelProposal(rng, cfg, ch.Channel, &initAlloc, app)
7574
subchannel, err := ch.r.ProposeChannel(prop)
76-
assert.NoError(ch.r.t, err)
75+
ch.r.RequireNoError(err)
7776
ch.r.log.Infof("New subchannel opened: %v", subchannel.Channel)
7877

7978
for i, bal := range initBals {
@@ -88,7 +87,7 @@ func (ch *paymentChannel) acceptSubchannel(
8887
initBals []*big.Int,
8988
) *paymentChannel {
9089
subchannel, err := propHandler.Next()
91-
assert.NoError(ch.r.t, err)
90+
ch.r.RequireNoError(err)
9291
ch.r.log.Infof("New subchannel opened: %v", subchannel.Channel)
9392

9493
for i, bal := range initBals {
@@ -105,7 +104,7 @@ func (ch *paymentChannel) sendUpdate(updater func(*channel.State), desc string)
105104

106105
err := ch.Update(ctx, updater)
107106
ch.log.Infof("Sent update: %s, err: %v", desc, err)
108-
assert.NoError(ch.r.t, err)
107+
ch.r.RequireNoError(err)
109108
}
110109

111110
func (ch *paymentChannel) sendTransfer(amount channel.Bal, desc string) {
@@ -127,10 +126,11 @@ func (ch *paymentChannel) recvUpdate(accept bool, desc string) *channel.State {
127126
select {
128127
case res := <-ch.res:
129128
ch.log.Infof("Received update: %s, err: %v", desc, res.err)
130-
assert.NoError(ch.r.t, res.err)
129+
ch.r.RequireNoError(res.err)
131130
return res.up.State
132131
case <-time.After(ch.r.timeout):
133-
ch.r.t.Error("timeout: expected incoming channel update")
132+
err := errors.New("timeout: expected incoming channel update")
133+
ch.r.RequireNoError(err)
134134
return nil
135135
}
136136
}
@@ -150,22 +150,21 @@ func (ch *paymentChannel) assertBals(state *channel.State) {
150150
ch.bals[0], ch.bals[1],
151151
bals[0], bals[1],
152152
)
153-
assert := assert.New(ch.r.t)
154-
assert.Zerof(bals[0].Cmp(ch.bals[0]), "bal[0]: %v != %v", bals[0], ch.bals[0])
155-
assert.Zerof(bals[1].Cmp(ch.bals[1]), "bal[1]: %v != %v", bals[1], ch.bals[1])
153+
ch.r.RequireTruef(bals[0].Cmp(ch.bals[0]) == 0, "bal[0]: %v != %v", bals[0], ch.bals[0])
154+
ch.r.RequireTruef(bals[1].Cmp(ch.bals[1]) == 0, "bal[1]: %v != %v", bals[1], ch.bals[1])
156155
}
157156

158157
func (ch *paymentChannel) sendFinal() {
159158
ch.sendUpdate(func(state *channel.State) {
160159
state.IsFinal = true
161160
}, "final")
162-
assert.True(ch.r.t, ch.State().IsFinal)
161+
ch.r.RequireTrue(ch.State().IsFinal)
163162
}
164163

165164
func (ch *paymentChannel) recvFinal() {
166165
state := ch.recvUpdate(true, "final")
167-
assert.NotNil(ch.r.t, state)
168-
assert.True(ch.r.t, state.IsFinal)
166+
ch.r.RequireTrue(state != nil)
167+
ch.r.RequireTrue(state.IsFinal)
169168
}
170169

171170
func (ch *paymentChannel) settle() {
@@ -177,18 +176,16 @@ func (ch *paymentChannel) settleSecondary() {
177176
}
178177

179178
func (ch *paymentChannel) settleImpl(secondary bool) {
180-
assert := assert.New(ch.r.t)
181-
182179
ctx, cancel := context.WithTimeout(context.Background(), ch.r.timeout)
183180
defer cancel()
184181

185-
assert.NoError(ch.Settle(ctx, secondary))
182+
ch.r.RequireNoError(ch.Settle(ctx, secondary))
186183
ch.assertBals(ch.State())
187184

188185
if ch.IsSubChannel() {
189186
// Track parent channel's balances.
190187
parentChannel, ok := ch.r.chans.channel(ch.Parent().ID())
191-
assert.True(ok, "parent channel not found")
188+
ch.r.RequireTruef(ok, "parent channel not found")
192189

193190
for i, bal := range ch.bals {
194191
parentBal := parentChannel.bals[i]

client/test/mallory.go

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ import (
2121
"testing"
2222
"time"
2323

24-
"github.com/stretchr/testify/assert"
25-
2624
"perun.network/go-perun/client"
2725
)
2826

@@ -53,7 +51,6 @@ func (r *Mallory) Execute(cfg ExecConfig) {
5351

5452
func (r *Mallory) exec(_cfg ExecConfig, ch *paymentChannel) {
5553
cfg := _cfg.(*MalloryCarolExecConfig)
56-
assert := assert.New(r.t)
5754
we, _ := r.Idxs(cfg.Peers())
5855
// AdjudicatorReq for version 0
5956
req0 := client.NewTestChannel(ch.Channel).AdjudicatorReq()
@@ -73,35 +70,35 @@ func (r *Mallory) exec(_cfg ExecConfig, ch *paymentChannel) {
7370
regCtx, regCancel := context.WithTimeout(context.Background(), r.timeout)
7471
defer regCancel()
7572
r.log.Debug("Registering version 0 state.")
76-
assert.NoError(r.setup.Adjudicator.Register(regCtx, req0, nil))
73+
r.RequireNoError(r.setup.Adjudicator.Register(regCtx, req0, nil))
7774

7875
// within the challenge duration, Carol should refute.
7976
subCtx, subCancel := context.WithTimeout(context.Background(), r.timeout+challengeDuration)
8077
defer subCancel()
8178
sub, err := r.setup.Adjudicator.Subscribe(subCtx, ch.Params().ID())
82-
assert.NoError(err)
79+
r.RequireNoError(err)
8380

8481
// 3rd stage - wait until Carol has refuted
8582
r.waitStage()
8683

8784
event := sub.Next() // should be event caused by Carol's refutation.
88-
assert.NotNil(event)
89-
assert.True(event.Timeout().IsElapsed(subCtx),
85+
r.RequireTrue(event != nil)
86+
r.RequireTruef(event.Timeout().IsElapsed(subCtx),
9087
"Carol's refutation should already have progressed past the timeout.")
9188

92-
assert.NoError(sub.Close())
93-
assert.NoError(sub.Err())
89+
r.RequireNoError(sub.Close())
90+
r.RequireNoError(sub.Err())
9491
r.log.Debugln("<Registered> refuted: ", event)
95-
assert.Equal(ch.State().Version, event.Version(), "expected refutation with current version")
92+
r.RequireTruef(ch.State().Version == event.Version(), "expected refutation with current version")
9693
waitCtx, waitCancel := context.WithTimeout(context.Background(), r.timeout+challengeDuration)
9794
defer waitCancel()
9895
// refutation increased the timeout.
99-
assert.NoError(event.Timeout().Wait(waitCtx))
96+
r.RequireNoError(event.Timeout().Wait(waitCtx))
10097

10198
wdCtx, wdCancel := context.WithTimeout(context.Background(), r.timeout)
10299
defer wdCancel()
103100
err = r.setup.Adjudicator.Withdraw(wdCtx, req0, nil)
104-
assert.Error(err, "withdrawing should fail because Carol should have refuted.")
101+
r.RequireTruef(err != nil, "withdrawing should fail because Carol should have refuted")
105102

106103
// settling current version should work
107104
ch.settle()

0 commit comments

Comments
 (0)