diff --git a/chatserver.go b/chatserver.go index ac49b6a..928f22c 100644 --- a/chatserver.go +++ b/chatserver.go @@ -1,10 +1,8 @@ package chat // This multi-user chat server helps me learn concurrency. -// This is a learning project, please don't count on it improving, or even -// working entirely well. -// You can use nc or telnet to connect to localhost port 40001, -// and chat with this server. +// You can use netcat or telnet to connect to its TCP port. +// This is a learning project, please don't count on it improving. import ( "bufio" @@ -135,6 +133,7 @@ func (c *connection) processInput() { Anything you type will be sent to all other users of this chat server. A line that begins with a slash (/) is considered a command - enter /help for a list of valid commands. `) scanner := bufio.NewScanner(c) + c.server.sendSystemMessage(fmt.Sprintf("%s has joined the chat", c.GetNickname())) for scanner.Scan() { line := scanner.Text() if line == "" { @@ -269,7 +268,7 @@ func WithLogWriter(w io.Writer) ServerOption { func NewServer(options ...ServerOption) (*Server, error) { openForBusiness, stopReceivingSignals := signal.NotifyContext(context.Background(), os.Interrupt) s := &Server{ - listenAddress: ":40001", + listenAddress: ":0", openForBusiness: openForBusiness, stopReceivingSignals: stopReceivingSignals, addConnCh: make(chan *connection), @@ -298,10 +297,9 @@ func (s *Server) createLog() { s.log = log } -// GetListenAddress returns the listen address of the chat server, of the form -// host:port or :port. +// GetListenAddress returns the listen address of the chat server net.Listener. func (s Server) GetListenAddress() string { - return s.listenAddress + return s.listener.Addr().String() } // startConnectionAccepter starts a goroutine that accepts connections to the @@ -347,7 +345,6 @@ func (s *Server) startConnectionAndMessageManager() { case newConn := <-s.addConnCh: s.log.Debugf("adding connection from %s", newConn.UniqueID()) currentConnections = append(currentConnections, newConn) - s.sendSystemMessage(fmt.Sprintf("%s has joined the chat", newConn.GetNickname())) s.numConnections = len(currentConnections) case removeConn := <-s.removeConnCh: s.log.Debugf("removing connection %s", removeConn.UniqueID()) @@ -452,7 +449,7 @@ func (s *Server) ListenAndServe() error { if err != nil { return fmt.Errorf("cannot listen on %s: %v", s.listenAddress, err) } - s.log.Infof("listening for connections on %s", s.listenAddress) + s.log.Infof("listening for connections on %s", s.GetListenAddress()) s.startConnectionAccepter() s.startConnectionAndMessageManager() return nil diff --git a/script_test.go b/script_test.go index 096bbea..9a4a1b0 100644 --- a/script_test.go +++ b/script_test.go @@ -10,6 +10,12 @@ import ( "github.com/rogpeppe/go-internal/testscript" ) +var testScriptSetup func(*testscript.Env) error = func(e *testscript.Env) error { + // default to use a random chat server TCP listener port. + e.Vars = append(e.Vars, "CHATSERVER_LISTEN_ADDRESS=:0") + return nil +} + func TestMain(m *testing.M) { os.Exit(testscript.RunMain(m, map[string]func() int{ "chatserver": chat.RunCLIWithoutWaitingForExit, @@ -18,6 +24,7 @@ func TestMain(m *testing.M) { func TestScript(t *testing.T) { testscript.Run(t, testscript.Params{ - Dir: "testdata/script", + Setup: testScriptSetup, + Dir: "testdata/script", }) } diff --git a/testdata/script/listen-address-envvar.txtar b/testdata/script/listen-address-envvar.txtar index ff74f97..475073d 100644 --- a/testdata/script/listen-address-envvar.txtar +++ b/testdata/script/listen-address-envvar.txtar @@ -1,3 +1,3 @@ env CHATSERVER_LISTEN_ADDRESS=':8765' exec chatserver -stderr 'listening for connections on :8765' +stderr 'listening for connections on .+:8765' diff --git a/testdata/script/listen-address.txtar b/testdata/script/listen-address.txtar index 77eae94..bbc2cec 100644 --- a/testdata/script/listen-address.txtar +++ b/testdata/script/listen-address.txtar @@ -1,2 +1,2 @@ exec chatserver --listen-address :5678 -stderr 'listening for connections on :5678' +stderr 'listening for connections on .+:5678'