Skip to content

Commit 8a79da5

Browse files
authored
test: Fixed flaky test for change subscription handler and antivirus (#4649)
It waited only for orgInst1 to be updated before
2 parents 2fa9e21 + aee45bf commit 8a79da5

File tree

2 files changed

+59
-26
lines changed

2 files changed

+59
-26
lines changed

pkg/rabbitmq/rabbitmq_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,18 @@ func TestHandlers(t *testing.T) {
505505
return updated.FeatureSets[0] == msg.Features.Stack.FeatureSets[0]
506506
})
507507

508+
// Wait until orgInst2 is updated (the handler updates instances sequentially)
509+
testutils.WaitForOrFail(t, 10*time.Second, func() bool {
510+
updated, err := lifecycle.GetInstance(orgInst2.Domain)
511+
if err != nil {
512+
return false
513+
}
514+
if len(updated.FeatureSets) == 0 {
515+
return false
516+
}
517+
return updated.FeatureSets[0] == msg.Features.Stack.FeatureSets[0]
518+
})
519+
508520
// orgInst1 is updated
509521
updated, err := lifecycle.GetInstance(orgInst1.Domain)
510522
require.NoError(t, err)

worker/antivirus/antivirus_test.go

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/cozy/cozy-stack/pkg/consts"
1818
"github.com/cozy/cozy-stack/pkg/couchdb"
1919
"github.com/cozy/cozy-stack/pkg/logger"
20+
"github.com/cozy/cozy-stack/pkg/utils"
2021
"github.com/cozy/cozy-stack/tests/testutils"
2122
"github.com/cozy/cozy-stack/worker/antivirus"
2223
"github.com/stretchr/testify/require"
@@ -349,6 +350,7 @@ func testWorkerFileVersionUpdate(t *testing.T, inst *instance.Instance) {
349350
originalMD5 := doc.MD5Sum
350351

351352
// Step 2: Update file with infected content (simulating new version upload)
353+
// Note: updateTestFileContent handles conflicts from background antivirus jobs
352354
doc = updateTestFileContent(t, fs, doc, testutils.EICARTestSignature())
353355

354356
// Verify MD5 changed (content was updated)
@@ -378,38 +380,57 @@ func testWorkerFileVersionUpdate(t *testing.T, inst *instance.Instance) {
378380
}
379381

380382
// updateTestFileContent updates a file's content by creating a new version.
383+
// It retries on conflict errors caused by background antivirus jobs updating the document.
381384
func updateTestFileContent(t *testing.T, fs vfs.VFS, olddoc *vfs.FileDoc, newContent []byte) *vfs.FileDoc {
382385
t.Helper()
383386

384-
// Create new file doc for update - use -1 for size and nil for MD5
385-
// so the VFS calculates them from the actual content
386-
newdoc, err := vfs.NewFileDoc(
387-
olddoc.DocName,
388-
olddoc.DirID,
389-
-1, // size will be calculated
390-
nil, // MD5 will be calculated
391-
olddoc.Mime,
392-
olddoc.Class,
393-
time.Now(),
394-
olddoc.Executable,
395-
olddoc.Trashed,
396-
olddoc.Encrypted,
397-
olddoc.Tags,
398-
)
399-
require.NoError(t, err)
387+
var result *vfs.FileDoc
388+
fileID := olddoc.DocID
400389

401-
// Create the new version of the file
402-
file, err := fs.CreateFile(newdoc, olddoc)
403-
require.NoError(t, err)
390+
err := utils.RetryWithExpBackoff(5, 10*time.Millisecond, func() error {
391+
// Re-fetch the document to get the latest revision
392+
currentDoc, err := fs.FileByID(fileID)
393+
if err != nil {
394+
return err
395+
}
404396

405-
_, err = file.Write(newContent)
406-
require.NoError(t, err)
397+
// Create new file doc for update
398+
newdoc, err := vfs.NewFileDoc(
399+
currentDoc.DocName,
400+
currentDoc.DirID,
401+
-1, // size will be calculated
402+
nil, // MD5 will be calculated
403+
currentDoc.Mime,
404+
currentDoc.Class,
405+
time.Now(),
406+
currentDoc.Executable,
407+
currentDoc.Trashed,
408+
currentDoc.Encrypted,
409+
currentDoc.Tags,
410+
)
411+
if err != nil {
412+
return err
413+
}
407414

408-
err = file.Close()
409-
require.NoError(t, err)
415+
// Create the new version of the file
416+
file, err := fs.CreateFile(newdoc, currentDoc)
417+
if err != nil {
418+
return err
419+
}
420+
421+
if _, err = file.Write(newContent); err != nil {
422+
return err
423+
}
424+
425+
if err = file.Close(); err != nil {
426+
return err
427+
}
428+
429+
// Success - get the updated document
430+
result, err = fs.FileByID(fileID)
431+
return err
432+
})
410433

411-
// Get the updated document
412-
updatedDoc, err := fs.FileByID(olddoc.DocID)
413434
require.NoError(t, err)
414-
return updatedDoc
435+
return result
415436
}

0 commit comments

Comments
 (0)