Skip to content

Commit cd0df98

Browse files
committed
fix(tests): avoid result4 panic and assert concurrent update errors
- dockertest: check len(result4) before using result4[0].Name so test failure reporting does not panic when the query returns an unexpected number of rows. - dockertest: capture Update(...).Error in concurrent update goroutines, send errors on errCh, and t.Fatalf on any error after goroutines finish so failed writes fail the test.
1 parent 751accb commit cd0df98

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

cache/dockertest_integration_test.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,10 @@ func TestCompositePrimaryKey_MySQL(t *testing.T) {
363363
if err := db.Where("user_id = ? AND role_id = ?", 1, 1).Find(&result4).Error; err != nil {
364364
t.Fatalf("Failed to query: %v", err)
365365
}
366-
if len(result4) != 1 || result4[0].Name != "SuperAdmin" {
366+
if len(result4) != 1 {
367+
t.Fatalf("Expected 1 result, got %d", len(result4))
368+
}
369+
if result4[0].Name != "SuperAdmin" {
367370
t.Errorf("Expected name 'SuperAdmin', got %s", result4[0].Name)
368371
}
369372
}
@@ -853,13 +856,17 @@ func TestCacheConsistency_Advanced_MySQL(t *testing.T) {
853856
const numGoroutines = 5
854857
const numUpdates = 3
855858
done := make(chan bool, numGoroutines)
859+
errCh := make(chan error, numGoroutines)
856860

857861
for i := 0; i < numGoroutines; i++ {
858862
go func(id int) {
859863
defer func() { done <- true }()
860864
for j := 0; j < numUpdates; j++ {
861865
newName := fmt.Sprintf("Update-%d-%d", id, j)
862-
db.Model(&UserRole{}).Where("user_id = ? AND role_id = ?", 1000, 2000).Update("name", newName)
866+
if err := db.Model(&UserRole{}).Where("user_id = ? AND role_id = ?", 1000, 2000).Update("name", newName).Error; err != nil {
867+
errCh <- err
868+
return
869+
}
863870
time.Sleep(10 * time.Millisecond)
864871
}
865872
}(i)
@@ -868,6 +875,10 @@ func TestCacheConsistency_Advanced_MySQL(t *testing.T) {
868875
for i := 0; i < numGoroutines; i++ {
869876
<-done
870877
}
878+
close(errCh)
879+
for err := range errCh {
880+
t.Fatalf("concurrent update failed: %v", err)
881+
}
871882

872883
if err := waitForCondition(20*time.Millisecond, 3*time.Second, func() bool {
873884
var r UserRole

0 commit comments

Comments
 (0)