Repository navigation
Expand file tree
/
Copy pathpull_test.go
More file actions
72 lines (61 loc) · 2.22 KB
/
Copy pathpull_test.go
File metadata and controls
72 lines (61 loc) · 2.22 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
package main
import (
"testing"
"time"
)
// Failure mode #1: within the TTL, a cached grant that the owner has since revoked
// is served as still-granted, with no error. This proves the "silently stale"
// claim the README and demo make about the pull approach.
func TestPull_ServesStaleWithinTTL(t *testing.T) {
log := NewEventLog()
owner := NewOwner(log)
owner.Grant("u", "p")
// Controllable clock so the test is deterministic, not wall-clock dependent.
clock := time.Unix(0, 0)
pull := NewPullConsumer(owner, 5*time.Second, 2)
pull.now = func() time.Time { return clock }
if ok, _ := pull.Allowed("u", "p"); !ok { // warms cache with granted=true
t.Fatal("expected granted on first lookup")
}
owner.Revoke("u", "p") // owner is now false; cache still says true
clock = clock.Add(1 * time.Second) // still within the 5s TTL
ok, err := pull.Allowed("u", "p")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !ok {
t.Fatal("expected STALE granted=true within TTL (the failure mode under test)")
}
clock = clock.Add(5 * time.Second) // now past the TTL -> re-fetch
ok, _ = pull.Allowed("u", "p")
if ok {
t.Fatal("after TTL expiry, pull should re-fetch and see the revoke")
}
}
// Failure mode #2: on a cache miss while the owner is down, retries exhaust and
// the call returns an error — the consumer is blocked on the owner's uptime.
func TestPull_FailsClosedOnColdReadDuringOutage(t *testing.T) {
log := NewEventLog()
owner := NewOwner(log)
owner.Grant("u", "p")
owner.SetDown(true)
pull := NewPullConsumer(owner, 5*time.Second, 2)
_, err := pull.Allowed("u", "p") // cold key, owner down
if err == nil {
t.Fatal("expected an error on a cold read during outage")
}
}
// Contrast anchor: the push consumer answers the same cold-read-during-outage
// correctly, because its read path never touches the owner. This is the whole
// thesis in one assertion.
func TestPush_AnswersColdReadDuringOutage(t *testing.T) {
log := NewEventLog()
owner := NewOwner(log)
owner.Grant("u", "p")
push := NewPushConsumer(log)
push.rebuildSynchronously()
owner.SetDown(true) // irrelevant to push; it reads locally
if !push.Allowed("u", "p") {
t.Fatal("push should answer from local view regardless of owner state")
}
}