package main import ( "os" "path/filepath" "syscall" "testing" ) func TestAcquireSingleInstance(t *testing.T) { lockPath := filepath.Join(os.TempDir(), "nex-gateway-test.lock") origLockFile := lockFile lockFile = nil defer func() { lockFile = origLockFile }() f, err := os.OpenFile(lockPath, os.O_CREATE|os.O_RDWR, 0666) if err != nil { t.Fatalf("无法创建锁文件: %v", err) } defer f.Close() defer os.Remove(lockPath) err = syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB) if err != nil { t.Fatalf("无法获取文件锁: %v", err) } defer syscall.Flock(int(f.Fd()), syscall.LOCK_UN) t.Log("单实例锁测试通过") } func TestReleaseSingleInstance(t *testing.T) { lockFile = nil releaseSingleInstance() t.Log("释放空锁测试通过") }