Help with go coverage #1924
-
HI Guys, First of all thanks for a great tool. I am playing with samples-go/gin-redis I followed all the steps both in the app and integrating. Link and Link Regardless what I do, I am seeing 0% coverage. I would expect at least a non zero percentage. Following is the report. Is there a working sample that I can use it bring up the tool? Primary goal is to mock with real test case and generate a code coverage report. thanks Steps:
Generates report like this:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Hey @srinikom, the go tool requires graceful shutdown in the application because it calculates the coverage after receiving the sigint signal. That was missing in the application and that is why we are getting 0 coverage. You can try using this in the server/server.go file, and it should give you the coverage. package server
import (
"time"
"github.com/keploy/gin-redis/routes"
"os"
"context"
"fmt"
"net/http"
"os/signal"
"syscall"
)
func Init() {
r := routes.NewRouter()
port := "3001"
srv := &http.Server{
Addr: ":" + port,
Handler: r,
}
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
fmt.Printf("listen: %s\n", err)
}
}()
GracefulShutdown(srv)
}
func GracefulShutdown(srv *http.Server) {
stopper := make(chan os.Signal, 1)
// listens for interrupt and SIGTERM signal
signal.Notify(stopper, syscall.SIGINT, syscall.SIGTERM)
<-stopper
fmt.Println("Shutting down gracefully...")
// Create a deadline for the graceful shutdown
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Attempt graceful shutdown
if err := srv.Shutdown(ctx); err != nil {
fmt.Printf("Server forced to shutdown: %v\n", err)
}
fmt.Println("Server exiting")
} |
Beta Was this translation helpful? Give feedback.
Hey @srinikom, the go tool requires graceful shutdown in the application because it calculates the coverage after receiving the sigint signal. That was missing in the application and that is why we are getting 0 coverage. You can try using this in the server/server.go file, and it should give you the coverage.