mirror of
https://github.com/ncruces/go-sqlite3.git
synced 2026-01-12 05:59:14 +00:00
More tests.
This commit is contained in:
80
vfs_lock_test.go
Normal file
80
vfs_lock_test.go
Normal file
@@ -0,0 +1,80 @@
|
||||
package sqlite3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"runtime"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_vfsLock(t *testing.T) {
|
||||
switch runtime.GOOS {
|
||||
case "linux", "darwin":
|
||||
//
|
||||
default:
|
||||
t.Skip()
|
||||
}
|
||||
|
||||
file1, err := os.CreateTemp("", "sqlite3-")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer file1.Close()
|
||||
|
||||
name := file1.Name()
|
||||
defer os.RemoveAll(name)
|
||||
|
||||
file2, err := os.OpenFile(name, os.O_RDWR, 0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer file2.Close()
|
||||
|
||||
vfsOpenFiles = append(vfsOpenFiles, &vfsOpenFile{
|
||||
file: file1,
|
||||
nref: 1,
|
||||
vfsLocker: &vfsFileLocker{file1, _NO_LOCK},
|
||||
}, &vfsOpenFile{
|
||||
file: file2,
|
||||
nref: 1,
|
||||
vfsLocker: &vfsFileLocker{file2, _NO_LOCK},
|
||||
})
|
||||
|
||||
mem := newMemory(128)
|
||||
mem.writeUint32(4+4, 0)
|
||||
mem.writeUint32(16+4, 1)
|
||||
|
||||
rc := vfsCheckReservedLock(context.TODO(), mem.mod, 4, 32)
|
||||
if rc != _OK {
|
||||
t.Fatal("returned", rc)
|
||||
}
|
||||
if got := mem.readUint32(16); got != 0 {
|
||||
t.Error("file was locked")
|
||||
}
|
||||
|
||||
rc = vfsLock(context.TODO(), mem.mod, 16, _SHARED_LOCK)
|
||||
if rc != _OK {
|
||||
t.Fatal("returned", rc)
|
||||
}
|
||||
|
||||
rc = vfsCheckReservedLock(context.TODO(), mem.mod, 4, 32)
|
||||
if rc != _OK {
|
||||
t.Fatal("returned", rc)
|
||||
}
|
||||
if got := mem.readUint32(32); got != 0 {
|
||||
t.Error("file was locked")
|
||||
}
|
||||
|
||||
rc = vfsLock(context.TODO(), mem.mod, 16, _EXCLUSIVE_LOCK)
|
||||
if rc != _OK {
|
||||
t.Fatal("returned", rc)
|
||||
}
|
||||
|
||||
rc = vfsCheckReservedLock(context.TODO(), mem.mod, 4, 32)
|
||||
if rc != _OK {
|
||||
t.Fatal("returned", rc)
|
||||
}
|
||||
if got := mem.readUint32(32); got == 0 {
|
||||
t.Error("file wasn't locked")
|
||||
}
|
||||
}
|
||||
@@ -156,7 +156,7 @@ func (l *vfsFileLocker) CheckReservedLock() (bool, xErrorCode) {
|
||||
if l.fcntlGetLock(&lock) != nil {
|
||||
return false, IOERR_CHECKRESERVEDLOCK
|
||||
}
|
||||
return lock.Type == syscall.F_UNLCK, _OK
|
||||
return lock.Type != syscall.F_UNLCK, _OK
|
||||
}
|
||||
|
||||
func (l *vfsFileLocker) fcntlGetLock(lock *syscall.Flock_t) error {
|
||||
|
||||
Reference in New Issue
Block a user