mirror of
https://github.com/gogrlx/nats-server.git
synced 2026-04-14 10:10:42 -07:00
Added ability to use random ports to limit unit test port contention.
This commit is contained in:
@@ -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()
|
||||
}
|
||||
|
||||
// HttpAddr will return the net.Addr object for the http monitoring listener.
|
||||
func (s *Server) HttpAddr() 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`.
|
||||
|
||||
@@ -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,25 @@ 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.HttpAddr() == nil || s.HttpAddr().(*net.TCPAddr).Port <= 0 {
|
||||
t.Fatal("Should have dynamically assigned monitoring port.")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user