This is the main entrypoint of my app and I would like to test it. However, given that my Go knowledge is a few days old, I am struggling to write a test for it. Technically it boots up http server and shuts down gracefully if signal is received. How do I test this please?
Thanks
func main() {
srv := http.NewServer()
log.Print("app starting")
shutChan := make(chan struct{})
signChan := make(chan os.Signal, 1)
signal.Notify(signChan, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
ctx, cancel := context.WithCancel(context.Background())
go listen(cancel, signChan)
// This calls ListenAndServe behind the scene
if err := srv.Start(ctx); err != nil {
panic(err)
}
<-shutChan
log.Print("app shutdown")
}
func listen(cancel context.CancelFunc, signChan chan os.Signal) {
sig := <-signChan
log.Printf("%v signal received", sig)
cancel()
}
Do you mean testing with a unit test called with go test ? It will be possible only if one can generate a SIGINT or SIGTERM signal by code. I don’t know if this is possible (signChan<- Beside, one can’t test a main function with go test. To do so you would have to encapsulate your code into functions that you can then test.
For these reasons, I would test this code manually by running it and type ctrl-C.
By the way, I could not find the method Start() in the http documentation or in the source. Normally, you should call srv.Shutdown(ctx) in your listen function and the given context is typically a timeout context because Shutdown will block until all connections have returned to idle. It’s not impossible that this may block forever.
Note also that os.Interrupt is equal to syscall.SIGINT (defined in os/exec_posix.go).
Yes, I meant to say I wanted to test with go test command. Currently I am testing manually as you suggested and the whole thing works fine. Since I am a learner I didn’t know how to simulate manual test in unit test.
The Start() function actually triggers ListenAndServe that comes as part of http.NewServer() at the very beginning of the code so it is a wrapper. You can see it in here which is what you answered previously.
I still need to somehow test something with cmd/api/main.go though. If you are wondering what my app looks like it is below. I am a learner so open for any suggestions for improvements.