Skip to content

Commit

Permalink
server: make http test listen on port 0 (pingcap#19266)
Browse files Browse the repository at this point in the history
  • Loading branch information
lysu authored Aug 19, 2020
1 parent 4774cb9 commit 172a2d3
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
11 changes: 9 additions & 2 deletions server/http_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -360,18 +361,24 @@ func (ts *basicHTTPHandlerTestSuite) startServer(c *C) {
ts.tidbdrv = NewTiDBDriver(ts.store)

cfg := newTestConfig()
cfg.Port = ts.port
cfg.Store = "tikv"
cfg.Status.StatusPort = ts.statusPort
cfg.Port = 0
cfg.Status.StatusPort = 0
cfg.Status.ReportStatus = true

server, err := NewServer(cfg, ts.tidbdrv)
c.Assert(err, IsNil)
ts.port = getPortFromTCPAddr(server.listener.Addr())
ts.statusPort = getPortFromTCPAddr(server.statusListener.Addr())
ts.server = server
go server.Run()
ts.waitUntilServerOnline()
}

func getPortFromTCPAddr(addr net.Addr) uint {
return uint(addr.(*net.TCPAddr).Port)
}

func (ts *basicHTTPHandlerTestSuite) stopServer(c *C) {
if ts.domain != nil {
ts.domain.Close()
Expand Down
5 changes: 4 additions & 1 deletion server/http_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func sleepWithCtx(ctx context.Context, d time.Duration) {

func (s *Server) listenStatusHTTPServer() error {
s.statusAddr = fmt.Sprintf("%s:%d", s.cfg.Status.StatusHost, s.cfg.Status.StatusPort)
if s.cfg.Status.StatusPort == 0 {
if s.cfg.Status.StatusPort == 0 && !runInGoTest {
s.statusAddr = fmt.Sprintf("%s:%d", s.cfg.Status.StatusHost, defaultStatusPort)
}

Expand All @@ -96,6 +96,9 @@ func (s *Server) listenStatusHTTPServer() error {
if err != nil {
logutil.BgLogger().Info("listen failed", zap.Error(err))
return errors.Trace(err)
} else if runInGoTest && s.cfg.Status.StatusPort == 0 {
s.statusAddr = s.statusListener.Addr().String()
s.cfg.Status.StatusPort = uint(s.statusListener.Addr().(*net.TCPAddr).Port)
}
return nil
}
Expand Down
16 changes: 11 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ package server
import (
"context"
"crypto/tls"
"flag"
"fmt"
"io"
"math/rand"
Expand Down Expand Up @@ -66,10 +67,11 @@ import (
)

var (
baseConnID uint32
serverPID int
osUser string
osVersion string
baseConnID uint32
serverPID int
osUser string
osVersion string
runInGoTest bool
)

func init() {
Expand All @@ -84,6 +86,7 @@ func init() {
if err != nil {
osVersion = ""
}
runInGoTest = flag.Lookup("test.v") != nil || flag.Lookup("check.v") != nil
}

var (
Expand Down Expand Up @@ -229,7 +232,7 @@ func NewServer(cfg *config.Config, driver IDriver) (*Server, error) {
s.capability |= mysql.ClientSSL
}

if s.cfg.Host != "" && s.cfg.Port != 0 {
if s.cfg.Host != "" && (s.cfg.Port != 0 || runInGoTest) {
addr := fmt.Sprintf("%s:%d", s.cfg.Host, s.cfg.Port)
if s.listener, err = net.Listen("tcp", addr); err == nil {
logutil.BgLogger().Info("server is running MySQL protocol", zap.String("addr", addr))
Expand All @@ -239,6 +242,9 @@ func NewServer(cfg *config.Config, driver IDriver) (*Server, error) {
go s.forwardUnixSocketToTCP()
}
}
if runInGoTest && s.cfg.Port == 0 {
s.cfg.Port = uint(s.listener.Addr().(*net.TCPAddr).Port)
}
}
} else if cfg.Socket != "" {
if s.listener, err = net.Listen("unix", cfg.Socket); err == nil {
Expand Down

0 comments on commit 172a2d3

Please sign in to comment.