diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 31c17707..c4f89c93 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -15,4 +15,4 @@ jobs: - name: Checkout code uses: actions/checkout@v3 - name: Test - run: go test ./... + run: go test ./... -timeout=120s diff --git a/internal/database/database.go b/internal/database/database.go index 732a7f93..c887332c 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -21,8 +21,8 @@ type CLIDatabase struct { DB *sqlx.DB } -func NewConnection() (CLIDatabase, error) { - db, err := getDatabase() +func NewConnection(extendedBusyTimeout bool) (CLIDatabase, error) { + db, err := getDatabase(extendedBusyTimeout) if err != nil { return CLIDatabase{}, err } @@ -30,7 +30,8 @@ func NewConnection() (CLIDatabase, error) { return CLIDatabase{DB: &db}, nil } -func getDatabase() (sqlx.DB, error) { +// 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. +func getDatabase(extendedBusyTimeout bool) (sqlx.DB, error) { home, err := util.GetApplicationDir() if err != nil { return sqlx.DB{}, err @@ -46,9 +47,14 @@ func getDatabase() (sqlx.DB, error) { needToInit = true } + // force Foreign Key support ("fk=true") + dbFlags := "?_fk=true&cache=shared" + if extendedBusyTimeout { + // https://www.sqlite.org/c3ref/busy_timeout.html + dbFlags += "&_busy_timeout=60000" + } for i := 0; i <= 5; i++ { - // open and force Foreign Key support ("fk=true") - db, err := sqlx.Open("sqlite3", path+"?_fk=true&cache=shared") + db, err := sqlx.Open("sqlite3", path+dbFlags) if err != nil { log.Print(i) if i == 5 { diff --git a/internal/database/database_test.go b/internal/database/database_test.go index 50cfb4b5..059e81a4 100644 --- a/internal/database/database_test.go +++ b/internal/database/database_test.go @@ -46,7 +46,7 @@ func TestMain(m *testing.M) { log.Fatal(err) } - db, err = NewConnection() + db, err = NewConnection(true) if err != nil { log.Print(err) } diff --git a/internal/events/trigger/retrigger_event.go b/internal/events/trigger/retrigger_event.go index 847bcf54..1f063119 100644 --- a/internal/events/trigger/retrigger_event.go +++ b/internal/events/trigger/retrigger_event.go @@ -12,7 +12,7 @@ import ( ) func RefireEvent(id string, p TriggerParameters) (string, error) { - db, err := database.NewConnection() + db, err := database.NewConnection(false) if err != nil { return "", err } diff --git a/internal/events/trigger/trigger_event.go b/internal/events/trigger/trigger_event.go index a57dfa34..75eba8ba 100644 --- a/internal/events/trigger/trigger_event.go +++ b/internal/events/trigger/trigger_event.go @@ -156,7 +156,7 @@ https://dev.twitch.tv/docs/eventsub/handling-webhook-events#processing-an-event` return "", err } - db, err := database.NewConnection() + db, err := database.NewConnection(false) if err != nil { return "", err } diff --git a/internal/login/login_test.go b/internal/login/login_test.go index 5bd02c10..1f67b8a5 100644 --- a/internal/login/login_test.go +++ b/internal/login/login_test.go @@ -151,7 +151,7 @@ func TestUserAuthServer(t *testing.T) { userResponse <- *res }() - time.Sleep(25) + time.Sleep(1 * time.Second) _, err = loginRequest(http.MethodGet, fmt.Sprintf("http://localhost:3000?code=%s&state=%s", code, state), nil) a.Nil(err, err) diff --git a/internal/mock_api/authentication/authentication_test.go b/internal/mock_api/authentication/authentication_test.go index 8a94cb04..23eaa46a 100644 --- a/internal/mock_api/authentication/authentication_test.go +++ b/internal/mock_api/authentication/authentication_test.go @@ -94,7 +94,7 @@ func baseMiddleware(next http.Handler) http.Handler { ctx := context.Background() // just stub it all - db, err := database.NewConnection() + db, err := database.NewConnection(false) if err != nil { log.Fatalf("Error connecting to database: %v", err.Error()) return diff --git a/internal/mock_api/endpoints/channel_points/channel_points_test.go b/internal/mock_api/endpoints/channel_points/channel_points_test.go index 99c84fa1..878fe05b 100644 --- a/internal/mock_api/endpoints/channel_points/channel_points_test.go +++ b/internal/mock_api/endpoints/channel_points/channel_points_test.go @@ -29,7 +29,7 @@ var ( func TestMain(m *testing.M) { test_setup.SetupTestEnv(&testing.T{}) - db, err := database.NewConnection() + db, err := database.NewConnection(true) if err != nil { log.Fatal(err) } diff --git a/internal/mock_api/endpoints/channels/channels_test.go b/internal/mock_api/endpoints/channels/channels_test.go index 1fe80293..f2351ae3 100644 --- a/internal/mock_api/endpoints/channels/channels_test.go +++ b/internal/mock_api/endpoints/channels/channels_test.go @@ -19,7 +19,7 @@ func TestMain(m *testing.M) { test_setup.SetupTestEnv(&testing.T{}) // adding mock data - db, _ := database.NewConnection() + db, _ := database.NewConnection(true) q := db.NewQuery(nil, 100) q.InsertStream(database.Stream{ID: util.RandomGUID(), UserID: "1", StreamType: "live", ViewerCount: 0}, false) db.DB.Close() diff --git a/internal/mock_api/endpoints/clips/clips_test.go b/internal/mock_api/endpoints/clips/clips_test.go index 8f6abb58..1316722e 100644 --- a/internal/mock_api/endpoints/clips/clips_test.go +++ b/internal/mock_api/endpoints/clips/clips_test.go @@ -18,7 +18,7 @@ func TestMain(m *testing.M) { test_setup.SetupTestEnv(&testing.T{}) // adding mock data - db, _ := database.NewConnection() + db, _ := database.NewConnection(true) q := db.NewQuery(nil, 100) q.InsertStream(database.Stream{ID: util.RandomGUID(), UserID: "1", StreamType: "live", ViewerCount: 0}, false) db.DB.Close() diff --git a/internal/mock_api/endpoints/drops/drops_test.go b/internal/mock_api/endpoints/drops/drops_test.go index 69d3edfe..98943a24 100644 --- a/internal/mock_api/endpoints/drops/drops_test.go +++ b/internal/mock_api/endpoints/drops/drops_test.go @@ -22,7 +22,7 @@ var entitlement database.DropsEntitlement func TestMain(m *testing.M) { test_setup.SetupTestEnv(&testing.T{}) - db, err := database.NewConnection() + db, err := database.NewConnection(true) if err != nil { log.Fatal(err) } diff --git a/internal/mock_api/endpoints/schedule/scehdule_test.go b/internal/mock_api/endpoints/schedule/scehdule_test.go index 5f9b54cf..fc3928f3 100644 --- a/internal/mock_api/endpoints/schedule/scehdule_test.go +++ b/internal/mock_api/endpoints/schedule/scehdule_test.go @@ -28,7 +28,7 @@ var ( func TestMain(m *testing.M) { test_setup.SetupTestEnv(&testing.T{}) - db, err := database.NewConnection() + db, err := database.NewConnection(true) if err != nil { log.Fatal(err) } diff --git a/internal/mock_api/generate/generate.go b/internal/mock_api/generate/generate.go index eac2a1a9..c81aab71 100644 --- a/internal/mock_api/generate/generate.go +++ b/internal/mock_api/generate/generate.go @@ -28,7 +28,7 @@ type UserInfo struct { var f = false func Generate(userCount int) error { - db, err := database.NewConnection() + db, err := database.NewConnection(false) if err != nil { return err } diff --git a/internal/mock_api/mock_server/server.go b/internal/mock_api/mock_server/server.go index e26573bd..8a3a6c57 100644 --- a/internal/mock_api/mock_server/server.go +++ b/internal/mock_api/mock_server/server.go @@ -33,7 +33,7 @@ func StartServer(port int) error { ctx := context.Background() - db, err := database.NewConnection() + db, err := database.NewConnection(false) if err != nil { return fmt.Errorf("Error connecting to database: %v", err.Error()) } diff --git a/internal/mock_auth/mock_auth_test.go b/internal/mock_auth/mock_auth_test.go index 2b7c14f3..e6f72d4e 100644 --- a/internal/mock_auth/mock_auth_test.go +++ b/internal/mock_auth/mock_auth_test.go @@ -91,7 +91,7 @@ func TestValidateToken(t *testing.T) { a.Nil(err, err) a.Equal(401, resp.StatusCode) - db, err := database.NewConnection() + db, err := database.NewConnection(true) a.Nil(err, err) defer db.DB.Close() @@ -152,7 +152,7 @@ func baseMiddleware(next http.Handler) http.Handler { ctx := context.Background() // just stub it all - db, err := database.NewConnection() + db, err := database.NewConnection(true) if err != nil { log.Fatalf("Error connecting to database: %v", err.Error()) return diff --git a/test_setup/test_server/test_server.go b/test_setup/test_server/test_server.go index d32c296d..098cbcd0 100644 --- a/test_setup/test_server/test_server.go +++ b/test_setup/test_server/test_server.go @@ -18,7 +18,7 @@ func SetupTestServer(next mock_api.MockEndpoint) *httptest.Server { ctx := context.Background() // just stub it all - db, err := database.NewConnection() + db, err := database.NewConnection(true) if err != nil { log.Fatalf("Error connecting to database: %v", err.Error()) return