From 12db23d07655a0ac9e4e3a191dbbefe45451deb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Wed, 30 Nov 2022 23:46:28 +0100 Subject: [PATCH] Improve test performance (better waiting logic until http server is up) --- server/go.mod | 2 +- server/go.sum | 6 ++---- server/logic/application.go | 16 +++++++++++++--- server/test/util/webserver.go | 8 ++++++-- 4 files changed, 22 insertions(+), 10 deletions(-) diff --git a/server/go.mod b/server/go.mod index 546d152..1dde241 100644 --- a/server/go.mod +++ b/server/go.mod @@ -8,7 +8,7 @@ require ( github.com/mattn/go-sqlite3 v1.14.16 github.com/rs/zerolog v1.28.0 github.com/swaggo/swag v1.8.7 - gogs.mikescher.com/BlackForestBytes/goext v0.0.22 + gogs.mikescher.com/BlackForestBytes/goext v0.0.27 ) require ( diff --git a/server/go.sum b/server/go.sum index ee6848b..f386ffd 100644 --- a/server/go.sum +++ b/server/go.sum @@ -94,10 +94,8 @@ github.com/swaggo/swag v1.8.7/go.mod h1:ezQVUUhly8dludpVk+/PuwJWvLLanB13ygV5Pr9e github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M= github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0= github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY= -gogs.mikescher.com/BlackForestBytes/goext v0.0.21 h1:OibsssmorZsTdFYRiQFlkXtjUYweQg9SBkWO40ONe0Y= -gogs.mikescher.com/BlackForestBytes/goext v0.0.21/go.mod h1:TMBOjo3FRFh/GiTT0z3nwLmgcFJB87oSF2VMs4XUCTQ= -gogs.mikescher.com/BlackForestBytes/goext v0.0.22 h1:D+49BDPz+2BbRwUePilIDqUsWNZIfXJKqX7yGL2b6+Q= -gogs.mikescher.com/BlackForestBytes/goext v0.0.22/go.mod h1:TMBOjo3FRFh/GiTT0z3nwLmgcFJB87oSF2VMs4XUCTQ= +gogs.mikescher.com/BlackForestBytes/goext v0.0.27 h1:Psjv/EGFI2smJoRv+1yUccmms8szmLIN0r1th9JQcik= +gogs.mikescher.com/BlackForestBytes/goext v0.0.27/go.mod h1:TMBOjo3FRFh/GiTT0z3nwLmgcFJB87oSF2VMs4XUCTQ= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI= golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s= diff --git a/server/logic/application.go b/server/logic/application.go index 9010ad2..4f622b4 100644 --- a/server/logic/application.go +++ b/server/logic/application.go @@ -13,6 +13,7 @@ import ( "github.com/gin-gonic/gin/binding" "github.com/rs/zerolog/log" "gogs.mikescher.com/BlackForestBytes/goext/langext" + "gogs.mikescher.com/BlackForestBytes/goext/syncext" "math/rand" "net" "net/http" @@ -33,12 +34,14 @@ type Application struct { Jobs []Job stopChan chan bool Port string + IsRunning *syncext.AtomicBool } func NewApp(db *db.Database) *Application { return &Application{ - Database: db, - stopChan: make(chan bool, 8), + Database: db, + stopChan: make(chan bool), + IsRunning: syncext.NewAtomicBool(false), } } @@ -51,7 +54,10 @@ func (app *Application) Init(cfg scn.Config, g *gin.Engine, fb push.Notification } func (app *Application) Stop() { - app.stopChan <- true + // non-blocking send + select { + case app.stopChan <- true: + } } func (app *Application) Run() { @@ -80,6 +86,8 @@ func (app *Application) Run() { app.Port = port + app.IsRunning.Set(true) // the net.Listener a few lines above is at this point actually already buffering requests + errChan <- httpserver.Serve(ln) }() @@ -126,6 +134,8 @@ func (app *Application) Run() { for _, job := range app.Jobs { job.Stop() } + + app.IsRunning.Set(false) } func (app *Application) GenerateRandomAuthKey() string { diff --git a/server/test/util/webserver.go b/server/test/util/webserver.go index fc19800..f0e4903 100644 --- a/server/test/util/webserver.go +++ b/server/test/util/webserver.go @@ -82,10 +82,14 @@ func StartSimpleWebserver(t *testing.T) (*logic.Application, func()) { router.Init(ginengine) - stop := func() { app.Stop(); _ = os.Remove(dbfile) } + stop := func() { app.Stop(); _ = os.Remove(dbfile); _ = app.IsRunning.WaitWithTimeout(400*time.Millisecond, false) } + go func() { app.Run() }() - time.Sleep(100 * time.Millisecond) // wait until http server is up + err = app.IsRunning.WaitWithTimeout(100*time.Millisecond, true) + if err != nil { + TestFailErr(t, err) + } return app, stop }