Skip to content

Commit

Permalink
add web_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
bilalcaliskan committed Sep 11, 2021
1 parent b127654 commit d061229
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 4 deletions.
2 changes: 1 addition & 1 deletion internal/metrics/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ func RunMetricsServer(router *mux.Router) {
ReadTimeout: time.Duration(int32(opts.ReadTimeoutSeconds)) * time.Second,
}
router.Handle("/metrics", promhttp.Handler())
logger.Info("metric server is up and running", zap.Int("port", opts.MetricsPort))
logger.Info("starting metrics server", zap.Int("port", opts.MetricsPort))
panic(metricServer.ListenAndServe())
}
4 changes: 2 additions & 2 deletions internal/metrics/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ func TestRunMetricsServer(t *testing.T) {
ReadTimeout: time.Duration(int32(opts.ReadTimeoutSeconds)) * time.Second,
}
router.Handle("/metrics", promhttp.Handler())
logger.Info("metric server is up and running", zap.Int("port", opts.MetricsPort))
logger.Info("starting metrics server", zap.Int("port", opts.MetricsPort))
errChan <- metricServer.ListenAndServe()
}()

for {
select {
case c := <-errChan:
t.Error(c)
case <-time.After(15 * time.Second):
case <-time.After(10 * time.Second):
t.Log("success")
return
}
Expand Down
2 changes: 1 addition & 1 deletion internal/web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ func RunWebServer(router *mux.Router) {
time.Duration(int32(opts.WriteTimeoutSeconds))*time.Second,
time.Duration(int32(opts.ReadTimeoutSeconds))*time.Second, logger)

logger.Info("web server is up and running", zap.Int("port", opts.ServerPort))
logger.Info("starting web server", zap.Int("port", opts.ServerPort))
panic(webServer.ListenAndServe())
}
33 changes: 33 additions & 0 deletions internal/web/web_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package web

import (
"fmt"
"testing"
"time"

"github.com/gorilla/mux"
"go.uber.org/zap"
)

func TestRunWebServer(t *testing.T) {
errChan := make(chan error, 1)

go func() {
router := mux.NewRouter()
webServer := initServer(router, fmt.Sprintf(":%d", opts.ServerPort),
time.Duration(int32(opts.WriteTimeoutSeconds))*time.Second,
time.Duration(int32(opts.ReadTimeoutSeconds))*time.Second, logger)
logger.Info("starting web server", zap.Int("port", opts.ServerPort))
errChan <- webServer.ListenAndServe()
}()

for {
select {
case c := <-errChan:
t.Error(c)
case <-time.After(10 * time.Second):
t.Log("success")
return
}
}
}

0 comments on commit d061229

Please sign in to comment.