Skip to content

Commit

Permalink
Merge pull request hyperledger-labs#347 from perun-network/cleanup-cl…
Browse files Browse the repository at this point in the history
…ient-test-require

🎨 client/test: Reduce setup complexity
  • Loading branch information
matthiasgeihs authored May 30, 2022
2 parents 424a38b + fe57fc0 commit 35f8a87
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 30 deletions.
4 changes: 2 additions & 2 deletions client/appchannel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
func TestProgression(t *testing.T) {
rng := pkgtest.Prng(t)

setups, errs := NewSetups(rng, []string{"Paul", "Paula"})
setups := NewSetups(rng, []string{"Paul", "Paula"})
roles := [2]clienttest.Executer{
clienttest.NewPaul(t, setups[0]),
clienttest.NewPaula(t, setups[1]),
Expand All @@ -52,5 +52,5 @@ func TestProgression(t *testing.T) {

ctx, cancel := context.WithTimeout(context.Background(), twoPartyTestTimeout)
defer cancel()
clienttest.ExecuteTwoPartyTest(ctx, t, roles, execConfig, errs)
clienttest.ExecuteTwoPartyTest(ctx, t, roles, execConfig)
}
10 changes: 5 additions & 5 deletions client/client_persistence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ func TestPersistencePetraRobert(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), twoPartyTestTimeout)
defer cancel()

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

func NewSetupsPersistence(t *testing.T, rng *rand.Rand, names []string) ([]ctest.RoleSetup, chan error) {
func NewSetupsPersistence(t *testing.T, rng *rand.Rand, names []string) []ctest.RoleSetup {
t.Helper()
setups, errs := NewSetups(rng, names)
setups := NewSetups(rng, names)
for i := range names {
setups[i].PR = chprtest.NewPersistRestorer(t)
}
return setups, errs
return setups
}
19 changes: 9 additions & 10 deletions client/client_role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,12 @@ const (
twoPartyTestTimeout = 10 * time.Second
)

func NewSetups(rng *rand.Rand, names []string) ([]ctest.RoleSetup, chan error) {
func NewSetups(rng *rand.Rand, names []string) []ctest.RoleSetup {
var (
bus = wiretest.NewSerializingLocalBus()
n = len(names)
setup = make([]ctest.RoleSetup, n)
backend = ctest.NewMockBackend(rng, "1337")
errs = make(chan error)
)

for i := 0; i < n; i++ {
Expand All @@ -64,21 +63,21 @@ func NewSetups(rng *rand.Rand, names []string) ([]ctest.RoleSetup, chan error) {
Timeout: roleOperationTimeout,
BalanceReader: backend,
ChallengeDuration: 60,
Errors: errs,
Errors: make(chan error),
}
}

return setup, errs
return setup
}

type Client struct {
*client.Client
ctest.RoleSetup
}

func NewClients(t *testing.T, rng *rand.Rand, names []string) ([]*Client, chan error) {
func NewClients(t *testing.T, rng *rand.Rand, names []string) []*Client {
t.Helper()
setups, errs := NewSetups(rng, names)
setups := NewSetups(rng, names)
clients := make([]*Client, len(setups))
for i, setup := range setups {
setup.Identity = setup.Wallet.NewRandomAccount(rng)
Expand All @@ -89,14 +88,14 @@ func NewClients(t *testing.T, rng *rand.Rand, names []string) ([]*Client, chan e
RoleSetup: setup,
}
}
return clients, errs
return clients
}

func runAliceBobTest(ctx context.Context, t *testing.T, setup func(*rand.Rand) ([]ctest.RoleSetup, [2]ctest.Executer, chan error)) {
func runAliceBobTest(ctx context.Context, t *testing.T, setup func(*rand.Rand) ([]ctest.RoleSetup, [2]ctest.Executer)) {
t.Helper()
rng := test.Prng(t)
for i := 0; i < 2; i++ {
setups, roles, errs := setup(rng)
setups, roles := setup(rng)
app := client.WithoutApp()
if i == 1 {
app = client.WithApp(
Expand All @@ -115,6 +114,6 @@ func runAliceBobTest(ctx context.Context, t *testing.T, setup func(*rand.Rand) (
TxAmounts: [2]*big.Int{big.NewInt(5), big.NewInt(3)},
}

ctest.ExecuteTwoPartyTest(ctx, t, roles, cfg, errs)
ctest.ExecuteTwoPartyTest(ctx, t, roles, cfg)
}
}
4 changes: 2 additions & 2 deletions client/dispute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestDispute(t *testing.T) {
defer cancel()

const mallory, carol = 0, 1 // Indices of Mallory and Carol
setups, errs := NewSetups(rng, []string{"Mallory", "Carol"})
setups := NewSetups(rng, []string{"Mallory", "Carol"})
roles := [2]ctest.Executer{
ctest.NewMallory(t, setups[0]),
ctest.NewCarol(t, setups[1]),
Expand All @@ -48,5 +48,5 @@ func TestDispute(t *testing.T) {
NumPayments: [2]int{5, 0},
TxAmounts: [2]*big.Int{big.NewInt(20), big.NewInt(0)},
}
ctest.ExecuteTwoPartyTest(ctx, t, roles, cfg, errs)
ctest.ExecuteTwoPartyTest(ctx, t, roles, cfg)
}
8 changes: 4 additions & 4 deletions client/happy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func TestHappyAliceBob(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), twoPartyTestTimeout)
defer cancel()

runAliceBobTest(ctx, t, func(rng *rand.Rand) (setups []ctest.RoleSetup, roles [2]ctest.Executer, errs chan error) {
setups, errs = NewSetups(rng, []string{"Alice", "Bob"})
roles = [2]ctest.Executer{
runAliceBobTest(ctx, t, func(rng *rand.Rand) ([]ctest.RoleSetup, [2]ctest.Executer) {
setups := NewSetups(rng, []string{"Alice", "Bob"})
roles := [2]ctest.Executer{
ctest.NewAlice(t, setups[0]),
ctest.NewBob(t, setups[1]),
}
return
return setups, roles
})
}
4 changes: 2 additions & 2 deletions client/subchannel_dispute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
func TestSubChannelDispute(t *testing.T) {
rng := test.Prng(t)

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

ctx, cancel := context.WithTimeout(context.Background(), twoPartyTestTimeout)
defer cancel()
ctest.ExecuteTwoPartyTest(ctx, t, roles, cfg, errs)
ctest.ExecuteTwoPartyTest(ctx, t, roles, cfg)
}
4 changes: 2 additions & 2 deletions client/subchannel_happy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
func TestSubChannelHappy(t *testing.T) {
rng := test.Prng(t)

setups, errs := NewSetups(rng, []string{"Susie", "Tim"})
setups := NewSetups(rng, []string{"Susie", "Tim"})
roles := [2]ctest.Executer{
ctest.NewSusie(t, setups[0]),
ctest.NewTim(t, setups[1]),
Expand Down Expand Up @@ -62,5 +62,5 @@ func TestSubChannelHappy(t *testing.T) {

ctx, cancel := context.WithTimeout(context.Background(), twoPartyTestTimeout)
defer cancel()
ctest.ExecuteTwoPartyTest(ctx, t, roles, cfg, errs)
ctest.ExecuteTwoPartyTest(ctx, t, roles, cfg)
}
5 changes: 5 additions & 0 deletions client/test/persistence.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,11 @@ func (r *multiClientRole) assertPersistedPeerAndChannel(cfg ExecConfig, state *c
r.RequireNoError(restoredCh.CurrentTXV.State.Equal(state))
}

// Errors returns the error channel.
func (r *multiClientRole) Errors() <-chan error {
return r.errs
}

type addresses []wire.Address

func (a addresses) contains(b wire.Address) bool {
Expand Down
5 changes: 5 additions & 0 deletions client/test/proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ func (r *Proposer) Execute(cfg ExecConfig, exec func(ExecConfig, *paymentChannel

r.RequireNoError(ch.Close()) // May or may not already be closed due to channelConn closing.
}

// Errors returns the error channel.
func (r *Proposer) Errors() <-chan error {
return r.errs
}
5 changes: 5 additions & 0 deletions client/test/responder.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,8 @@ func (r *Responder) Execute(cfg ExecConfig, exec func(ExecConfig, *paymentChanne

r.RequireNoError(ch.Close())
}

// Errors returns the error channel.
func (r *Responder) Errors() <-chan error {
return r.errs
}
8 changes: 6 additions & 2 deletions client/test/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,14 +104,16 @@ type (
EnableStages() Stages
// SetStages enables role synchronization using the given stages.
SetStages(Stages)
// Errors returns the error channel.
Errors() <-chan error
}

// Stages are used to synchronize multiple roles.
Stages = []sync.WaitGroup
)

// ExecuteTwoPartyTest executes the specified client test.
func ExecuteTwoPartyTest(ctx context.Context, t *testing.T, role [2]Executer, cfg ExecConfig, errs chan error) {
func ExecuteTwoPartyTest(ctx context.Context, t *testing.T, role [2]Executer, cfg ExecConfig) {
t.Helper()
log.Info("Starting two-party test")
defer log.Info("Two-party test done")
Expand All @@ -136,7 +138,9 @@ func ExecuteTwoPartyTest(ctx context.Context, t *testing.T, role [2]Executer, cf
case <-wg.WaitCh():
case <-ctx.Done():
t.Fatal(ctx.Err())
case err := <-errs:
case err := <-role[0].Errors():
t.Fatal(err)
case err := <-role[1].Errors():
t.Fatal(err)
}
}
Expand Down
2 changes: 1 addition & 1 deletion client/virtual_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func setupVirtualChannelTest(t *testing.T, ctx context.Context) (vct virtualChan
vct.errs = make(chan error, 10)

// Setup clients.
clients, _ := NewClients(
clients := NewClients(
t,
rng,
[]string{"Alice", "Bob", "Ingrid"},
Expand Down

0 comments on commit 35f8a87

Please sign in to comment.