-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Add t.Helper() Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Rearrange arguments in fundAll. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Add comment for FundAll. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Add const for waiting time. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Change to loop. Signed-off-by: Sophia Koehler <[email protected]> Revert "refactor(fund): change to loop." This reverts commit a567031. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): change to loop. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): remove loop, add 2 second wait. Signed-off-by: Sophia Koehler <[email protected]> refactor(funder): rearrange imports. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Add waitgroup. Signed-off-by: Sophia Koehler <[email protected]> refactor(funder_test) refactor(fund): Add concurrent testing. Signed-off-by: Sophia Koehler <[email protected]> refactor(fund): Add comments, delete unnecessary function. Signed-off-by: Sophia Koehler <[email protected]>
- Loading branch information
Showing
4 changed files
with
45 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,68 @@ | ||
// Copyright 2020 - See NOTICE file for copyright holders. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package test | ||
|
||
import ( | ||
"context" | ||
"github.com/perun-network/perun-eth-backend/channel" | ||
pchannel "perun.network/go-perun/channel" | ||
pkgerrors "polycry.pt/poly-go/errors" | ||
"sync" | ||
"testing" | ||
"time" | ||
|
||
"github.com/perun-network/perun-eth-backend/channel" | ||
"github.com/stretchr/testify/require" | ||
pchannel "perun.network/go-perun/channel" | ||
) | ||
|
||
// waitTime is the time to wait before funding a channel when the funder is not egoistic. | ||
const waitTime = 2 * time.Second | ||
|
||
// FunderFinishTime is the returned type of FundAll which includes the process time of each funder. | ||
type FunderFinishTime struct { | ||
Index int // Funder ID | ||
Time time.Duration // Time when Fund method returned | ||
Index int | ||
Time time.Duration | ||
} | ||
|
||
func FundAll(ctx context.Context, funders []*channel.Funder, reqs []*pchannel.FundingReq) ([]FunderFinishTime, error) { | ||
g := pkgerrors.NewGatherer() | ||
// FundAll calls fund for all funders simultaneously. | ||
func FundAll(ctx context.Context, t *testing.T, funders []*channel.Funder, reqs []*pchannel.FundingReq, egoisticIndex int) ([]FunderFinishTime, error) { | ||
t.Helper() | ||
finishTimes := make([]FunderFinishTime, len(funders)) | ||
var wg sync.WaitGroup | ||
var mutex sync.Mutex | ||
|
||
var wg sync.WaitGroup | ||
wg.Add(len(funders)) | ||
|
||
for i := range funders { | ||
i := i | ||
g.Go(func() error { | ||
go func() { | ||
defer wg.Done() | ||
if i != egoisticIndex { | ||
time.Sleep(waitTime) | ||
} | ||
startTime := time.Now() | ||
err := funders[i].Fund(ctx, *reqs[i]) | ||
require.NoError(t, err) | ||
finishTime := time.Now() | ||
mutex.Lock() | ||
finishTimes[i] = FunderFinishTime{ | ||
Index: i, | ||
Time: finishTime.Sub(startTime), | ||
} | ||
mutex.Unlock() | ||
return err | ||
}) | ||
}() | ||
} | ||
wg.Wait() | ||
|
||
wg.Wait() | ||
return finishTimes, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters