Files
sqlite3/vfs_lock_test.go

123 lines
2.4 KiB
Go
Raw Permalink Normal View History

2023-01-30 08:12:12 +00:00
package sqlite3
import (
"context"
"os"
"runtime"
"testing"
)
func Test_vfsLock(t *testing.T) {
2023-02-10 17:14:43 +00:00
// Other OSes lack open file descriptors locks.
2023-01-30 08:12:12 +00:00
switch runtime.GOOS {
2023-02-13 13:51:35 +00:00
case "linux", "darwin", "illumos", "windows":
2023-01-30 08:12:12 +00:00
//
default:
t.Skip()
}
2023-02-10 17:14:43 +00:00
// Create a temporary file.
2023-01-30 08:12:12 +00:00
file1, err := os.CreateTemp("", "sqlite3-")
if err != nil {
t.Fatal(err)
}
defer file1.Close()
name := file1.Name()
defer os.RemoveAll(name)
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
// Bypass open file reuse.
2023-01-30 08:12:12 +00:00
vfsOpenFiles = append(vfsOpenFiles, &vfsOpenFile{
2023-02-07 03:11:59 +00:00
file: file1,
nref: 1,
locker: vfsFileLocker{file: file1},
2023-01-30 08:12:12 +00:00
}, &vfsOpenFile{
2023-02-07 03:11:59 +00:00
file: file2,
nref: 1,
locker: vfsFileLocker{file: file2},
2023-01-30 08:12:12 +00:00
})
mem := newMemory(128)
mem.writeUint32(4+4, 0)
mem.writeUint32(16+4, 1)
2023-02-10 17:14:43 +00:00
const (
pFile1 = 4
pFile2 = 16
pOutput = 32
)
rc := vfsCheckReservedLock(context.TODO(), 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-02-10 17:14:43 +00:00
rc = vfsLock(context.TODO(), mem.mod, pFile2, _SHARED_LOCK)
2023-01-30 08:12:12 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-02-10 17:14:43 +00:00
rc = vfsCheckReservedLock(context.TODO(), 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-02-10 17:14:43 +00:00
rc = vfsLock(context.TODO(), mem.mod, pFile2, _RESERVED_LOCK)
if rc != _OK {
t.Fatal("returned", rc)
}
rc = vfsLock(context.TODO(), mem.mod, pFile2, _SHARED_LOCK)
2023-01-30 08:12:12 +00:00
if rc != _OK {
t.Fatal("returned", rc)
}
2023-02-10 17:14:43 +00:00
rc = vfsCheckReservedLock(context.TODO(), 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
rc = vfsLock(context.TODO(), mem.mod, pFile2, _EXCLUSIVE_LOCK)
if rc != _OK {
t.Fatal("returned", rc)
}
rc = vfsCheckReservedLock(context.TODO(), mem.mod, pFile1, pOutput)
if rc != _OK {
t.Fatal("returned", rc)
}
if got := mem.readUint32(pOutput); got == 0 {
t.Error("file wasn't locked")
}
rc = vfsLock(context.TODO(), mem.mod, pFile1, _SHARED_LOCK)
if rc == _OK {
t.Fatal("returned", rc)
}
rc = vfsUnlock(context.TODO(), mem.mod, pFile2, _SHARED_LOCK)
if rc != _OK {
t.Fatal("returned", rc)
}
rc = vfsLock(context.TODO(), mem.mod, pFile1, _SHARED_LOCK)
if rc != _OK {
t.Fatal("returned", rc)
}
2023-01-30 08:12:12 +00:00
}