Skip to content

Commit

Permalink
Fix TestSendParallel by using only a single DB connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikescher committed Dec 20, 2022
1 parent 2fd710d commit 921fd5c
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 9 deletions.
2 changes: 2 additions & 0 deletions server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

- deploy

- diff my currently used scnsend script vs the one in the docs here

-------------------------------------------------------------------------------------------------------------------------------

- in my script: use (backupname || hostname) for sendername
Expand Down
4 changes: 3 additions & 1 deletion server/common/ginext/gin.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ func NewEngine(cfg scn.Config) *gin.Engine {
engine.RedirectFixedPath = false
engine.RedirectTrailingSlash = false

engine.Use(CorsMiddleware())
if cfg.Cors {
engine.Use(CorsMiddleware())
}

if cfg.GinDebug {
ginlogger := gin.Logger()
Expand Down
16 changes: 14 additions & 2 deletions server/common/ginresp/resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ func (j dataHTTPResponse) Write(g *gin.Context) {
g.Data(j.statusCode, j.contentType, j.data)
}

type errorHTTPResponse struct {
statusCode int
data any
error error
}

func (j errorHTTPResponse) Write(g *gin.Context) {
g.JSON(j.statusCode, j.data)
}

func Status(sc int) HTTPResponse {
return &emptyHTTPResponse{statusCode: sc}
}
Expand Down Expand Up @@ -98,7 +108,7 @@ func createApiError(g *gin.Context, ident string, status int, errorid apierr.API
Msg(fmt.Sprintf("[%s] %s", ident, msg))

if scn.Conf.ReturnRawErrors {
return &jsonHTTPResponse{
return &errorHTTPResponse{
statusCode: status,
data: apiError{
Success: false,
Expand All @@ -108,16 +118,18 @@ func createApiError(g *gin.Context, ident string, status int, errorid apierr.API
RawError: langext.Ptr(langext.Conditional(e == nil, "", fmt.Sprintf("%+v", e))),
Trace: string(debug.Stack()),
},
error: e,
}
} else {
return &jsonHTTPResponse{
return &errorHTTPResponse{
statusCode: status,
data: apiError{
Success: false,
Error: int(errorid),
ErrorHighlight: int(highlight),
Message: msg,
},
error: e,
}
}
}
Expand Down
12 changes: 12 additions & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Config struct {
DBConnMaxLifetime time.Duration `env:"SCN_DB_CONNEXTIONMAXLIFETIME"`
DBConnMaxIdleTime time.Duration `env:"SCN_DB_CONNEXTIONMAXIDLETIME"`
DBCheckForeignKeys bool `env:"SCN_DB_CHECKFOREIGNKEYS"`
DBSingleConn bool `env:"SCN_DB_SINGLECONNECTION"`
RequestTimeout time.Duration `env:"SCN_REQUEST_TIMEOUT"`
ReturnRawErrors bool `env:"SCN_ERROR_RETURN"`
DummyFirebase bool `env:"SCN_DUMMY_FB"`
Expand All @@ -39,6 +40,7 @@ type Config struct {
GoogleAPIPrivateKey string `env:"SCN_GOOG_PRIVATEKEY"`
GooglePackageName string `env:"SCN_GOOG_PACKAGENAME"`
GoogleProProductID string `env:"SCN_GOOG_PROPRODUCTID"`
Cors bool `env:"SCN_CORS"`
}

var Conf Config
Expand All @@ -55,6 +57,7 @@ var configLocHost = func() Config {
DBJournal: "WAL",
DBTimeout: 5 * time.Second,
DBCheckForeignKeys: false,
DBSingleConn: true,
DBMaxOpenConns: 5,
DBMaxIdleConns: 5,
DBConnMaxLifetime: 60 * time.Minute,
Expand All @@ -74,6 +77,7 @@ var configLocHost = func() Config {
GoogleAPIPrivateKey: "",
GooglePackageName: "",
GoogleProProductID: "",
Cors: true,
}
}

Expand All @@ -89,6 +93,7 @@ var configLocDocker = func() Config {
DBJournal: "WAL",
DBTimeout: 5 * time.Second,
DBCheckForeignKeys: false,
DBSingleConn: true,
DBMaxOpenConns: 5,
DBMaxIdleConns: 5,
DBConnMaxLifetime: 60 * time.Minute,
Expand All @@ -108,6 +113,7 @@ var configLocDocker = func() Config {
GoogleAPIPrivateKey: "",
GooglePackageName: "",
GoogleProProductID: "",
Cors: true,
}
}

Expand All @@ -123,6 +129,7 @@ var configDev = func() Config {
DBJournal: "WAL",
DBTimeout: 5 * time.Second,
DBCheckForeignKeys: false,
DBSingleConn: true,
DBMaxOpenConns: 5,
DBMaxIdleConns: 5,
DBConnMaxLifetime: 60 * time.Minute,
Expand All @@ -142,6 +149,7 @@ var configDev = func() Config {
GoogleAPIPrivateKey: confEnv("SCN_GOOG_PRIVATEKEY"),
GooglePackageName: confEnv("SCN_GOOG_PACKAGENAME"),
GoogleProProductID: confEnv("SCN_GOOG_PROPRODUCTID"),
Cors: true,
}
}

Expand All @@ -157,6 +165,7 @@ var configStag = func() Config {
DBJournal: "WAL",
DBTimeout: 5 * time.Second,
DBCheckForeignKeys: false,
DBSingleConn: true,
DBMaxOpenConns: 5,
DBMaxIdleConns: 5,
DBConnMaxLifetime: 60 * time.Minute,
Expand All @@ -176,6 +185,7 @@ var configStag = func() Config {
GoogleAPIPrivateKey: confEnv("SCN_GOOG_PRIVATEKEY"),
GooglePackageName: confEnv("SCN_GOOG_PACKAGENAME"),
GoogleProProductID: confEnv("SCN_GOOG_PROPRODUCTID"),
Cors: true,
}
}

Expand All @@ -191,6 +201,7 @@ var configProd = func() Config {
DBJournal: "WAL",
DBTimeout: 5 * time.Second,
DBCheckForeignKeys: false,
DBSingleConn: true,
DBMaxOpenConns: 5,
DBMaxIdleConns: 5,
DBConnMaxLifetime: 60 * time.Minute,
Expand All @@ -210,6 +221,7 @@ var configProd = func() Config {
GoogleAPIPrivateKey: confEnv("SCN_SCN_GOOG_PRIVATEKEY"),
GooglePackageName: confEnv("SCN_SCN_GOOG_PACKAGENAME"),
GoogleProProductID: confEnv("SCN_SCN_GOOG_PROPRODUCTID"),
Cors: true,
}
}

Expand Down
12 changes: 8 additions & 4 deletions server/db/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ func NewDatabase(conf server.Config) (*Database, error) {
return nil, err
}

xdb.SetMaxOpenConns(5)
xdb.SetMaxIdleConns(5)
xdb.SetConnMaxLifetime(60 * time.Minute)
xdb.SetConnMaxIdleTime(60 * time.Minute)
if conf.DBSingleConn {
xdb.SetMaxOpenConns(1)
} else {
xdb.SetMaxOpenConns(5)
xdb.SetMaxIdleConns(5)
xdb.SetConnMaxLifetime(60 * time.Minute)
xdb.SetConnMaxIdleTime(60 * time.Minute)
}

qqdb := sq.NewDB(xdb)

Expand Down
2 changes: 0 additions & 2 deletions server/test/send_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1429,8 +1429,6 @@ func TestQuotaExceededPro(t *testing.T) {
}

func TestSendParallel(t *testing.T) {
t.SkipNow()

_, baseUrl, stop := tt.StartSimpleWebserver(t)
defer stop()

Expand Down
1 change: 1 addition & 0 deletions server/test/util/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ func StartSimpleWebserver(t *testing.T) (*logic.Application, string, func()) {
RequestTimeout: 30 * time.Second,
ReturnRawErrors: true,
DummyFirebase: true,
DBSingleConn: true,
}

sqlite, err := db.NewDatabase(conf)
Expand Down

0 comments on commit 921fd5c

Please sign in to comment.