mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-08 14:47:44 +00:00
refactor: retry file remove/rename when a file is busy and clean up os detection (#38588)
Rename functions "util.Remove" (remove.go) to "util.RemoveWithRetry" (file_retry.go) and add comments to clarify their behaviors, also add tests. Refactor callers: when no concurrent access (cmd cli, migration, app init, test), use "os.Xxx" directly. More details are in `modules/util/file_retry.go` By the way, clean up OS (windows) detection, make FileURLToPath test always run
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"syscall"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestRetryWhenFileBusy(t *testing.T) {
|
||||
var callCount int
|
||||
testErrPath := &os.PathError{Op: "test", Path: "test", Err: syscall.EBUSY}
|
||||
fn := func() error {
|
||||
if callCount == 2 {
|
||||
return nil
|
||||
}
|
||||
callCount++
|
||||
return testErrPath
|
||||
}
|
||||
|
||||
callCount = 0
|
||||
err := retryWhenFileBusyInternal(1, time.Millisecond, fn)
|
||||
assert.Equal(t, testErrPath, err)
|
||||
assert.Equal(t, 1, callCount)
|
||||
|
||||
callCount = 0
|
||||
err = retryWhenFileBusyInternal(10, time.Millisecond, fn)
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 2, callCount)
|
||||
|
||||
callCount = 0
|
||||
err = retryWhenFileBusyInternal(10, time.Millisecond, func() error {
|
||||
callCount++
|
||||
return io.ErrUnexpectedEOF
|
||||
})
|
||||
assert.Equal(t, io.ErrUnexpectedEOF, err)
|
||||
assert.Equal(t, 1, callCount)
|
||||
}
|
||||
Reference in New Issue
Block a user