From 27081adae2dbb18da945d7109742b2cbeff64b9b Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Fri, 10 Feb 2023 17:14:43 +0000 Subject: [PATCH] Tests. --- vfs_lock_test.go | 58 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 8 deletions(-) diff --git a/vfs_lock_test.go b/vfs_lock_test.go index e2b296e..f2b2740 100644 --- a/vfs_lock_test.go +++ b/vfs_lock_test.go @@ -8,6 +8,7 @@ import ( ) func Test_vfsLock(t *testing.T) { + // Other OSes lack open file descriptors locks. switch runtime.GOOS { case "linux", "darwin", "solaris", "windows": // @@ -15,6 +16,7 @@ func Test_vfsLock(t *testing.T) { t.Skip() } + // Create a temporary file. file1, err := os.CreateTemp("", "sqlite3-") if err != nil { t.Fatal(err) @@ -24,12 +26,14 @@ func Test_vfsLock(t *testing.T) { name := file1.Name() defer os.RemoveAll(name) + // Open the temporary file again. file2, err := os.OpenFile(name, os.O_RDWR, 0) if err != nil { t.Fatal(err) } defer file2.Close() + // Bypass open file reuse. vfsOpenFiles = append(vfsOpenFiles, &vfsOpenFile{ file: file1, nref: 1, @@ -44,37 +48,75 @@ func Test_vfsLock(t *testing.T) { mem.writeUint32(4+4, 0) mem.writeUint32(16+4, 1) - rc := vfsCheckReservedLock(context.TODO(), mem.mod, 4, 32) + const ( + pFile1 = 4 + pFile2 = 16 + pOutput = 32 + ) + + rc := vfsCheckReservedLock(context.TODO(), mem.mod, pFile1, pOutput) if rc != _OK { t.Fatal("returned", rc) } - if got := mem.readUint32(16); got != 0 { + if got := mem.readUint32(pOutput); got != 0 { t.Error("file was locked") } - rc = vfsLock(context.TODO(), mem.mod, 16, _SHARED_LOCK) + rc = vfsLock(context.TODO(), mem.mod, pFile2, _SHARED_LOCK) if rc != _OK { t.Fatal("returned", rc) } - rc = vfsCheckReservedLock(context.TODO(), mem.mod, 4, 32) + rc = vfsCheckReservedLock(context.TODO(), mem.mod, pFile1, pOutput) if rc != _OK { t.Fatal("returned", rc) } - if got := mem.readUint32(32); got != 0 { + if got := mem.readUint32(pOutput); got != 0 { t.Error("file was locked") } - rc = vfsLock(context.TODO(), mem.mod, 16, _RESERVED_LOCK) + 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) if rc != _OK { t.Fatal("returned", rc) } - rc = vfsCheckReservedLock(context.TODO(), mem.mod, 4, 32) + rc = vfsCheckReservedLock(context.TODO(), mem.mod, pFile1, pOutput) if rc != _OK { t.Fatal("returned", rc) } - if got := mem.readUint32(32); got == 0 { + if got := mem.readUint32(pOutput); got == 0 { t.Error("file wasn't locked") } + + 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) + } }