Skip to content

Commit

Permalink
l1: add EthSubscriber test
Browse files Browse the repository at this point in the history
  • Loading branch information
weiihann committed Jun 27, 2024
1 parent 22794c2 commit d7f1fa1
Showing 1 changed file with 64 additions and 0 deletions.
64 changes: 64 additions & 0 deletions l1/l1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"context"
"errors"
"math/big"
"net"
"net/http"
"testing"
"time"

Expand All @@ -15,6 +17,8 @@ import (
"github.com/NethermindEth/juno/l1/contract"
"github.com/NethermindEth/juno/mocks"
"github.com/NethermindEth/juno/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/rpc"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)
Expand Down Expand Up @@ -154,3 +158,63 @@ func TestEventListener(t *testing.T) {
StateRoot: new(felt.Felt),
}, got)
}

func newTestL1Client(service service) *rpc.Server {
server := rpc.NewServer()
if err := server.RegisterName("eth", service); err != nil {
panic(err)
}
return server
}

type service interface {
GetBlockByNumber(ctx context.Context, number string, fullTx bool) (interface{}, error)
}

type testService struct{}

func (testService) GetBlockByNumber(ctx context.Context, number string, fullTx bool) (interface{}, error) {
return 100, nil
}

type testEmptyService struct{}

func (testEmptyService) GetBlockByNumber(ctx context.Context, number string, fullTx bool) (interface{}, error) {
return nil, nil
}

func TestEthSubscriber_EmptyFinalisedHeight(t *testing.T) {
tc := []service{
testService{},
testEmptyService{},
}

for _, sv := range tc {
t.Run("", func(t *testing.T) {
startServer := func(addr string, service service) (*rpc.Server, net.Listener) {
srv := newTestL1Client(service)
l, err := net.Listen("tcp", addr)
if err != nil {
t.Fatal("can't listen:", err)
}
go func() {
_ = http.Serve(l, srv.WebsocketHandler([]string{"*"}))
}()
return srv, l
}

ctx, cancel := context.WithTimeout(context.Background(), 12*time.Second)
defer cancel()

server, listener := startServer("127.0.0.1:0", sv)
defer server.Stop()

subscriber, err := l1.NewEthSubscriber("ws://"+listener.Addr().String(), common.Address{})
require.NoError(t, err)

height, err := subscriber.FinalisedHeight(ctx)
require.Equal(t, uint64(0), height)
require.Error(t, err)
})
}
}

0 comments on commit d7f1fa1

Please sign in to comment.