Skip to content

Commit

Permalink
Merge pull request #273 from twitchdev/fixing-test-errors
Browse files Browse the repository at this point in the history
Fixing test errors
  • Loading branch information
Xemdo authored Sep 12, 2023
2 parents ad81eb2 + 10faf6c commit 100d3b0
Show file tree
Hide file tree
Showing 16 changed files with 27 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ jobs:
- name: Checkout code
uses: actions/checkout@v3
- name: Test
run: go test ./...
run: go test ./... -timeout=120s
16 changes: 11 additions & 5 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ 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
}

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
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/events/trigger/retrigger_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/events/trigger/trigger_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/login/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion internal/mock_api/authentication/authentication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/mock_api/endpoints/channels/channels_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion internal/mock_api/endpoints/clips/clips_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion internal/mock_api/endpoints/drops/drops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/mock_api/endpoints/schedule/scehdule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/mock_api/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion internal/mock_api/mock_server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
Expand Down
4 changes: 2 additions & 2 deletions internal/mock_auth/mock_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test_setup/test_server/test_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 100d3b0

Please sign in to comment.