2022-11-30 13:57:55 +01:00
|
|
|
package util
|
|
|
|
|
|
|
|
import (
|
|
|
|
scn "blackforestbytes.com/simplecloudnotifier"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/api"
|
2022-12-20 13:55:09 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/api/ginext"
|
2022-11-30 13:57:55 +01:00
|
|
|
"blackforestbytes.com/simplecloudnotifier/db"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/google"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/jobs"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/logic"
|
|
|
|
"blackforestbytes.com/simplecloudnotifier/push"
|
|
|
|
"gogs.mikescher.com/BlackForestBytes/goext/langext"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Void = struct{}
|
|
|
|
|
2022-12-14 17:02:18 +01:00
|
|
|
func StartSimpleWebserver(t *testing.T) (*logic.Application, string, func()) {
|
2022-11-30 22:59:33 +01:00
|
|
|
InitTests()
|
2022-11-30 13:57:55 +01:00
|
|
|
|
|
|
|
uuid2, _ := langext.NewHexUUID()
|
|
|
|
dbdir := t.TempDir()
|
|
|
|
dbfile := filepath.Join(dbdir, uuid2+".sqlite3")
|
|
|
|
|
|
|
|
err := os.MkdirAll(dbdir, os.ModePerm)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Create(dbfile)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
err = f.Close()
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Chmod(dbfile, 0777)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
2022-12-09 00:40:50 +01:00
|
|
|
TPrintln("DatabaseFile: " + dbfile)
|
2022-11-30 13:57:55 +01:00
|
|
|
|
|
|
|
conf := scn.Config{
|
2022-12-07 22:11:44 +01:00
|
|
|
Namespace: "test",
|
|
|
|
GinDebug: true,
|
|
|
|
ServerIP: "0.0.0.0",
|
|
|
|
ServerPort: "0", // simply choose a free port
|
|
|
|
DBFile: dbfile,
|
|
|
|
DBJournal: "WAL",
|
|
|
|
DBTimeout: 500 * time.Millisecond,
|
|
|
|
DBMaxOpenConns: 2,
|
|
|
|
DBMaxIdleConns: 2,
|
|
|
|
DBConnMaxLifetime: 1 * time.Second,
|
|
|
|
DBConnMaxIdleTime: 1 * time.Second,
|
|
|
|
RequestTimeout: 30 * time.Second,
|
2022-12-20 09:52:33 +01:00
|
|
|
RequestMaxRetry: 32,
|
|
|
|
RequestRetrySleep: 100 * time.Millisecond,
|
2022-12-07 22:11:44 +01:00
|
|
|
ReturnRawErrors: true,
|
|
|
|
DummyFirebase: true,
|
2022-12-20 09:52:33 +01:00
|
|
|
DBSingleConn: false,
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|
|
|
|
|
2022-12-20 09:52:33 +01:00
|
|
|
scn.Conf = conf
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
sqlite, err := db.NewDatabase(conf)
|
2022-11-30 13:57:55 +01:00
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
app := logic.NewApp(sqlite)
|
|
|
|
|
|
|
|
if err := app.Migrate(); err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ginengine := ginext.NewEngine(conf)
|
|
|
|
|
|
|
|
router := api.NewRouter(app)
|
|
|
|
|
|
|
|
nc := push.NewTestSink()
|
|
|
|
|
|
|
|
apc := google.NewDummy()
|
|
|
|
|
|
|
|
jobRetry := jobs.NewDeliveryRetryJob(app)
|
|
|
|
app.Init(conf, ginengine, nc, apc, []logic.Job{jobRetry})
|
|
|
|
|
|
|
|
router.Init(ginengine)
|
|
|
|
|
2022-12-07 22:11:44 +01:00
|
|
|
stop := func() {
|
|
|
|
app.Stop()
|
|
|
|
_ = os.Remove(dbfile)
|
|
|
|
_ = app.IsRunning.WaitWithTimeout(400*time.Millisecond, false)
|
|
|
|
}
|
2022-11-30 23:46:28 +01:00
|
|
|
|
2022-11-30 13:57:55 +01:00
|
|
|
go func() { app.Run() }()
|
2022-11-30 21:39:14 +01:00
|
|
|
|
2022-11-30 23:46:28 +01:00
|
|
|
err = app.IsRunning.WaitWithTimeout(100*time.Millisecond, true)
|
|
|
|
if err != nil {
|
|
|
|
TestFailErr(t, err)
|
|
|
|
}
|
2022-11-30 21:39:14 +01:00
|
|
|
|
2022-12-14 17:02:18 +01:00
|
|
|
return app, "http://127.0.0.1:" + app.Port, stop
|
2022-11-30 13:57:55 +01:00
|
|
|
}
|