-
Notifications
You must be signed in to change notification settings - Fork 245
/
watch.go
216 lines (181 loc) · 6.19 KB
/
watch.go
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package test
import (
"context"
"errors"
"fmt"
"strconv"
"testing"
"time"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/google/go-cmp/cmp"
"github.com/scylladb/go-set/strset"
"github.com/stretchr/testify/require"
"google.golang.org/protobuf/testing/protocmp"
"github.com/authzed/spicedb/internal/datastore/common"
"github.com/authzed/spicedb/pkg/datastore"
core "github.com/authzed/spicedb/pkg/proto/core/v1"
"github.com/authzed/spicedb/pkg/tuple"
)
const waitForChangesTimeout = 5 * time.Second
// WatchTest tests whether or not the requirements for watching changes hold
// for a particular datastore.
func WatchTest(t *testing.T, tester DatastoreTester) {
testCases := []struct {
numTuples int
expectFallBehind bool
}{
{
numTuples: 1,
expectFallBehind: false,
},
{
numTuples: 2,
expectFallBehind: false,
},
{
numTuples: 256,
expectFallBehind: true,
},
}
for _, tc := range testCases {
t.Run(strconv.Itoa(tc.numTuples), func(t *testing.T) {
require := require.New(t)
ds, err := tester.New(0, veryLargeGCInterval, veryLargeGCWindow, 16)
require.NoError(err)
setupDatastore(ds, require)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
lowestRevision, err := ds.HeadRevision(ctx)
require.NoError(err)
changes, errchan := ds.Watch(ctx, lowestRevision)
require.Zero(len(errchan))
var testUpdates [][]*core.RelationTupleUpdate
var bulkDeletes []*core.RelationTupleUpdate
for i := 0; i < tc.numTuples; i++ {
newRelationship := makeTestTuple(fmt.Sprintf("relation%d", i), "test_user")
newUpdate := tuple.Touch(newRelationship)
batch := []*core.RelationTupleUpdate{newUpdate}
testUpdates = append(testUpdates, batch)
_, err := common.UpdateTuplesInDatastore(ctx, ds, newUpdate)
require.NoError(err)
if i != 0 {
bulkDeletes = append(bulkDeletes, tuple.Delete(newRelationship))
}
}
updateUpdate := tuple.Touch(makeTestTuple("relation0", "test_user"))
createUpdate := tuple.Touch(makeTestTuple("another_relation", "somestuff"))
batch := []*core.RelationTupleUpdate{updateUpdate, createUpdate}
_, err = common.UpdateTuplesInDatastore(ctx, ds, batch...)
require.NoError(err)
deleteUpdate := tuple.Delete(makeTestTuple("relation0", "test_user"))
_, err = common.UpdateTuplesInDatastore(ctx, ds, deleteUpdate)
require.NoError(err)
testUpdates = append(testUpdates, batch, []*core.RelationTupleUpdate{deleteUpdate})
_, err = ds.ReadWriteTx(ctx, func(rwt datastore.ReadWriteTransaction) error {
err := rwt.DeleteRelationships(ctx, &v1.RelationshipFilter{
ResourceType: testResourceNamespace,
OptionalRelation: testReaderRelation,
OptionalSubjectFilter: &v1.SubjectFilter{
SubjectType: testUserNamespace,
OptionalSubjectId: "test_user",
},
})
require.NoError(err)
return err
})
require.NoError(err)
if len(bulkDeletes) > 0 {
testUpdates = append(testUpdates, bulkDeletes)
}
verifyUpdates(require, testUpdates, changes, errchan, tc.expectFallBehind)
// Test the catch-up case
changes, errchan = ds.Watch(ctx, lowestRevision)
verifyUpdates(require, testUpdates, changes, errchan, tc.expectFallBehind)
})
}
}
func verifyUpdates(
require *require.Assertions,
testUpdates [][]*core.RelationTupleUpdate,
changes <-chan *datastore.RevisionChanges,
errchan <-chan error,
expectDisconnect bool,
) {
for _, expected := range testUpdates {
changeWait := time.NewTimer(waitForChangesTimeout)
select {
case change, ok := <-changes:
if !ok {
require.True(expectDisconnect, "unexpected disconnect")
errWait := time.NewTimer(waitForChangesTimeout)
select {
case err := <-errchan:
require.True(errors.As(err, &datastore.ErrWatchDisconnected{}))
return
case <-errWait.C:
require.Fail("Timed out waiting for ErrWatchDisconnected")
}
return
}
expectedChangeSet := setOfChanges(expected)
actualChangeSet := setOfChanges(change.Changes)
missingExpected := strset.Difference(expectedChangeSet, actualChangeSet)
unexpected := strset.Difference(actualChangeSet, expectedChangeSet)
require.True(missingExpected.IsEmpty(), "expected changes missing: %s", missingExpected)
require.True(unexpected.IsEmpty(), "unexpected changes: %s", unexpected)
time.Sleep(1 * time.Millisecond)
case <-changeWait.C:
require.Fail("Timed out", "waiting for changes: %s", expected)
}
}
require.False(expectDisconnect, "all changes verified without expected disconnect")
}
func setOfChanges(changes []*core.RelationTupleUpdate) *strset.Set {
changeSet := strset.NewWithSize(len(changes))
for _, change := range changes {
changeSet.Add(fmt.Sprintf("OPERATION_%s(%s)", change.Operation, tuple.StringWithoutCaveat(change.Tuple)))
}
return changeSet
}
// WatchCancelTest tests whether or not the requirements for cancelling watches
// hold for a particular datastore.
func WatchCancelTest(t *testing.T, tester DatastoreTester) {
require := require.New(t)
ds, err := tester.New(0, veryLargeGCInterval, veryLargeGCWindow, 1)
require.NoError(err)
startWatchRevision := setupDatastore(ds, require)
ctx, cancel := context.WithCancel(context.Background())
changes, errchan := ds.Watch(ctx, startWatchRevision)
require.Zero(len(errchan))
_, err = common.WriteTuples(ctx, ds, core.RelationTupleUpdate_CREATE, makeTestTuple("test", "test"))
require.NoError(err)
cancel()
for {
changeWait := time.NewTimer(waitForChangesTimeout)
select {
case created, ok := <-changes:
if ok {
foundDiff := cmp.Diff(
[]*core.RelationTupleUpdate{tuple.Touch(makeTestTuple("test", "test"))},
created.Changes,
protocmp.Transform(),
)
require.Empty(foundDiff)
require.True(created.Revision.GreaterThan(datastore.NoRevision))
} else {
errWait := time.NewTimer(waitForChangesTimeout)
require.Zero(created)
select {
case err := <-errchan:
require.True(errors.As(err, &datastore.ErrWatchCanceled{}))
return
case <-errWait.C:
require.Fail("Timed out waiting for ErrWatchCanceled")
}
return
}
case <-changeWait.C:
require.Fail("deadline exceeded waiting for cancellation")
}
}
}