Skip to content

Commit

Permalink
Unit test for coordination windows watch function
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasz-zimnoch committed Nov 24, 2023
1 parent c91636c commit f687085
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions pkg/tbtc/coordination_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package tbtc

import (
"github.com/keep-network/keep-core/internal/testutils"
"context"
"testing"
"time"

"github.com/keep-network/keep-core/internal/testutils"
)

func TestCoordinationWindow_ActivePhaseEndBlock(t *testing.T) {
Expand Down Expand Up @@ -58,4 +61,58 @@ func TestCoordinationWindow_IsAfterActivePhase(t *testing.T) {
false,
window.isAfter(nextWindow),
)
}
}

func TestWatchCoordinationWindows(t *testing.T) {
watchBlocksFn := func(ctx context.Context) <-chan uint64 {
blocksChan := make(chan uint64)

go func() {
ticker := time.NewTicker(1 * time.Millisecond)
defer ticker.Stop()

block := uint64(0)

for {
select {
case <-ticker.C:
block++
blocksChan <- block
case <-ctx.Done():
return
}
}
}()

return blocksChan
}

receivedWindows := make([]*coordinationWindow, 0)
onWindowFn := func(window *coordinationWindow) {
receivedWindows = append(receivedWindows, window)
}

ctx, cancelCtx := context.WithTimeout(
context.Background(),
2000*time.Millisecond,
)
defer cancelCtx()

go watchCoordinationWindows(ctx, watchBlocksFn, onWindowFn)

<-ctx.Done()

testutils.AssertIntsEqual(t, "received windows", 2, len(receivedWindows))
testutils.AssertIntsEqual(
t,
"first window",
900,
int(receivedWindows[0].coordinationBlock),
)
testutils.AssertIntsEqual(
t,
"second window",
1800,
int(receivedWindows[1].coordinationBlock),
)
}

0 comments on commit f687085

Please sign in to comment.