-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathStreamingTest.cs
289 lines (236 loc) · 11.4 KB
/
StreamingTest.cs
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using FaunaDB.Client;
using FaunaDB.Errors;
using FaunaDB.Types;
using NUnit.Framework;
using static FaunaDB.Query.Language;
using static Test.ClientTest;
namespace Test
{
public class StreamingTest : TestCase
{
[OneTimeSetUp]
public new void SetUp()
{
SetUpAsync().Wait();
}
private async Task SetUpAsync()
{
await adminClient.Query(CreateCollection(Obj("name", "streams_test")));
}
[Test]
public void TestThatStreamFailsIfTargetDoesNotExist()
{
AsyncTestDelegate doc = async () => { await adminClient.Stream(Get(Ref(Collection("streams_test"), "1234"))); };
var ex = Assert.ThrowsAsync<NotFound>(doc);
Assert.AreEqual("instance not found: Document not found.", ex.Message);
AssertErrors(ex, code: "instance not found", description: "Document not found.");
}
[Test]
public void TestStreamFailsIfIncorrectValuePassedToStreamMethod()
{
AsyncTestDelegate doc = async () => { await adminClient.Stream(Collection("streams_test")); };
var ex = Assert.ThrowsAsync<BadRequest>(doc);
Assert.AreEqual("invalid argument: Expected a Document Ref or Version, or a Set Ref, got Collection Ref.", ex.Message);
AssertErrors(ex, code: "invalid argument", description: "Expected a Document Ref or Version, or a Set Ref, got Collection Ref.");
}
[Test]
public void TestStreamFailsIfQueryIsNotReadOnly()
{
AsyncTestDelegate doc = async () => { await adminClient.Stream(CreateCollection(Collection("streams_test"))); };
var ex = Assert.ThrowsAsync<BadRequest>(doc);
Assert.AreEqual("invalid expression: Write effect in read-only query expression.", ex.Message);
AssertErrors(ex, code: "invalid expression", description: "Write effect in read-only query expression.");
}
[Test]
public async Task TestStreamEventsOnDocumentReferenceWithDocumentFieldByDefault()
{
Value createdInstance = await adminClient.Query(
Create(await RandomCollection(),
Obj("credentials",
Obj("password", "abcdefg"))));
var docRef = createdInstance.At("ref");
var provider = await adminClient.Stream(docRef);
var done = new TaskCompletionSource<object>();
List<Value> events = new List<Value>();
var monitor = new StreamingEventMonitor(
value =>
{
events.Add(value);
if (events.Count == 4)
{
provider.Complete();
}
else
{
provider.RequestData();
}
},
ex => { done.SetException(ex); },
() => { done.SetResult(null); }
);
// subscribe to data provider
monitor.Subscribe(provider);
// push 3 updates
await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue1"))));
await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue2"))));
await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue3"))));
// blocking until we receive all the events
await done.Task;
// clear the subscription
monitor.Unsubscribe();
Value startEvent = events[0];
Assert.AreEqual("start", startEvent.At("type").To<string>().Value);
Value e1 = events[1];
Assert.AreEqual("version", e1.At("type").To<string>().Value);
Assert.AreEqual("testValue1", e1.At("event", "document", "data", "testField").To<string>().Value);
Value e2 = events[2];
Assert.AreEqual("version", e1.At("type").To<string>().Value);
Assert.AreEqual("testValue2", e2.At("event", "document", "data", "testField").To<string>().Value);
Value e3 = events[3];
Assert.AreEqual("version", e1.At("type").To<string>().Value);
Assert.AreEqual("testValue3", e3.At("event", "document", "data", "testField").To<string>().Value);
}
[Test]
public async Task TeststreamEventsOnDocumentReferenceWithOptInFields()
{
Value createdInstance = await adminClient.Query(
Create(await RandomCollection(),
Obj("data",
Obj("testField", "testValue0"))));
var docRef = createdInstance.At("ref");
var fields = new List<EventField>
{
EventField.ActionField,
EventField.DiffField,
EventField.DocumentField,
EventField.PrevField,
};
var provider = await adminClient.Stream(docRef, fields);
var done = new TaskCompletionSource<object>();
List<Value> events = new List<Value>();
var monitor = new StreamingEventMonitor(
value =>
{
events.Add(value);
if (events.Count == 4)
{
provider.Complete();
}
else
{
provider.RequestData();
}
},
ex => { done.SetException(ex); },
() => { done.SetResult(null); }
);
// subscribe to data provider
monitor.Subscribe(provider);
// push 3 updates
await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue1"))));
await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue2"))));
await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "testValue3"))));
// blocking until we receive all the events
await done.Task;
// clear the subscription
monitor.Unsubscribe();
Value startEvent = events[0];
Assert.AreEqual("start", startEvent.At("type").To<string>().Value);
Value e1 = events[1];
Assert.AreEqual("version", e1.At("type").To<string>().Value);
Assert.AreEqual("update", e1.At("event", "action").To<string>().Value);
Assert.AreEqual(
FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue1")),
((ObjectV)e1.At("event", "diff", "data")).Value);
Assert.AreEqual(
FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue1")),
((ObjectV)e1.At("event", "document", "data")).Value);
Assert.AreEqual(
FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue0")),
((ObjectV)e1.At("event", "prev", "data")).Value);
Value e2 = events[2];
Assert.AreEqual("version", e2.At("type").To<string>().Value);
Assert.AreEqual("update", e2.At("event", "action").To<string>().Value);
Assert.AreEqual(
FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue2")),
((ObjectV)e2.At("event", "diff", "data")).Value);
Assert.AreEqual(
FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue2")),
((ObjectV)e2.At("event", "document", "data")).Value);
Assert.AreEqual(
FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue1")),
((ObjectV)e2.At("event", "prev", "data")).Value);
Value e3 = events[3];
Assert.AreEqual("version", e3.At("type").To<string>().Value);
Assert.AreEqual("update", e3.At("event", "action").To<string>().Value);
Assert.AreEqual(
FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue3")),
((ObjectV)e3.At("event", "diff", "data")).Value);
Assert.AreEqual(
FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue3")),
((ObjectV)e3.At("event", "document", "data")).Value);
Assert.AreEqual(
FaunaDB.Collections.ImmutableDictionary.Of("testField", StringV.Of("testValue2")),
((ObjectV)e3.At("event", "prev", "data")).Value);
}
[Test]
public async Task TestStreamHandlesLossOfAuthorization()
{
await adminClient.Query(
CreateCollection(Obj("name", "streamed-things-auth"))
);
Value createdInstance = await adminClient.Query(
Create(Collection("streamed-things-auth"),
Obj("credentials",
Obj("password", "abcdefg"))));
var docRef = createdInstance.At("ref");
// new key + client
Value newKey = await adminClient.Query(CreateKey(Obj("role", "server-readonly")));
FaunaClient streamingClient = adminClient.NewSessionClient(newKey.At("secret").To<string>().Value);
var provider = await streamingClient.Stream(docRef);
var done = new TaskCompletionSource<object>();
List<Value> events = new List<Value>();
var monitor = new StreamingEventMonitor(
async value =>
{
if (events.Count == 0)
{
try
{
// update doc on `start` event
await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "afterStart"))));
// delete key
await adminClient.Query(Delete(newKey.At("ref").To<RefV>().Value));
// push an update to force auth revalidation.
await adminClient.Query(Update(docRef, Obj("data", Obj("testField", "afterKeyDelete"))));
}
catch (Exception ex)
{
done.SetException(ex);
}
}
// capture element
events.Add(value);
// ask for more elements
provider.RequestData();
},
ex => { done.SetException(ex); },
() => { done.SetResult(null); }
);
// subscribe to data provider
monitor.Subscribe(provider);
// wrapping an asynchronous call
AsyncTestDelegate res = async () => await done.Task;
// blocking until we get an exception
var exception = Assert.ThrowsAsync<StreamingException>(res);
// clear the subscription
monitor.Unsubscribe();
// validating exception message
Assert.AreEqual("permission denied: Authorization lost during stream evaluation.", exception.Message);
AssertErrors(exception, code: "permission denied", description: "Authorization lost during stream evaluation.");
}
}
}