Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Race when concurrently appending m2m association when preloaded model #534

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ go 1.16
require (
github.com/denisenkom/go-mssqldb v0.12.2 // indirect
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
gorm.io/driver/mysql v1.4.1
gorm.io/driver/postgres v1.4.4
gorm.io/driver/sqlite v1.4.2
golang.org/x/crypto v0.1.0 // indirect
gorm.io/driver/mysql v1.4.3
gorm.io/driver/postgres v1.4.5
gorm.io/driver/sqlite v1.4.3
gorm.io/driver/sqlserver v1.4.1
gorm.io/gorm v1.24.0
gorm.io/gorm v1.24.1-0.20221019064659-5dd2bb482755
)

replace gorm.io/gorm => ./gorm
53 changes: 48 additions & 5 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,63 @@
package main

import (
"log"
"strconv"
"sync"
"testing"
"time"

"gorm.io/gorm/clause"
)

const (
count = 30
)

// GORM_REPO: https://github.com/go-gorm/gorm.git
// GORM_BRANCH: master
// TEST_DRIVERS: sqlite, mysql, postgres, sqlserver

func TestGORM(t *testing.T) {
user := User{Name: "jinzhu"}
languages := []Language{}
for i := 0; i < count; i++ {
language := Language{Code: strconv.Itoa(i), Name: strconv.Itoa(i)}
DB.Create(&language)
languages = append(languages, language)
}

DB.Create(&user)
/////////////////////////////////////////////////////////////////
// When this is deleted, the test passes also when preloading.
whenThisIsDeletedTheTestPasses := User{}
DB.Create(&whenThisIsDeletedTheTestPasses)
////////////////////////////////////////////////////////////////

user1 := User{}
if true { // If not preloading, the test passes.
DB.Preload(clause.Associations).FirstOrCreate(&user1)
} else {
DB.FirstOrCreate(&user1)
}

var wg sync.WaitGroup
for _, language := range languages {
wg.Add(1)
go func(userCopy User, languageCopy Language) {
defer wg.Done()
err := DB.Model(&userCopy).Association("Languages").Append(&languageCopy)
if err != nil {
log.Printf("[!] Error white appending language to user, %s", err)
return
}
time.Sleep(time.Second) // Simulating task.
}(user1, language)
}
wg.Wait()

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
user2 := User{}
DB.Preload(clause.Associations).First(&user2)
log.Println(len(user2.Languages) == count, len(user2.Languages))
if len(user2.Languages) != count {
t.Errorf("Failed, %d should be %d", len(user2.Languages), count)
}
}