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-02-13 13:51:35 +00:00
|
|
|
case "linux", "darwin", "illumos", "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)
|
|
|
|
|
vfsFilePtr{mem.mod, pFile1}.SetID(vfsGetFileID(file1)).SetLock(_NO_LOCK)
|
|
|
|
|
vfsFilePtr{mem.mod, pFile2}.SetID(vfsGetFileID(file2)).SetLock(_NO_LOCK)
|
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, _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)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 13:29:51 +00:00
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 17:14:43 +00:00
|
|
|
rc = vfsUnlock(context.TODO(), mem.mod, pFile2, _SHARED_LOCK)
|
|
|
|
|
if rc != _OK {
|
|
|
|
|
t.Fatal("returned", rc)
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-23 13:29:51 +00:00
|
|
|
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 was locked")
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-10 17:14:43 +00:00
|
|
|
rc = vfsLock(context.TODO(), mem.mod, pFile1, _SHARED_LOCK)
|
|
|
|
|
if rc != _OK {
|
|
|
|
|
t.Fatal("returned", rc)
|
|
|
|
|
}
|
2023-01-30 08:12:12 +00:00
|
|
|
}
|