Skip to content

Commit

Permalink
Update test-script tests to run the chat-server in the background and…
Browse files Browse the repository at this point in the history
… kill it when tests are satisfied, making `processCLIArgsAndRunServer` and `RunCLIWithoutWaitingForExit` no longer necessary
  • Loading branch information
ivanfetch committed Sep 29, 2023
1 parent 7be282f commit b91bc66
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 36 deletions.
14 changes: 6 additions & 8 deletions chatserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,15 +279,12 @@ func WithProfiling() ServerOption {
// optional parameters can be specified via With*()
// functional options.
func NewServer(options ...ServerOption) (*Server, error) {
openForBusiness, stopReceivingSignals := signal.NotifyContext(context.Background(), os.Interrupt)
s := &Server{
listenAddress: ":0",
openForBusiness: openForBusiness,
stopReceivingSignals: stopReceivingSignals,
addConnCh: make(chan *connection),
removeConnCh: make(chan *connection),
addMessageCh: make(chan message),
exitWG: &sync.WaitGroup{},
listenAddress: ":0",
addConnCh: make(chan *connection),
removeConnCh: make(chan *connection),
addMessageCh: make(chan message),
exitWG: &sync.WaitGroup{},
}
s.createLog()
for _, option := range options {
Expand Down Expand Up @@ -469,6 +466,7 @@ func (s *Server) ListenAndServe() error {
return fmt.Errorf("cannot listen on %s: %v", s.listenAddress, err)
}
s.log.Infof("listening for connections on %s - press CTRL-c to stop the chat server", s.GetListenAddress())
s.openForBusiness, s.stopReceivingSignals = signal.NotifyContext(context.Background(), os.Interrupt)
s.startConnectionAccepter()
s.startConnectionAndMessageManager()
return nil
Expand Down
25 changes: 3 additions & 22 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
)

// RunCLI processes command-line arguments, instantiates a new chat server,
// calls ListenAndServe, optionally waits for chat server routines to exit,
// calls ListenAndServe, waits for chat server routines to exit,
// then returns an exit status code.
func processCLIArgsAndRunServer(waitForExit bool) int {
func RunCLI() int {
server, err := NewServerFromArgs(os.Args[1:])
if err != nil {
fmt.Println(err)
Expand All @@ -27,29 +27,10 @@ func processCLIArgsAndRunServer(waitForExit bool) int {
fmt.Println(err)
return 1
}
if waitForExit {
server.WaitForExit()
}
server.WaitForExit()
return 0
}

// RunCLI processes command-line arguments, instantiates a new chat server, calls ListenAndServe,
// waits for the chat server routines to cleanup and exit, then returns an
// exit status code.
func RunCLI() int {
return processCLIArgsAndRunServer(true)
}

// RunCLIWithoutWaitingForExit processes command-line arguments, instantiates
// a new chat server, calls ListenAndServe, then returns an exit status code
// without waiting for the chat
// server routines to exit.
// This is useful for Go TestScript tests, which can avoid retaining and
// calling cleanup methods on the chat server.
func RunCLIWithoutWaitingForExit() int {
return processCLIArgsAndRunServer(false)
}

// NewServerFromArgs returns a type *Server after processing command-line
// arguments.
func NewServerFromArgs(args []string) (*Server, error) {
Expand Down
2 changes: 1 addition & 1 deletion script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var testScriptSetup func(*testscript.Env) error = func(e *testscript.Env) error

func TestMain(m *testing.M) {
os.Exit(testscript.RunMain(m, map[string]func() int{
"chatserver": chat.RunCLIWithoutWaitingForExit,
"chatserver": chat.RunCLI,
}))
}

Expand Down
3 changes: 2 additions & 1 deletion testdata/script/debug-envvar.txtar
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
env CHATSERVER_DEBUG_LOGGING=1
exec chatserver
! exec chatserver &
stop
stderr 'debug logging enabled'
4 changes: 2 additions & 2 deletions testdata/script/debug.txtar
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
chatserver --debug-logging
! exec chatserver --debug-logging &
stop
stderr 'debug logging enabled'

3 changes: 2 additions & 1 deletion testdata/script/listen-address-envvar.txtar
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
env CHATSERVER_LISTEN_ADDRESS=':8765'
exec chatserver
! exec chatserver &
stop
stderr 'listening for connections on .+:8765'
3 changes: 2 additions & 1 deletion testdata/script/listen-address.txtar
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
exec chatserver --listen-address :5678
! exec chatserver --listen-address :5678 &
stop
stderr 'listening for connections on .+:5678'

0 comments on commit b91bc66

Please sign in to comment.