Improve test performance (better waiting logic until http server is up)

This commit is contained in:
Mike Schwörer 2022-11-30 23:46:28 +01:00
parent fd182f0abb
commit 12db23d076
Signed by: Mikescher
GPG Key ID: D3C7172E0A70F8CF
4 changed files with 22 additions and 10 deletions

View File

@ -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 (

View File

@ -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=

View File

@ -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 {

View File

@ -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
}