Files
sqlite3/vfs_lock_test.go

129 lines
2.5 KiB
Go
Raw Normal View History

2023-01-30 08:12:12 +00:00
package sqlite3
import (
"context"
"os"
2023-02-20 13:30:01 +00:00
"path/filepath"
2023-01-30 08:12:12 +00:00
"runtime"
"testing"
)
func Test_vfsLock(t *testing.T) {
switch runtime.GOOS {
2023-03-17 17:13:03 +00:00
case "linux", "darwin", "windows":
2023-02-23 13:29:51 +00:00
break
2023-01-30 08:12:12 +00:00
default:
2023-03-01 12:16:36 +00:00
t.Skip("OS lacks OFD locks")
2023-01-30 08:12:12 +00:00
}
2023-02-20 13:30:01 +00:00
name := filepath.Join(t.TempDir(), "test.db")
2023-02-10 17:14:43 +00:00
// Create a temporary file.
2023-02-20 13:30:01 +00:00
file1, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_EXCL, 0666)
2023-01-30 08:12:12 +00:00
if err != nil {
t.Fatal(err)
}
defer file1.Close()
2023-02-10 17:14:43 +00:00
// Open the temporary file again.
2023-01-30 08:12:12 +00:00
file2, err := os.OpenFile(name, os.O_RDWR, 0)
if err != nil {
t.Fatal(err)
}
defer file2.Close()
2023-02-10 17:14:43 +00:00
const (
pFile1 = 4
pFile2 = 16
pOutput = 32
)
2023-02-23 13:29:51 +00:00
mem := newMemory(128)
2023-03-07 03:51:07 +00:00
ctx, vfs := vfsContext(context.TODO())
defer vfs.Close()
2023-02-10 17:14:43 +00:00
2023-03-07 10:45:11 +00:00
vfsFile.Open(ctx, mem.mod, pFile1, file1)
vfsFile.Open(ctx, mem.mod, pFile2, file2)
2023-03-07 03:51:07 +00:00
rc := vfsCheckReservedLock(ctx, mem.mod, pFile1, pOutput)
2023-01-30 08:12:12 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-02-10 17:14:43 +00:00
if got := mem.readUint32(pOutput); got != 0 {
2023-01-30 08:12:12 +00:00
t.Error("file was locked")
}
2023-03-07 03:51:07 +00:00
rc = vfsLock(ctx, mem.mod, pFile2, _SHARED_LOCK)
2023-01-30 08:12:12 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-03-07 03:51:07 +00:00
rc = vfsCheckReservedLock(ctx, mem.mod, pFile1, pOutput)
2023-01-30 08:12:12 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-02-10 17:14:43 +00:00
if got := mem.readUint32(pOutput); got != 0 {
2023-01-30 08:12:12 +00:00
t.Error("file was locked")
}
2023-03-07 03:51:07 +00:00
rc = vfsLock(ctx, mem.mod, pFile2, _RESERVED_LOCK)
2023-02-10 17:14:43 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-03-07 03:51:07 +00:00
rc = vfsLock(ctx, mem.mod, pFile2, _SHARED_LOCK)
2023-01-30 08:12:12 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-03-07 03:51:07 +00:00
rc = vfsCheckReservedLock(ctx, mem.mod, pFile1, pOutput)
2023-01-30 08:12:12 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-02-10 17:14:43 +00:00
if got := mem.readUint32(pOutput); got == 0 {
2023-01-30 08:12:12 +00:00
t.Error("file wasn't locked")
}
2023-02-10 17:14:43 +00:00
2023-03-07 03:51:07 +00:00
rc = vfsLock(ctx, mem.mod, pFile2, _EXCLUSIVE_LOCK)
2023-02-10 17:14:43 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-03-07 03:51:07 +00:00
rc = vfsCheckReservedLock(ctx, mem.mod, pFile1, pOutput)
2023-02-10 17:14:43 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
if got := mem.readUint32(pOutput); got == 0 {
t.Error("file wasn't locked")
}
2023-03-07 03:51:07 +00:00
rc = vfsLock(ctx, mem.mod, pFile1, _SHARED_LOCK)
2023-02-10 17:14:43 +00:00
if rc == _OK {
t.Fatal("returned", rc)
}
2023-03-07 03:51:07 +00:00
rc = vfsCheckReservedLock(ctx, mem.mod, pFile1, pOutput)
2023-02-23 13:29:51 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
if got := mem.readUint32(pOutput); got == 0 {
t.Error("file wasn't locked")
}
2023-03-07 03:51:07 +00:00
rc = vfsUnlock(ctx, mem.mod, pFile2, _SHARED_LOCK)
2023-02-10 17:14:43 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-03-07 03:51:07 +00:00
rc = vfsCheckReservedLock(ctx, mem.mod, pFile1, pOutput)
2023-02-23 13:29:51 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
if got := mem.readUint32(pOutput); got != 0 {
t.Error("file was locked")
}
2023-03-07 03:51:07 +00:00
rc = vfsLock(ctx, mem.mod, pFile1, _SHARED_LOCK)
2023-02-10 17:14:43 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-01-30 08:12:12 +00:00
}