Repository navigation
Expand file tree
/
Copy pathproperty_test.go
More file actions
147 lines (130 loc) · 3.67 KB
/
Copy pathproperty_test.go
File metadata and controls
147 lines (130 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package lockd
import (
"context"
"math/rand"
"reflect"
"testing"
"testing/quick"
"time"
"github.com/imharjot/lockd/clock"
"github.com/imharjot/lockd/lease"
)
// opKind is one generated operation in a property run.
type opKind int
const (
pAcquire opKind = iota
pRenew
pRelease
pTick
)
type genOp struct {
kind opKind
holder int
ttl time.Duration
adv time.Duration
}
// Generate makes a random genOp for testing/quick.
func (genOp) Generate(r *rand.Rand, _ int) reflect.Value {
o := genOp{
kind: opKind(r.Intn(4)),
holder: r.Intn(3), // small holder set so contention actually happens
ttl: time.Duration(1+r.Intn(5)) * time.Second,
adv: time.Duration(r.Intn(4)) * time.Second,
}
return reflect.ValueOf(o)
}
// TestInvariantsUnderRandomOps drives the engine through random operation
// sequences on a single key and asserts the core invariants hold throughout:
//
// - at most one live holder at any instant (qty=1, the whole point);
// - fencing tokens never decrease and are unique per grant;
// - the engine's reported state matches a simple sequential reference model
// applied to the same operations.
func TestInvariantsUnderRandomOps(t *testing.T) {
f := func(ops []genOp) bool {
return runProperty(t, ops)
}
if err := quick.Check(f, &quick.Config{MaxCount: 400}); err != nil {
t.Fatal(err)
}
}
func runProperty(t *testing.T, ops []genOp) bool {
t.Helper()
fc := clock.NewFake(epoch)
e := New(Config{Shards: 1, Clock: fc, Tick: time.Second, WheelSlots: 32, ManualTick: true})
defer e.Close()
const key = "k"
holders := []string{"h0", "h1", "h2"}
// reference model: who holds the key and with which token.
var refHeld bool
var refHolder string
var refToken lease.Token
var refExpiry time.Time
var lastToken lease.Token
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for _, o := range ops {
now := fc.Now()
// expire the reference model if the live lease has lapsed.
if refHeld && !refExpiry.After(now) {
refHeld = false
}
switch o.kind {
case pAcquire:
// non-blocking acquire: cancel context up front so a held key
// returns immediately instead of queueing.
ac, c := context.WithCancel(ctx)
c()
g, err := e.Acquire(ac, key, holders[o.holder], o.ttl)
if err == nil {
// engine granted: model must have been free.
if refHeld {
t.Logf("engine granted while model held: %+v", g)
return false
}
if g.Token <= lastToken {
t.Logf("token not monotonic: %d after %d", g.Token, lastToken)
return false
}
lastToken = g.Token
refHeld, refHolder, refToken, refExpiry = true, holders[o.holder], g.Token, g.ExpiresAt
}
case pRenew:
g, err := e.Renew(key, holders[o.holder], refToken, o.ttl)
if err == nil {
if !refHeld || refHolder != holders[o.holder] {
t.Logf("engine renewed for non-holder")
return false
}
refExpiry = g.ExpiresAt
}
case pRelease:
err := e.Release(key, holders[o.holder], refToken)
if err == nil {
if !refHeld || refHolder != holders[o.holder] {
t.Logf("engine released for non-holder")
return false
}
refHeld = false
}
case pTick:
if o.adv > 0 {
fc.Advance(o.adv)
e.Tick()
}
}
// invariant check after each step: engine fence agrees with the model
// on held-ness (allowing for expiry the model already folded in).
tok, held := e.Fence(key)
modelHeld := refHeld && refExpiry.After(fc.Now())
if held != modelHeld {
t.Logf("held mismatch: engine=%v model=%v at %v", held, modelHeld, fc.Now())
return false
}
if held && tok != refToken {
t.Logf("token mismatch: engine=%d model=%d", tok, refToken)
return false
}
}
return true
}