Skip to content

Commit

Permalink
TestScript tests use a random chat-server listen port to avoid clashi…
Browse files Browse the repository at this point in the history
…ng, send `has joined the chat` announcements during `Server.processInput()` to avoid an output race condition for tests, `NewServer()` defaults to a random listen-port
  • Loading branch information
ivanfetch committed Aug 20, 2023
1 parent 2345be1 commit 41c50a7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
17 changes: 7 additions & 10 deletions chatserver.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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 == "" {
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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())
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
})
}
2 changes: 1 addition & 1 deletion testdata/script/listen-address-envvar.txtar
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
env CHATSERVER_LISTEN_ADDRESS=':8765'
exec chatserver
stderr 'listening for connections on :8765'
stderr 'listening for connections on .+:8765'
2 changes: 1 addition & 1 deletion testdata/script/listen-address.txtar
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
exec chatserver --listen-address :5678
stderr 'listening for connections on :5678'
stderr 'listening for connections on .+:5678'

0 comments on commit 41c50a7

Please sign in to comment.