|
| 1 | +package lock |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "sync" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | +) |
| 10 | + |
| 11 | +func TestAdvisoryKeyDeterministic(t *testing.T) { |
| 12 | + subject := "devstack-shared-ledger" |
| 13 | + first, second := AdvisoryKey(subject), AdvisoryKey(subject) |
| 14 | + if first != second { |
| 15 | + t.Fatalf("same subject must hash to the same key (%d != %d)", first, second) |
| 16 | + } |
| 17 | + if AdvisoryKey("a") == AdvisoryKey("b") { |
| 18 | + t.Fatal("different subjects should (almost surely) differ") |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +// fakeAdvisory is an in-memory model of ONE cluster's advisory-lock table: a |
| 23 | +// single map[key]bool guarded by a real mutex, so concurrent PGLockers exercise |
| 24 | +// genuine mutual exclusion (compare-and-set), like the real server session. |
| 25 | +type fakeAdvisory struct { |
| 26 | + mu sync.Mutex |
| 27 | + held map[int64]bool |
| 28 | +} |
| 29 | + |
| 30 | +func newFakeAdvisory() *fakeAdvisory { return &fakeAdvisory{held: map[int64]bool{}} } |
| 31 | + |
| 32 | +// session is one connection to the fake server. |
| 33 | +type fakeSession struct { |
| 34 | + srv *fakeAdvisory |
| 35 | + closed bool |
| 36 | + // hooks to simulate failures |
| 37 | + tryErr error |
| 38 | + closeErr error |
| 39 | +} |
| 40 | + |
| 41 | +func (s *fakeSession) TryLock(_ context.Context, key int64) (bool, error) { |
| 42 | + if s.tryErr != nil { |
| 43 | + return false, s.tryErr |
| 44 | + } |
| 45 | + s.srv.mu.Lock() |
| 46 | + defer s.srv.mu.Unlock() |
| 47 | + if s.srv.held[key] { |
| 48 | + return false, nil |
| 49 | + } |
| 50 | + s.srv.held[key] = true |
| 51 | + return true, nil |
| 52 | +} |
| 53 | + |
| 54 | +func (s *fakeSession) Unlock(_ context.Context, key int64) error { |
| 55 | + s.srv.mu.Lock() |
| 56 | + defer s.srv.mu.Unlock() |
| 57 | + delete(s.srv.held, key) |
| 58 | + return nil |
| 59 | +} |
| 60 | + |
| 61 | +func (s *fakeSession) Close(context.Context) error { |
| 62 | + s.closed = true |
| 63 | + return s.closeErr |
| 64 | +} |
| 65 | + |
| 66 | +func (f *fakeAdvisory) connector() PGConnector { |
| 67 | + return func(context.Context) (AdvisoryConn, error) { |
| 68 | + return &fakeSession{srv: f}, nil |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func TestPGLocker_AcquireRunRelease(t *testing.T) { |
| 73 | + srv := newFakeAdvisory() |
| 74 | + l := &PGLocker{Subject: "s", Connect: srv.connector()} |
| 75 | + ran := false |
| 76 | + if err := l.WithLock(context.Background(), func() error { |
| 77 | + ran = true |
| 78 | + // While fn runs, the key must be held. |
| 79 | + srv.mu.Lock() |
| 80 | + defer srv.mu.Unlock() |
| 81 | + if !srv.held[AdvisoryKey("s")] { |
| 82 | + t.Error("key should be held while fn runs") |
| 83 | + } |
| 84 | + return nil |
| 85 | + }); err != nil { |
| 86 | + t.Fatal(err) |
| 87 | + } |
| 88 | + if !ran { |
| 89 | + t.Fatal("fn did not run") |
| 90 | + } |
| 91 | + // Released after WithLock returns. |
| 92 | + if srv.held[AdvisoryKey("s")] { |
| 93 | + t.Error("key should be released after WithLock") |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +func TestPGLocker_PropagatesFnError(t *testing.T) { |
| 98 | + srv := newFakeAdvisory() |
| 99 | + l := &PGLocker{Subject: "s", Connect: srv.connector()} |
| 100 | + sentinel := errors.New("boom") |
| 101 | + if err := l.WithLock(context.Background(), func() error { return sentinel }); !errors.Is(err, sentinel) { |
| 102 | + t.Fatalf("want fn error propagated, got %v", err) |
| 103 | + } |
| 104 | + if srv.held[AdvisoryKey("s")] { |
| 105 | + t.Error("key must be released even when fn errors") |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestPGLocker_NoConnector(t *testing.T) { |
| 110 | + l := &PGLocker{Subject: "s"} |
| 111 | + if err := l.WithLock(context.Background(), func() error { return nil }); err == nil { |
| 112 | + t.Fatal("want error when no connector is configured") |
| 113 | + } |
| 114 | +} |
| 115 | + |
| 116 | +func TestPGLocker_ConnectError(t *testing.T) { |
| 117 | + l := &PGLocker{Subject: "s", Connect: func(context.Context) (AdvisoryConn, error) { |
| 118 | + return nil, errors.New("dial refused") |
| 119 | + }} |
| 120 | + ran := false |
| 121 | + err := l.WithLock(context.Background(), func() error { ran = true; return nil }) |
| 122 | + if err == nil || ran { |
| 123 | + t.Fatalf("connect error must abort before fn (err=%v ran=%v)", err, ran) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func TestPGLocker_CtxTimeoutWhenHeld(t *testing.T) { |
| 128 | + srv := newFakeAdvisory() |
| 129 | + // Pre-hold the key so TryLock always returns false. |
| 130 | + srv.held[AdvisoryKey("s")] = true |
| 131 | + l := &PGLocker{Subject: "s", Connect: srv.connector(), retry: time.Millisecond} |
| 132 | + ctx, cancel := context.WithTimeout(context.Background(), 30*time.Millisecond) |
| 133 | + defer cancel() |
| 134 | + ran := false |
| 135 | + err := l.WithLock(ctx, func() error { ran = true; return nil }) |
| 136 | + if err == nil || ran { |
| 137 | + t.Fatalf("a permanently-held key must time out before fn (err=%v ran=%v)", err, ran) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func TestPGLocker_TryLockErrorSurfaces(t *testing.T) { |
| 142 | + l := &PGLocker{Subject: "s", Connect: func(context.Context) (AdvisoryConn, error) { |
| 143 | + return &fakeSession{srv: newFakeAdvisory(), tryErr: errors.New("conn reset")}, nil |
| 144 | + }} |
| 145 | + if err := l.WithLock(context.Background(), func() error { return nil }); err == nil { |
| 146 | + t.Fatal("a TryLock error must surface") |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +// TestPGLocker_Serializes is the concurrency guarantee: N goroutines each take |
| 151 | +// the lock and do a deliberately non-atomic read-modify-write; mutual exclusion |
| 152 | +// must make the final count exact. Run under -race. |
| 153 | +func TestPGLocker_Serializes(t *testing.T) { |
| 154 | + srv := newFakeAdvisory() |
| 155 | + l := &PGLocker{Subject: "ledger", Connect: srv.connector(), retry: time.Millisecond} |
| 156 | + const n = 50 |
| 157 | + counter := 0 |
| 158 | + var wg sync.WaitGroup |
| 159 | + for range n { |
| 160 | + wg.Go(func() { |
| 161 | + _ = l.WithLock(context.Background(), func() error { |
| 162 | + c := counter |
| 163 | + time.Sleep(time.Microsecond) // widen the race window |
| 164 | + counter = c + 1 |
| 165 | + return nil |
| 166 | + }) |
| 167 | + }) |
| 168 | + } |
| 169 | + wg.Wait() |
| 170 | + if counter != n { |
| 171 | + t.Fatalf("mutual exclusion failed: counter = %d, want %d", counter, n) |
| 172 | + } |
| 173 | +} |
0 commit comments