Skip to content

Commit

Permalink
add test + set prepareStmt to true + setMaxConn to 1
Browse files Browse the repository at this point in the history
  • Loading branch information
pascal-fischer committed Jan 22, 2025
1 parent fabf3ff commit 18125b6
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
6 changes: 5 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ func init() {
log.Printf("failed to connect database, got error %v\n", err)
}

sqlDB.SetMaxOpenConns(1)

RunMigrations()
if DB.Dialector.Name() == "sqlite" {
DB.Exec("PRAGMA foreign_keys = ON")
Expand Down Expand Up @@ -69,7 +71,9 @@ func OpenTestConnection() (db *gorm.DB, err error) {
db, err = gorm.Open(sqlserver.Open(dbDSN), &gorm.Config{})
default:
log.Println("testing sqlite3...")
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), &gorm.Config{})
db, err = gorm.Open(sqlite.Open(filepath.Join(os.TempDir(), "gorm.db")), &gorm.Config{
PrepareStmt: true, // set this to false and the test passes
})
}

if debug := os.Getenv("DEBUG"); debug == "true" {
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,9 @@ require (
golang.org/x/crypto v0.29.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/sync v0.9.0 // indirect
golang.org/x/sys v0.27.0 // indirect
golang.org/x/text v0.20.0 // indirect
golang.org/x/tools v0.27.0 // indirect
gorm.io/datatypes v1.2.4 // indirect
gorm.io/hints v1.1.2 // indirect
gorm.io/plugin/dbresolver v1.5.3 // indirect
)

replace gorm.io/gorm => ./gorm
63 changes: 60 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
package main

import (
"context"
"sync"
"testing"
"time"
)

const concurrentReads = 40

// GORM_REPO: https://github.com/go-gorm/gorm.git
// GORM_BRANCH: master
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver
Expand All @@ -13,8 +18,60 @@ func TestGORM(t *testing.T) {

DB.Create(&user)

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
testRunSuccessful := false
wgSuccess := sync.WaitGroup{}
wgSuccess.Add(concurrentReads)

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()

start := make(chan struct{})

for i := 0; i < concurrentReads/2; i++ {
go func() {
t.Logf("Entered routine 1-%d", i)
var result User

<-start
transaction := DB.Begin()
if err := transaction.First(&result, "id = ? ", 1).Error; err != nil {
transaction.Rollback()
return
}
transaction.Commit()
t.Log("Got User from routine 1")
wgSuccess.Done()
}()
}

for i := 0; i < concurrentReads/2; i++ {
go func() {
t.Logf("Entered routine 2-%d", i)
var result User

<-start
if err := DB.First(&result, "id = ? ", 1).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
return
}
t.Log("Got User from routine 2")
wgSuccess.Done()
}()
}

time.Sleep(200 * time.Millisecond)
close(start)
t.Log("Started routines")

go func() {
wgSuccess.Wait()
testRunSuccessful = true
}()

<-ctx.Done()
if !testRunSuccessful {
t.Fatalf("Test failed")
}

t.Logf("Test completed")
}

0 comments on commit 18125b6

Please sign in to comment.