Skip to content

Commit

Permalink
refactor(fund): Change to loop.
Browse files Browse the repository at this point in the history
Signed-off-by: Sophia Koehler <[email protected]>
  • Loading branch information
sophia1ch committed Apr 2, 2024
1 parent 2c897b5 commit bd8cb49
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 32 deletions.
4 changes: 2 additions & 2 deletions channel/funder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func testFunderCrossOverFunding(t *testing.T, n int) {

func TestEgoisticParticipantFunding(t *testing.T) {
// Peers will randomly fund for each other.
for i := 0; i < 10; i++ {
for i := 0; i < 30; i++ {
name := fmt.Sprintf("Egoistic Funding %v", i)
t.Run(name, func(t *testing.T) { testEgoisticParticipantFunding(t) })
}
Expand Down Expand Up @@ -235,7 +235,7 @@ func testEgoisticParticipantFunding(t *testing.T) {
require.True(t, len(finishTimes) == n, "Length of indexes must be n")
require.NoError(t, err)

t.Logf("Finish Times: %v", finishTimes)
t.Logf("finishTimes: %v", finishTimes)
// Check if finish time of egoistic funder is larger than finish time of non-egoistic funder.
correct := finishTimes[egoisticIndex].Time > finishTimes[int(math.Abs(float64(1-egoisticIndex)))].Time
// Use require.True to compare the finish times
Expand Down
52 changes: 22 additions & 30 deletions channel/test/fund.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,38 +36,30 @@ func FundAll(ctx context.Context, funders []*channel.Funder, reqs []*pchannel.Fu
var mutex sync.Mutex

var wg sync.WaitGroup
wg.Add(2)
wg.Add(len(funders))

go func() {
defer wg.Done()
startTime := time.Now()
err := funders[egoisticIndex].Fund(ctx, *reqs[egoisticIndex])
require.NoError(t, err)
finishTime := time.Now()
require.NoError(t, err)
mutex.Lock()
finishTimes[egoisticIndex] = FunderFinishTime{
Index: egoisticIndex,
Time: finishTime.Sub(startTime),
}
mutex.Unlock()
}()
waitTime := 2 * time.Second

Check failure on line 41 in channel/test/fund.go

View workflow job for this annotation

GitHub Actions / Lint

mnd: Magic number: 2, in <operation> detected (gomnd)

go func() {
defer wg.Done()
time.Sleep(2 * time.Second)
startTime := time.Now()
err := funders[1-egoisticIndex].Fund(ctx, *reqs[1-egoisticIndex])
require.NoError(t, err)
finishTime := time.Now()
require.NoError(t, err)
mutex.Lock()
finishTimes[1-egoisticIndex] = FunderFinishTime{
Index: 1 - egoisticIndex,
Time: finishTime.Sub(startTime),
}
mutex.Unlock()
}()
for i := range funders {
i := i
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()
}()

Check failure on line 61 in channel/test/fund.go

View workflow job for this annotation

GitHub Actions / Lint

unnecessary trailing newline (whitespace)
}

wg.Wait()
return finishTimes, nil
Expand Down

0 comments on commit bd8cb49

Please sign in to comment.