Skip to content
This repository has been archived by the owner on Feb 24, 2021. It is now read-only.

Commit

Permalink
✅ [backend/ethereum/channel] Removed concurrent requires.
Browse files Browse the repository at this point in the history
  • Loading branch information
RmbRT authored and sebastianst committed Mar 5, 2020
1 parent 39c1fcf commit 194410c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
40 changes: 21 additions & 19 deletions backend/ethereum/channel/funder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"context"
"math/big"
"math/rand"
"sync"
"testing"
"time"

Expand All @@ -18,14 +17,15 @@ import (
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"perun.network/go-perun/backend/ethereum/wallet"
"perun.network/go-perun/channel"
wallettest "perun.network/go-perun/wallet/test"

"perun.network/go-perun/backend/ethereum/channel/test"
"perun.network/go-perun/backend/ethereum/wallet"
ethwallettest "perun.network/go-perun/backend/ethereum/wallet/test"
"perun.network/go-perun/channel"
channeltest "perun.network/go-perun/channel/test"
pkgtest "perun.network/go-perun/pkg/test"
perunwallet "perun.network/go-perun/wallet"
wallettest "perun.network/go-perun/wallet/test"
)

const (
Expand Down Expand Up @@ -83,13 +83,14 @@ func testFundingTimeout(t *testing.T, faultyPeer, peers int) {
t.Logf("seed is %d", seed)
rng := rand.New(rand.NewSource(seed))

ct := pkgtest.NewConcurrent(t)

_, funders, _, params, allocation := newNFunders(ctx, t, rng, peers)
var wg sync.WaitGroup
wg.Add(peers)

for i, funder := range funders {
sleepTime := time.Duration(rng.Int63n(10) + 1)
go func(i int, funder *Funder) {
defer wg.Done()
i, funder := i, funder
go ct.StageN("funding loop", peers, func(rt require.TestingT) {
// Faulty peer does not fund the channel.
if i == faultyPeer {
return
Expand All @@ -103,13 +104,13 @@ func testFundingTimeout(t *testing.T, faultyPeer, peers int) {
ctx2, cancel := context.WithTimeout(context.Background(), 200*time.Millisecond)
defer cancel()
err := funder.Fund(ctx2, req)
require.True(t, channel.IsFundingTimeoutError(err), "funder should return FundingTimeoutError")
require.True(rt, channel.IsFundingTimeoutError(err), "funder should return FundingTimeoutError")
pErr := errors.Cause(err).(*channel.FundingTimeoutError) // unwrap error
assert.Equal(t, pErr.Errors[0].Asset, 0, "Wrong asset set")
assert.Equal(t, uint16(faultyPeer), pErr.Errors[0].TimedOutPeers[0], "Peer should be detected as erroneous")
}(i, funder)
})
}
wg.Wait()
ct.Wait("funding loop")
}

func TestFunder_Fund_multi(t *testing.T) {
Expand All @@ -127,31 +128,32 @@ func testFunderFunding(t *testing.T, n int) {
t.Logf("seed is %d", seed)
rng := rand.New(rand.NewSource(seed))

ct := pkgtest.NewConcurrent(t)

_, funders, _, params, allocation := newNFunders(ctx, t, rng, n)
var wg sync.WaitGroup
wg.Add(n)

for i, funder := range funders {
sleepTime := time.Duration(rng.Int63n(10) + 1)
go func(i int, funder *Funder) {
defer wg.Done()
i, funder := i, funder
go ct.StageN("funding loop", n, func(rt require.TestingT) {
time.Sleep(sleepTime * time.Millisecond)
req := channel.FundingReq{
Params: params,
Allocation: allocation,
Idx: uint16(i),
}
err := funder.Fund(ctx, req)
require.NoError(t, err, "funding should succeed")
require.NoError(rt, err, "funding should succeed")
newAlloc, err := getOnChainAllocation(ctx, funder, req)
require.NoError(t, err, "Get Post-Funding state should succeed")
require.NoError(rt, err, "Get Post-Funding state should succeed")
for i := range newAlloc {
for k := range newAlloc[i] {
assert.Equal(t, req.Allocation.OfParts[i][k], newAlloc[i][k], "Post-Funding balances should equal expected balances")
}
}
}(i, funder)
})
}
wg.Wait()
ct.Wait("funding loop")
}

func newNFunders(
Expand Down
14 changes: 7 additions & 7 deletions backend/ethereum/channel/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/require"
"perun.network/go-perun/channel"
channeltest "perun.network/go-perun/channel/test"
pkgtest "perun.network/go-perun/pkg/test"
)

func TestAdjudicator_MultipleRegisters(t *testing.T) {
Expand Down Expand Up @@ -46,22 +47,21 @@ func registerMultipleConcurrent(t *testing.T, numParts int, parallel bool) {
fundingCtx, funCancel := context.WithTimeout(context.Background(), timeout)
defer funCancel()
// fund the contract
var wgFund sync.WaitGroup
wgFund.Add(numParts)
ct := pkgtest.NewConcurrent(t)
for i, funder := range funders {
sleepTime := time.Duration(rng.Int63n(10) + 1)
go func(i int, funder *Funder) {
defer wgFund.Done()
i, funder := i, funder
go ct.StageN("funding loop", numParts, func(rt require.TestingT) {
time.Sleep(sleepTime * time.Millisecond)
req := channel.FundingReq{
Params: params,
Allocation: &state.Allocation,
Idx: channel.Index(i),
}
require.NoError(t, funders[i].Fund(fundingCtx, req), "funding should succeed")
}(i, funder)
require.NoError(rt, funder.Fund(fundingCtx, req), "funding should succeed")
})
}
wgFund.Wait()
ct.Wait("funding loop")

// Now test the register function
ctx, cancel := context.WithTimeout(context.Background(), timeout)
Expand Down
14 changes: 7 additions & 7 deletions backend/ethereum/channel/withdraw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/stretchr/testify/require"
"perun.network/go-perun/channel"
channeltest "perun.network/go-perun/channel/test"
pkgtest "perun.network/go-perun/pkg/test"
)

func TestAdjudicator_MultipleWithdraws_FinalState(t *testing.T) {
Expand Down Expand Up @@ -46,22 +47,21 @@ func withdrawMultipleConcurrentFinal(t *testing.T, numParts int, parallel bool)
fundingCtx, funCancel := context.WithTimeout(context.Background(), timeout)
defer funCancel()
// fund the contract
var wg sync.WaitGroup
wg.Add(numParts)
ct := pkgtest.NewConcurrent(t)
for i, funder := range funders {
sleepTime := time.Duration(rng.Int63n(10) + 1)
go func(i int, funder *Funder) {
defer wg.Done()
i, funder := i, funder
go ct.StageN("funding loop", numParts, func(rt require.TestingT) {
time.Sleep(sleepTime * time.Millisecond)
req := channel.FundingReq{
Params: params,
Allocation: &state.Allocation,
Idx: channel.Index(i),
}
require.NoError(t, funders[i].Fund(fundingCtx, req), "funding should succeed")
}(i, funder)
require.NoError(rt, funder.Fund(fundingCtx, req), "funding should succeed")
})
}
wg.Wait()
ct.Wait("funding loop")
// manipulate the state
state.IsFinal = true
tx := signState(t, accs, params, state)
Expand Down

0 comments on commit 194410c

Please sign in to comment.