Repository navigation
Expand file tree
/
Copy pathpush_test.go
More file actions
160 lines (136 loc) · 4.94 KB
/
Copy pathpush_test.go
File metadata and controls
160 lines (136 loc) · 4.94 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
148
149
150
151
152
153
154
155
156
157
158
159
160
package main
import (
"testing"
"time"
)
// --- Projection logic ---
// A grant then a revoke must leave the view showing "not granted". This is the
// core of the projection: later facts supersede earlier ones for the same key.
func TestProjection_GrantThenRevoke(t *testing.T) {
log := NewEventLog()
log.Append(Event{Subject: "u", Perm: "p", Granted: true})
log.Append(Event{Subject: "u", Perm: "p", Granted: false})
c := NewPushConsumer(log)
c.rebuildSynchronously()
if c.Allowed("u", "p") {
t.Fatalf("expected not granted after revoke, got granted")
}
if c.Cursor() != 1 {
t.Fatalf("expected cursor 1, got %d", c.Cursor())
}
}
// Different subjects and perms must not bleed into each other.
func TestProjection_Isolation(t *testing.T) {
log := NewEventLog()
log.Append(Event{Subject: "alice", Perm: "export", Granted: true})
log.Append(Event{Subject: "bob", Perm: "export", Granted: false})
log.Append(Event{Subject: "alice", Perm: "refund", Granted: true})
c := NewPushConsumer(log)
c.rebuildSynchronously()
if !c.Allowed("alice", "export") {
t.Error("alice/export should be granted")
}
if c.Allowed("bob", "export") {
t.Error("bob/export should not be granted")
}
if !c.Allowed("alice", "refund") {
t.Error("alice/refund should be granted")
}
if c.Allowed("alice", "unknown") {
t.Error("unknown perm should default to not granted")
}
}
// apply must be idempotent on Seq: replaying an already-seen event changes nothing
// and does not move the cursor backward. This is what makes the replay/live seam
// and at-least-once delivery safe.
func TestProjection_ApplyIsIdempotent(t *testing.T) {
log := NewEventLog()
e := log.Append(Event{Subject: "u", Perm: "p", Granted: true})
c := NewPushConsumer(log)
c.rebuildSynchronously()
before := c.Cursor()
c.apply(e) // same event again
c.apply(e) // and again
if c.Cursor() != before {
t.Fatalf("cursor moved on replay of seen event: %d -> %d", before, c.Cursor())
}
if !c.Allowed("u", "p") {
t.Error("value should be unchanged after idempotent re-apply")
}
}
// An out-of-order (older) event must not clobber a newer applied one.
func TestProjection_StaleEventIgnored(t *testing.T) {
log := NewEventLog()
older := log.Append(Event{Subject: "u", Perm: "p", Granted: true})
log.Append(Event{Subject: "u", Perm: "p", Granted: false})
c := NewPushConsumer(log)
c.rebuildSynchronously() // now at revoke (seq 1), view = not granted
c.apply(older) // seq 0 arriving late must be ignored
if c.Allowed("u", "p") {
t.Error("stale older event must not revive a superseded grant")
}
}
// --- Replay / recovery path ---
// A consumer that boots with the owner unreachable must still rebuild its full
// view from the log alone. We model "owner down" by simply never giving the
// consumer an owner reference — the push read path has none by construction.
func TestRecovery_RebuildWithoutOwner(t *testing.T) {
log := NewEventLog()
owner := NewOwner(log)
owner.Grant("u", "p1")
owner.Grant("u", "p2")
owner.Revoke("u", "p1")
owner.SetDown(true) // owner is now unreachable for lookups
// Fresh consumer, built only from the log, with the owner down.
c := NewPushConsumer(log)
c.rebuildSynchronously()
if c.Allowed("u", "p1") {
t.Error("p1 was revoked; rebuilt view should show not granted")
}
if !c.Allowed("u", "p2") {
t.Error("p2 was granted; rebuilt view should show granted")
}
if c.Cursor() != 2 {
t.Fatalf("expected to replay 3 events (cursor 2), got %d", c.Cursor())
}
}
// Start must stitch replay + live with no gap and no double-apply: events that
// exist before Start are replayed; events appended after Start arrive live; an
// event is never lost or applied in a way that corrupts the view.
func TestRecovery_ReplayThenLiveNoGap(t *testing.T) {
log := NewEventLog()
log.Append(Event{Subject: "u", Perm: "p", Granted: true}) // pre-existing history
c := NewPushConsumer(log)
c.Start()
defer c.Stop()
// live event after Start
log.Append(Event{Subject: "u", Perm: "q", Granted: true})
deadline := time.After(time.Second)
for {
if c.Allowed("u", "p") && c.Allowed("u", "q") {
break // both the replayed and the live event are present
}
select {
case <-deadline:
t.Fatalf("live event not applied: p=%v q=%v",
c.Allowed("u", "p"), c.Allowed("u", "q"))
default:
time.Sleep(time.Millisecond)
}
}
}
// The log's replay primitive must return events strictly after the cursor, in
// order — the contract Read(after) promises.
func TestEventLog_ReadAfter(t *testing.T) {
log := NewEventLog()
log.Append(Event{Subject: "u", Perm: "a", Granted: true}) // seq 0
log.Append(Event{Subject: "u", Perm: "b", Granted: true}) // seq 1
log.Append(Event{Subject: "u", Perm: "c", Granted: true}) // seq 2
got := log.Read(0) // strictly after seq 0 -> seq 1,2
if len(got) != 2 || got[0].Seq != 1 || got[1].Seq != 2 {
t.Fatalf("Read(0) returned wrong slice: %+v", got)
}
if all := log.Read(-1); len(all) != 3 {
t.Fatalf("Read(-1) should return all 3, got %d", len(all))
}
}