Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
golangisfun123 committed Jun 20, 2024
1 parent 32ce20c commit 1ec0562
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 14 deletions.
78 changes: 68 additions & 10 deletions ethergo/listener/listener_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,22 @@ package listener_test
import (
"context"
"fmt"
"sync"

"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/core/types"
"github.com/synapsecns/sanguine/ethergo/listener"
"sync"
)

func (l *ListenerTestSuite) TestListenForEvents() {
_, handle := l.manager.GetCounter(l.GetTestContext(), l.backend)
var wg sync.WaitGroup
const iterations = 10
for i := 0; i < iterations; i++ {
i := i
wg.Add(1)
go func(_ int) {
go func() {
defer wg.Done()

auth := l.backend.GetTxContext(l.GetTestContext(), nil)

//nolint:typecheck
bridgeRequestTX, err := handle.IncrementCounter(auth.TransactOpts)
l.NoError(err)
Expand All @@ -32,18 +30,55 @@ func (l *ListenerTestSuite) TestListenForEvents() {
l.NoError(err)
l.NotNil(bridgeResponseTX)
l.backend.WaitForConfirmation(l.GetTestContext(), bridgeResponseTX)
}(i)
}()
}

wg.Wait()

startBlock, err := handle.DeployBlock(&bind.CallOpts{Context: l.GetTestContext()})
l.NoError(err)

cl, err := listener.NewChainListener(l.backend, l.store, handle.Address(), uint64(startBlock.Int64()), l.metrics, listener.WithNewBlockHandler(func(ctx context.Context, block uint64) error {
fmt.Println(block)
return nil
}))
cl, err := listener.NewChainListener(
l.backend,
l.store,
handle.Address(),
uint64(startBlock.Int64()),
l.metrics,
listener.WithNewBlockHandler(func(ctx context.Context, block uint64) error {
fmt.Println(block)
return nil
}),
)
l.NoError(err)

clSafe, err := listener.NewChainListener(
l.backend,
l.store,
handle.Address(),
uint64(startBlock.Int64()),
l.metrics,
listener.WithNewBlockHandler(func(ctx context.Context, block uint64) error {
fmt.Println(block)
return nil
}),
listener.WithFinalityMode("safe"),
listener.WithBlockWait(10),
)
l.NoError(err)

clFinalized, err := listener.NewChainListener(
l.backend,
l.store,
handle.Address(),
uint64(startBlock.Int64()),
l.metrics,
listener.WithNewBlockHandler(func(ctx context.Context, block uint64) error {
fmt.Println(block)
return nil
}),
listener.WithFinalityMode("finalized"),
listener.WithBlockWait(10),
)
l.NoError(err)

eventCount := 0
Expand All @@ -59,4 +94,27 @@ func (l *ListenerTestSuite) TestListenForEvents() {

return nil
})

_ = clSafe.Listen(listenCtx, func(ctx context.Context, log types.Log) error {
eventCount++

if eventCount == iterations*2 {
cancel()
}

return nil
})

_ = clFinalized.Listen(listenCtx, func(ctx context.Context, log types.Log) error {
eventCount++

if eventCount == iterations*2 {
cancel()
}

return nil
})

l.NotEqual(cl.LatestBlock(), clFinalized.LatestBlock(), clSafe.LatestBlock())

}
4 changes: 0 additions & 4 deletions ethergo/listener/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,6 @@ func WithFinalityMode(mode string) Option {
switch mode {
case "latest":
c.finalityMode = rpc.LatestBlockNumber
case "earliest":
c.finalityMode = rpc.EarliestBlockNumber
case "pending":
c.finalityMode = rpc.PendingBlockNumber
case "safe":
c.finalityMode = rpc.SafeBlockNumber
case "finalized":
Expand Down

0 comments on commit 1ec0562

Please sign in to comment.