test: Add metrics unit test

This commit is contained in:
Anthony Laibe 2021-10-18 14:43:17 +02:00
parent 4227a9d69d
commit 990bbc50dc
3 changed files with 29 additions and 4 deletions

View File

@ -69,13 +69,16 @@ func NewMetricsServer(address string, port int) *Server {
// Start executes the HTTP server in the background. // Start executes the HTTP server in the background.
func (p *Server) Start() { func (p *Server) Start() {
log.Info("Server stopped ", p.server.ListenAndServe()) log.Info("server stopped ", p.server.ListenAndServe())
} }
// Stop shuts down the prometheus server // Stop shuts down the prometheus server
func (p *Server) Stop(ctx context.Context) { func (p *Server) Stop(ctx context.Context) error {
err := p.server.Shutdown(ctx) err := p.server.Shutdown(ctx)
if err != nil { if err != nil {
log.Error("Server shutdown err", err) log.Error("error while stopping server", err)
return err
} }
return nil
} }

21
waku/metrics/http_test.go Normal file
View File

@ -0,0 +1,21 @@
package metrics
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func TestStartAndStopMetricsServer(t *testing.T) {
server := NewMetricsServer("0.0.0.0", 9876)
go func() {
time.Sleep(1 * time.Second)
err := server.Stop(context.Background())
require.NoError(t, err)
}()
server.Start()
}

View File

@ -233,7 +233,8 @@ func Execute(options Options) {
wakuNode.Stop() wakuNode.Stop()
if options.Metrics.Enable { if options.Metrics.Enable {
metricsServer.Stop(ctx) err = metricsServer.Stop(ctx)
failOnErr(err, "MetricsClose")
} }
if options.UseDB { if options.UseDB {