Skip to content
This repository has been archived by the owner on May 11, 2024. It is now read-only.

Commit

Permalink
feat(prover): use httptest.Server to simplify the prover server tes…
Browse files Browse the repository at this point in the history
…ts (#389)

Co-authored-by: Roger <[email protected]>
Co-authored-by: RogerLamTd <[email protected]>
Co-authored-by: David <[email protected]>
  • Loading branch information
4 people authored Sep 28, 2023
1 parent bdca930 commit 84eedae
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 50 deletions.
55 changes: 29 additions & 26 deletions prover/server/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,48 +3,51 @@ package server
import (
"crypto/rand"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"strings"
"time"

"github.com/cyberhorsey/webutils/testutils"
"github.com/ethereum/go-ethereum/common"
"github.com/labstack/echo/v4"
"github.com/taikoxyz/taiko-client/bindings/encoding"
)

func (s *ProverServerTestSuite) TestGetStatusSuccess() {
rec := s.sendReq("/status")
s.Equal(http.StatusOK, rec.Code)
resp := s.sendReq("/status")
s.Equal(http.StatusOK, resp.StatusCode)

status := new(Status)
s.Nil(json.Unmarshal(rec.Body.Bytes(), &status))

s.Equal(s.srv.minProofFee.Uint64(), status.MinProofFee)
s.Equal(uint64(s.srv.maxExpiry.Seconds()), status.MaxExpiry)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
s.Nil(err)
s.Nil(json.Unmarshal(b, &status))

s.Equal(s.ps.minProofFee.Uint64(), status.MinProofFee)
s.Equal(uint64(s.ps.maxExpiry.Seconds()), status.MaxExpiry)
s.Greater(status.CurrentCapacity, uint64(0))
}

func (s *ProverServerTestSuite) TestProposeBlockSuccess() {
rec := httptest.NewRecorder()

s.srv.ServeHTTP(rec, testutils.NewUnauthenticatedRequest(
echo.POST,
"/assignment",
&encoding.ProposeBlockData{
Fee: common.Big256,
Expiry: uint64(time.Now().Add(time.Minute).Unix()),
Input: encoding.TaikoL1BlockMetadataInput{
Proposer: common.BytesToAddress(randomHash().Bytes()),
TxListHash: randomHash(),
TxListByteStart: common.Big0,
TxListByteEnd: common.Big0,
CacheTxListInfo: false,
},
data, err := json.Marshal(encoding.ProposeBlockData{
Fee: common.Big256,
Expiry: uint64(time.Now().Add(time.Minute).Unix()),
Input: encoding.TaikoL1BlockMetadataInput{
Proposer: common.BytesToAddress(randomHash().Bytes()),
TxListHash: randomHash(),
TxListByteStart: common.Big0,
TxListByteEnd: common.Big0,
CacheTxListInfo: false,
},
))

testutils.AssertStatusAndBody(s.T(), rec, http.StatusOK, []string{"signedPayload"})
})
s.Nil(err)
resp, err := http.Post(s.ws.URL+"/assignment", "application/json", strings.NewReader(string(data)))
s.Nil(err)
s.Equal(http.StatusOK, resp.StatusCode)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
s.Nil(err)
s.Contains(string(b), "signedPayload")
}

// randomHash generates a random blob of data and returns it as a hash.
Expand Down
5 changes: 0 additions & 5 deletions prover/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ func (srv *ProverServer) Shutdown(ctx context.Context) error {
return srv.echo.Shutdown(ctx)
}

// ServeHTTP implements the `http.Handler` interface which serves HTTP requests.
func (srv *ProverServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
srv.echo.ServeHTTP(w, r)
}

// Health endpoints for probes.
func (srv *ProverServer) Health(c echo.Context) error {
return c.NoContent(http.StatusOK)
Expand Down
35 changes: 16 additions & 19 deletions prover/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ import (

type ProverServerTestSuite struct {
suite.Suite
srv *ProverServer
ps *ProverServer
ws *httptest.Server // web server
}

func (s *ProverServerTestSuite) SetupTest() {
Expand All @@ -45,7 +46,7 @@ func (s *ProverServerTestSuite) SetupTest() {
})
s.Nil(err)

srv := &ProverServer{
p := &ProverServer{
echo: echo.New(),
proverPrivateKey: l1ProverPrivKey,
minProofFee: common.Big1,
Expand All @@ -57,19 +58,19 @@ func (s *ProverServerTestSuite) SetupTest() {
isOracle: false,
}

srv.echo.HideBanner = true
srv.configureMiddleware()
srv.configureRoutes()

s.srv = srv
p.echo.HideBanner = true
p.configureMiddleware()
p.configureRoutes()
s.ps = p
s.ws = httptest.NewServer(p.echo)
}

func (s *ProverServerTestSuite) TestHealth() {
s.Equal(http.StatusOK, s.sendReq("/healthz").Code)
s.Equal(http.StatusOK, s.sendReq("/healthz").StatusCode)
}

func (s *ProverServerTestSuite) TestRoot() {
s.Equal(http.StatusOK, s.sendReq("/").Code)
s.Equal(http.StatusOK, s.sendReq("/").StatusCode)
}

func (s *ProverServerTestSuite) TestStartShutdown() {
Expand All @@ -80,7 +81,7 @@ func (s *ProverServerTestSuite) TestStartShutdown() {
s.Nil(err)

go func() {
if err := s.srv.Start(fmt.Sprintf(":%v", port)); err != nil {
if err := s.ps.Start(fmt.Sprintf(":%v", port)); err != nil {
log.Error("Failed to start prover server", "error", err)
}
}()
Expand All @@ -98,23 +99,19 @@ func (s *ProverServerTestSuite) TestStartShutdown() {
return nil
}, backoff.NewExponentialBackOff()))

s.Nil(s.srv.Shutdown(context.Background()))
s.Nil(s.ps.Shutdown(context.Background()))
}

func (s *ProverServerTestSuite) TearDownTest() {
s.Nil(s.srv.Shutdown(context.Background()))
s.ws.Close()
}

func TestProverServerTestSuite(t *testing.T) {
suite.Run(t, new(ProverServerTestSuite))
}

func (s *ProverServerTestSuite) sendReq(path string) *httptest.ResponseRecorder {
req, err := http.NewRequest(echo.GET, path, nil)
func (s *ProverServerTestSuite) sendReq(path string) *http.Response {
resp, err := http.Get(s.ws.URL + path)
s.Nil(err)
rec := httptest.NewRecorder()

s.srv.ServeHTTP(rec, req)

return rec
return resp
}

0 comments on commit 84eedae

Please sign in to comment.