Merge pull request #509 from nats-io/remote_monitoring_port

Added ability to use random ports to limit unit test port contention.
This commit is contained in:
Derek Collison
2017-06-09 06:56:40 -07:00
committed by GitHub
3 changed files with 58 additions and 3 deletions

View File

@@ -13,7 +13,7 @@ install:
before_script:
- EXCLUDE_VENDOR=$(go list ./... | grep -v "/vendor/")
- go build
- go fmt ./...
- $(exit $(go fmt $EXCLUDE_VENDOR | wc -l))
- go vet $EXCLUDE_VENDOR
- gosimple $EXCLUDE_VENDOR
- misspell -error -locale US .

View File

@@ -510,17 +510,26 @@ func (s *Server) startMonitoring(secure bool) error {
hp string
err error
httpListener net.Listener
port int
)
if secure {
hp = net.JoinHostPort(opts.HTTPHost, strconv.Itoa(opts.HTTPSPort))
port = opts.HTTPSPort
if port == -1 {
port = 0
}
hp = net.JoinHostPort(opts.HTTPHost, strconv.Itoa(port))
s.Noticef("Starting https monitor on %s", hp)
config := util.CloneTLSConfig(opts.TLSConfig)
config.ClientAuth = tls.NoClientCert
httpListener, err = tls.Listen("tcp", hp, config)
} else {
hp = net.JoinHostPort(opts.HTTPHost, strconv.Itoa(opts.HTTPPort))
port = opts.HTTPPort
if port == -1 {
port = 0
}
hp = net.JoinHostPort(opts.HTTPHost, strconv.Itoa(port))
s.Noticef("Starting http monitor on %s", hp)
httpListener, err = net.Listen("tcp", hp)
}
@@ -870,6 +879,16 @@ func (s *Server) Addr() net.Addr {
return s.listener.Addr()
}
// MonitorAddr will return the net.Addr object for the monitoring listener.
func (s *Server) MonitorAddr() net.Addr {
s.mu.Lock()
defer s.mu.Unlock()
if s.http == nil {
return nil
}
return s.http.Addr()
}
// ReadyForConnections returns `true` if the server is ready to accept client
// and, if routing is enabled, route connections. If after the duration
// `dur` the server is still not ready, returns `false`.

View File

@@ -279,6 +279,7 @@ func TestProcessCommandLineArgs(t *testing.T) {
func TestWriteDeadline(t *testing.T) {
opts := DefaultOptions
opts.WriteDeadline = 20 * time.Millisecond
opts.NoLog = false
s := RunServer(&opts)
defer s.Shutdown()
@@ -328,3 +329,38 @@ func TestWriteDeadline(t *testing.T) {
}
t.Fatal("Connection should have been closed")
}
func TestRandomPorts(t *testing.T) {
opts := DefaultOptions
opts.HTTPPort = -1
opts.Port = -1
s := RunServer(&opts)
defer s.Shutdown()
if s.Addr() == nil || s.Addr().(*net.TCPAddr).Port <= 0 {
t.Fatal("Should have dynamically assigned server port.")
}
if s.Addr() == nil || s.Addr().(*net.TCPAddr).Port == 4222 {
t.Fatal("Should not have dynamically assigned default port: 4222.")
}
if s.MonitorAddr() == nil || s.MonitorAddr().(*net.TCPAddr).Port <= 0 {
t.Fatal("Should have dynamically assigned monitoring port.")
}
}
func TestNilMonitoringPort(t *testing.T) {
opts := DefaultOptions
opts.HTTPPort = 0
opts.HTTPSPort = 0
s := RunServer(&opts)
defer s.Shutdown()
if s.MonitorAddr() != nil {
t.Fatal("HttpAddr should be nil.")
}
}