Skip to content

Commit

Permalink
Merge pull request #497 from nats-io/fix_specify_http_and_https_ports
Browse files Browse the repository at this point in the history
[FIXED] Specifying HTTP and HTTPs ports produces unexpected behavior
  • Loading branch information
derekcollison committed May 25, 2017
2 parents 4cef2dd + 513c0a0 commit 1acdd23
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 4 deletions.
6 changes: 6 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ func (s *Server) Start() {
s.logPid()
}

// Specifying both HTTP and HTTPS ports is a misconfiguration
if s.opts.HTTPPort != 0 && s.opts.HTTPSPort != 0 {
Fatalf("Can't specify both HTTP (%v) and HTTPs (%v) ports", s.opts.HTTPPort, s.opts.HTTPSPort)
return
}

// Start up the http server if needed.
if s.opts.HTTPPort != 0 {
s.StartHTTPMonitoring()
Expand Down
83 changes: 80 additions & 3 deletions test/monitor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"io/ioutil"
"net"
"net/http"
"testing"
"time"

"strings"
"sync"
"sync/atomic"
"testing"
"time"

"github.com/nats-io/gnatsd/server"
"github.com/nats-io/go-nats"
Expand Down Expand Up @@ -642,3 +642,80 @@ func createClientConnSubscribeAndPublish(t *testing.T) net.Conn {

return cl
}

func TestMonitorNoTLSConfig(t *testing.T) {
opts := DefaultTestOptions
opts.Port = CLIENT_PORT
opts.HTTPHost = "localhost"
opts.HTTPSPort = MONITOR_PORT
s := server.New(&opts)
defer s.Shutdown()
dl := &dummyLogger{}
s.SetLogger(dl, false, false)
defer s.SetLogger(nil, false, false)
// This should produce a fatal error due to lack of TLS config
s.Start()
if !strings.Contains(dl.msg, "TLS") {
t.Fatalf("Expected error about missing TLS config, got %v", dl.msg)
}
}

func TestMonitorErrorOnListen(t *testing.T) {
s := runMonitorServer()
defer s.Shutdown()

opts := DefaultTestOptions
opts.Port = CLIENT_PORT + 1
opts.HTTPHost = "localhost"
opts.HTTPPort = MONITOR_PORT
s2 := server.New(&opts)
defer s2.Shutdown()
dl := &dummyLogger{}
s2.SetLogger(dl, false, false)
defer s2.SetLogger(nil, false, false)
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
// This will block until server is shutdown
s2.Start()
}()
// Wait for the error to be produced
timeout := time.Now().Add(3 * time.Second)
ok := false
for time.Now().Before(timeout) {
dl.Lock()
msg := dl.msg
dl.Unlock()
if msg == "" {
continue
}
if strings.Contains(msg, "listen") {
ok = true
break
}
}
s2.Shutdown()
wg.Wait()
if !ok {
t.Fatalf("Should have produced a fatal error")
}
}

func TestMonitorBothPortsConfigured(t *testing.T) {
opts := DefaultTestOptions
opts.Port = CLIENT_PORT
opts.HTTPHost = "localhost"
opts.HTTPPort = MONITOR_PORT
opts.HTTPSPort = MONITOR_PORT + 1
s := server.New(&opts)
defer s.Shutdown()
dl := &dummyLogger{}
s.SetLogger(dl, false, false)
defer s.SetLogger(nil, false, false)
// This should produce a fatal error
s.Start()
if !strings.Contains(dl.msg, "specify both") {
t.Fatalf("Expected error about ports configured, got %v", dl.msg)
}
}
15 changes: 14 additions & 1 deletion test/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,33 @@ package test
import (
"fmt"
"strings"
"sync"
"testing"
)

type dummyLogger struct {
sync.Mutex
msg string
}

func (d *dummyLogger) Fatalf(format string, args ...interface{}) {
d.Lock()
d.msg = fmt.Sprintf(format, args...)

d.Unlock()
}

func (d *dummyLogger) Errorf(format string, args ...interface{}) {
}

func (d *dummyLogger) Debugf(format string, args ...interface{}) {
}

func (d *dummyLogger) Tracef(format string, args ...interface{}) {
}

func (d *dummyLogger) Noticef(format string, args ...interface{}) {
}

func TestStackFatal(t *testing.T) {
d := &dummyLogger{}
stackFatalf(d, "test stack %d", 1)
Expand Down

0 comments on commit 1acdd23

Please sign in to comment.