diff --git a/database/db_test.go b/database/db_test.go index 275f3ea4..37102df9 100644 --- a/database/db_test.go +++ b/database/db_test.go @@ -1,12 +1,13 @@ package database import ( - "encoding/json" "fmt" "os" "testing" + "time" "github.com/konveyor/tackle2-hub/model" + "gorm.io/gorm" "k8s.io/utils/env" ) @@ -26,25 +27,38 @@ func TestConcurrent(t *testing.T) { for w := 0; w < N; w++ { go func(id int) { fmt.Printf("Started %d\n", id) - for n := 0; n < N; n++ { - v, _ := json.Marshal(fmt.Sprintf("Test-%d", n)) - m := &model.Setting{Key: fmt.Sprintf("key-%d-%d", id, n), Value: v} + for n := 0; n < N*10; n++ { + m := &model.Setting{Key: fmt.Sprintf("key-%d-%d", id, n), Value: n} fmt.Printf("(%.4d) CREATE: %.4d\n", id, n) uErr := db.Create(m).Error if uErr != nil { panic(uErr) } - for i := 0; i < 3; i++ { - tx := db.Begin() - uErr = tx.First(m).Error + uErr = db.Save(m).Error + if uErr != nil { + panic(uErr) + } + for i := 0; i < 10; i++ { + fmt.Printf("(%.4d) READ: %.4d/%.4d\n", id, n, i) + uErr = db.First(m).Error if uErr != nil { panic(uErr) } - uErr = tx.Save(m).Error + } + for i := 0; i < 4; i++ { + uErr = db.Transaction(func(tx *gorm.DB) (err error) { + time.Sleep(time.Millisecond * 10) + for i := 0; i < 3; i++ { + err = tx.Save(m).Error + if err != nil { + break + } + } + return + }) if uErr != nil { panic(uErr) } - tx.Commit() } } dq <- id diff --git a/database/pkg.go b/database/pkg.go index 3e9adfda..b513c1c6 100644 --- a/database/pkg.go +++ b/database/pkg.go @@ -18,7 +18,7 @@ var log = logr.WithName("db") var Settings = &settings.Settings const ( - ConnectionString = "file:%s?_journal=WAL" + ConnectionString = "file:%s?_journal=WAL&_timeout=100" FKsOn = "&_foreign_keys=yes" FKsOff = "&_foreign_keys=no" )