Skip to content

Commit 8efd0bd

Browse files
committed
Extended database open timeout during go test, fixing occasional WSL test issue with database file not closing as quickly as desired
1 parent 9f91556 commit 8efd0bd

File tree

14 files changed

+25
-19
lines changed

14 files changed

+25
-19
lines changed

internal/database/database.go

+11-5
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,17 @@ type CLIDatabase struct {
2121
DB *sqlx.DB
2222
}
2323

24-
func NewConnection() (CLIDatabase, error) {
25-
db, err := getDatabase()
24+
func NewConnection(extendedBusyTimeout bool) (CLIDatabase, error) {
25+
db, err := getDatabase(extendedBusyTimeout)
2626
if err != nil {
2727
return CLIDatabase{}, err
2828
}
2929

3030
return CLIDatabase{DB: &db}, nil
3131
}
3232

33-
func getDatabase() (sqlx.DB, error) {
33+
// extendedBusyTimeout sets an extended timeout for waiting on a busy database. This is mainly an issue in tests on WSL, so this flag shouldn't be used in production.
34+
func getDatabase(extendedBusyTimeout bool) (sqlx.DB, error) {
3435
home, err := util.GetApplicationDir()
3536
if err != nil {
3637
return sqlx.DB{}, err
@@ -46,9 +47,14 @@ func getDatabase() (sqlx.DB, error) {
4647
needToInit = true
4748
}
4849

50+
// force Foreign Key support ("fk=true")
51+
dbFlags := "?_fk=true&cache=shared"
52+
if extendedBusyTimeout {
53+
// https://www.sqlite.org/c3ref/busy_timeout.html
54+
dbFlags += "&_busy_timeout=60000"
55+
}
4956
for i := 0; i <= 5; i++ {
50-
// open and force Foreign Key support ("fk=true")
51-
db, err := sqlx.Open("sqlite3", path+"?_fk=true&cache=shared")
57+
db, err := sqlx.Open("sqlite3", path+dbFlags)
5258
if err != nil {
5359
log.Print(i)
5460
if i == 5 {

internal/database/database_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func TestMain(m *testing.M) {
4646
log.Fatal(err)
4747
}
4848

49-
db, err = NewConnection()
49+
db, err = NewConnection(true)
5050
if err != nil {
5151
log.Print(err)
5252
}

internal/events/trigger/retrigger_event.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
func RefireEvent(id string, p TriggerParameters) (string, error) {
15-
db, err := database.NewConnection()
15+
db, err := database.NewConnection(false)
1616
if err != nil {
1717
return "", err
1818
}

internal/events/trigger/trigger_event.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event`
156156
return "", err
157157
}
158158

159-
db, err := database.NewConnection()
159+
db, err := database.NewConnection(false)
160160
if err != nil {
161161
return "", err
162162
}

internal/mock_api/authentication/authentication_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func baseMiddleware(next http.Handler) http.Handler {
9494
ctx := context.Background()
9595

9696
// just stub it all
97-
db, err := database.NewConnection()
97+
db, err := database.NewConnection(false)
9898
if err != nil {
9999
log.Fatalf("Error connecting to database: %v", err.Error())
100100
return

internal/mock_api/endpoints/channel_points/channel_points_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ var (
2929
func TestMain(m *testing.M) {
3030
test_setup.SetupTestEnv(&testing.T{})
3131

32-
db, err := database.NewConnection()
32+
db, err := database.NewConnection(true)
3333
if err != nil {
3434
log.Fatal(err)
3535
}

internal/mock_api/endpoints/channels/channels_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestMain(m *testing.M) {
1919
test_setup.SetupTestEnv(&testing.T{})
2020

2121
// adding mock data
22-
db, _ := database.NewConnection()
22+
db, _ := database.NewConnection(true)
2323
q := db.NewQuery(nil, 100)
2424
q.InsertStream(database.Stream{ID: util.RandomGUID(), UserID: "1", StreamType: "live", ViewerCount: 0}, false)
2525
db.DB.Close()

internal/mock_api/endpoints/clips/clips_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestMain(m *testing.M) {
1818
test_setup.SetupTestEnv(&testing.T{})
1919

2020
// adding mock data
21-
db, _ := database.NewConnection()
21+
db, _ := database.NewConnection(true)
2222
q := db.NewQuery(nil, 100)
2323
q.InsertStream(database.Stream{ID: util.RandomGUID(), UserID: "1", StreamType: "live", ViewerCount: 0}, false)
2424
db.DB.Close()

internal/mock_api/endpoints/drops/drops_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ var entitlement database.DropsEntitlement
2222
func TestMain(m *testing.M) {
2323
test_setup.SetupTestEnv(&testing.T{})
2424

25-
db, err := database.NewConnection()
25+
db, err := database.NewConnection(true)
2626
if err != nil {
2727
log.Fatal(err)
2828
}

internal/mock_api/endpoints/schedule/scehdule_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ var (
2828
func TestMain(m *testing.M) {
2929
test_setup.SetupTestEnv(&testing.T{})
3030

31-
db, err := database.NewConnection()
31+
db, err := database.NewConnection(true)
3232
if err != nil {
3333
log.Fatal(err)
3434
}

internal/mock_api/generate/generate.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ type UserInfo struct {
2828
var f = false
2929

3030
func Generate(userCount int) error {
31-
db, err := database.NewConnection()
31+
db, err := database.NewConnection(false)
3232
if err != nil {
3333
return err
3434
}

internal/mock_api/mock_server/server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func StartServer(port int) error {
3333

3434
ctx := context.Background()
3535

36-
db, err := database.NewConnection()
36+
db, err := database.NewConnection(false)
3737
if err != nil {
3838
return fmt.Errorf("Error connecting to database: %v", err.Error())
3939
}

internal/mock_auth/mock_auth_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func TestValidateToken(t *testing.T) {
9191
a.Nil(err, err)
9292
a.Equal(401, resp.StatusCode)
9393

94-
db, err := database.NewConnection()
94+
db, err := database.NewConnection(true)
9595
a.Nil(err, err)
9696
defer db.DB.Close()
9797

@@ -152,7 +152,7 @@ func baseMiddleware(next http.Handler) http.Handler {
152152
ctx := context.Background()
153153

154154
// just stub it all
155-
db, err := database.NewConnection()
155+
db, err := database.NewConnection(true)
156156
if err != nil {
157157
log.Fatalf("Error connecting to database: %v", err.Error())
158158
return

test_setup/test_server/test_server.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func SetupTestServer(next mock_api.MockEndpoint) *httptest.Server {
1818
ctx := context.Background()
1919

2020
// just stub it all
21-
db, err := database.NewConnection()
21+
db, err := database.NewConnection(true)
2222
if err != nil {
2323
log.Fatalf("Error connecting to database: %v", err.Error())
2424
return

0 commit comments

Comments
 (0)