gunk-dev/the-valley

lock_test.go

source4cbb5ec9c5dbmain's published tip history
2.9 KiB
1package location
2
3import (
4 "errors"
5 "os"
6 "path/filepath"
7 "syscall"
8 "testing"
9)
10
11// lone is a build that locks one path alone, as one that predates the
12// lock's move, or one that has finished it, does. flock excludes two open
13// file descriptions of a file, in one process as across two.
14func lone(t *testing.T, path string) (release func(), ok bool) {
15 t.Helper()
16 if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
17 t.Fatal(err)
18 }
19 f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0o644)
20 if err != nil {
21 t.Fatal(err)
22 }
23 if err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
24 f.Close()
25 if !errors.Is(err, syscall.EWOULDBLOCK) {
26 t.Fatal(err)
27 }
28 return nil, false
29 }
30 return func() { f.Close() }, true
31}
32
33func TestLockExcludesEitherPathAlone(t *testing.T) {
34 for i, name := range []string{"old-path-only", "new-path-only"} {
35 t.Run(name, func(t *testing.T) {
36 dir := t.TempDir()
37 paths := []string{filepath.Join(dir, "old", "x.lock"), filepath.Join(dir, "new", "x.lock")}
38 alone := paths[i]
39
40 // The lone locker first: this build waits its turn.
41 release, ok := lone(t, alone)
42 if !ok {
43 t.Fatal("lone locker could not lock")
44 }
45 _, err := TryLock(paths...)
46 var busy *BusyError
47 if !errors.As(err, &busy) || busy.Path != alone || !errors.Is(err, ErrBusy) {
48 t.Fatalf("TryLock with %s held = %v, want busy at it", alone, err)
49 }
50 // A failure at the second path released the first.
51 if i == 1 {
52 if r, ok := lone(t, paths[0]); !ok {
53 t.Error("the first path stayed locked after the second was busy")
54 } else {
55 r()
56 }
57 }
58 release()
59
60 // This build first: the lone locker waits its turn.
61 l, err := TryLock(paths...)
62 if err != nil {
63 t.Fatal(err)
64 }
65 if _, ok := lone(t, alone); ok {
66 t.Errorf("a lone locker of %s got the lock this build holds", alone)
67 }
68 l.Release()
69 if r, ok := lone(t, alone); !ok {
70 t.Error("the lock stayed held after Release")
71 } else {
72 r()
73 }
74 for _, p := range paths {
75 if _, err := os.Stat(p); err != nil {
76 t.Errorf("Release removed %s: %v", p, err)
77 }
78 }
79 })
80 }
81}
82
83// Two paths that are one file are locked once: a second open file
84// description would wait on the first forever.
85func TestLockSameFileOnce(t *testing.T) {
86 dir := t.TempDir()
87 old := filepath.Join(dir, "old.lock")
88 if err := os.WriteFile(old, nil, 0o644); err != nil {
89 t.Fatal(err)
90 }
91 link := filepath.Join(dir, "flow", "new.lock")
92 if err := os.MkdirAll(filepath.Dir(link), 0o755); err != nil {
93 t.Fatal(err)
94 }
95 if err := os.Symlink(old, link); err != nil {
96 t.Fatal(err)
97 }
98 l, err := WaitLock(old, link)
99 if err != nil {
100 t.Fatalf("WaitLock of one file under two names: %v", err)
101 }
102 if _, ok := lone(t, old); ok {
103 t.Error("a lone locker got the lock")
104 }
105 l.Release()
106 if r, ok := lone(t, link); !ok {
107 t.Error("the lock stayed held after Release")
108 } else {
109 r()
110 }
111}