Skip to content

Commit

Permalink
test: update mock.Server to use httptest.Server (cosmos#1145)
Browse files Browse the repository at this point in the history
## Overview

This PR improves upon cosmos#1144 by simplifying `mock.Server` to use
`httptest` directly, avoiding a duplicate `http.Server`. `Server.Start`
doesn't need a listener anymore, and it returns the URL at which the
`httptest.Server` is running.

## Checklist

<!-- 
Please complete the checklist to ensure that the PR is ready to be
reviewed.

IMPORTANT:
PRs should be left in Draft until the below checklist is completed.
-->

- [ ] New and updated code has appropriate documentation
- [ ] New and updated code has new and/or updated testing
- [ ] Required CI checks are passing
- [ ] Visual proof for any user facing features like CLI or
documentation updates
- [ ] Linked issues closed with keywords
  • Loading branch information
tuxcanfly authored Aug 17, 2023
1 parent e0cc388 commit 3967fc2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 23 deletions.
29 changes: 11 additions & 18 deletions da/celestia/mock/server.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package mock

import (
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net"
"net/http"
"net/http/httptest"
"time"

mux2 "github.com/gorilla/mux"
Expand Down Expand Up @@ -54,7 +53,7 @@ type response struct {
type Server struct {
mock *mockda.DataAvailabilityLayerClient
blockTime time.Duration
server *http.Server
server *httptest.Server
logger log.Logger
}

Expand All @@ -68,36 +67,30 @@ func NewServer(blockTime time.Duration, logger log.Logger) *Server {
}

// Start starts HTTP server with given listener.
func (s *Server) Start(listener net.Listener) error {
func (s *Server) Start() (string, error) {
kvStore, err := store.NewDefaultInMemoryKVStore()
if err != nil {
return err
return "", err
}
err = s.mock.Init([8]byte{}, []byte(s.blockTime.String()), kvStore, s.logger)
if err != nil {
return err
return "", err
}
err = s.mock.Start()
if err != nil {
return err
return "", err
}
go func() {
s.server = new(http.Server)
s.server.Handler = s.Handler()
err := s.server.Serve(listener)
s.logger.Debug("http server exited with", "error", err)
}()
return nil
s.server = httptest.NewServer(s.getHandler())
s.logger.Debug("http server exited with", "error", err)
return s.server.URL, nil
}

// Stop shuts down the Server.
func (s *Server) Stop() {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
_ = s.server.Shutdown(ctx)
s.server.Close()
}

func (s *Server) Handler() http.Handler {
func (s *Server) getHandler() http.Handler {
mux := mux2.NewRouter()
mux.HandleFunc("/", s.rpc).Methods(http.MethodPost)

Expand Down
7 changes: 2 additions & 5 deletions da/test/da_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"math/rand"
"net"
"net/http/httptest"
"os"
"strconv"
"testing"
Expand Down Expand Up @@ -34,7 +33,6 @@ var (
testNamespaceID = types.NamespaceID{0, 1, 2, 3, 4, 5, 6, 7}

testConfig = celestia.Config{
BaseURL: "http://localhost:26658",
Timeout: 30 * time.Second,
GasLimit: 3000000,
}
Expand Down Expand Up @@ -120,13 +118,12 @@ func startMockGRPCServ() *grpc.Server {

func startMockCelestiaNodeServer() *cmock.Server {
httpSrv := cmock.NewServer(mockDaBlockTime, cmlog.NewTMLogger(os.Stdout))
ts := httptest.NewServer(httpSrv.Handler())
testConfig.BaseURL = ts.URL
err := httpSrv.Start(ts.Listener)
url, err := httpSrv.Start()
if err != nil {
fmt.Println("can't start mock celestia-node RPC server")
return nil
}
testConfig.BaseURL = url
return httpSrv
}

Expand Down

0 comments on commit 3967fc2

Please sign in to comment.