@@ -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.
381384func 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