-
Notifications
You must be signed in to change notification settings - Fork 540
Expand file tree
/
Copy pathChangeFeedProcessorCoreTests.cs
More file actions
702 lines (609 loc) · 34.1 KB
/
ChangeFeedProcessorCoreTests.cs
File metadata and controls
702 lines (609 loc) · 34.1 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
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
//------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
//------------------------------------------------------------
namespace Microsoft.Azure.Cosmos.ChangeFeed.Tests
{
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Cosmos.ChangeFeed.Configuration;
using Microsoft.Azure.Cosmos.ChangeFeed.LeaseManagement;
using Microsoft.Azure.Cosmos.Tests;
using Microsoft.Azure.Cosmos.Tracing;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Moq;
[TestClass]
[TestCategory("ChangeFeed")]
public class ChangeFeedProcessorCoreTests
{
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ApplyBuildConfiguration_ValidatesNullStore()
{
ChangeFeedProcessorCore processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
null,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
new ChangeFeedProcessorOptions(),
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ApplyBuildConfiguration_ValidatesNullInstance()
{
ChangeFeedProcessorCore processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
Mock.Of<DocumentServiceLeaseStoreManager>(),
null,
null,
new ChangeFeedLeaseOptions(),
new ChangeFeedProcessorOptions(),
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
}
[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
public void ApplyBuildConfiguration_ValidatesNullMonitoredContainer()
{
ChangeFeedProcessorCore processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
Mock.Of<DocumentServiceLeaseStoreManager>(),
null,
"instanceName",
new ChangeFeedLeaseOptions(),
new ChangeFeedProcessorOptions(),
null);
}
[TestMethod]
public void ApplyBuildConfiguration_ValidCustomStore()
{
ChangeFeedProcessorCore processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
Mock.Of<DocumentServiceLeaseStoreManager>(),
null,
"instanceName",
new ChangeFeedLeaseOptions(),
new ChangeFeedProcessorOptions(),
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
}
[TestMethod]
public void ApplyBuildConfiguration_ValidContainerStore()
{
ChangeFeedProcessorCore processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
null,
ChangeFeedProcessorCoreTests.GetMockedContainer("leases"),
"instanceName",
new ChangeFeedLeaseOptions(),
new ChangeFeedProcessorOptions(),
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
}
[TestMethod]
public async Task StartAsync()
{
Mock<DocumentServiceLeaseStore> leaseStore = new Mock<DocumentServiceLeaseStore>();
leaseStore.Setup(l => l.IsInitializedAsync()).ReturnsAsync(true);
Mock<DocumentServiceLeaseContainer> leaseContainer = new Mock<DocumentServiceLeaseContainer>();
leaseContainer.Setup(l => l.GetOwnedLeasesAsync()).Returns(Task.FromResult(Enumerable.Empty<DocumentServiceLease>()));
leaseContainer.Setup(l => l.GetAllLeasesAsync()).ReturnsAsync(new List<DocumentServiceLease>());
Mock<DocumentServiceLeaseStoreManager> leaseStoreManager = new Mock<DocumentServiceLeaseStoreManager>();
leaseStoreManager.Setup(l => l.LeaseContainer).Returns(leaseContainer.Object);
leaseStoreManager.Setup(l => l.LeaseManager).Returns(Mock.Of<DocumentServiceLeaseManager>);
leaseStoreManager.Setup(l => l.LeaseStore).Returns(leaseStore.Object);
leaseStoreManager.Setup(l => l.LeaseCheckpointer).Returns(Mock.Of<DocumentServiceLeaseCheckpointer>);
ChangeFeedProcessorCore processor = null;
try
{
processor = ChangeFeedProcessorCoreTests.CreateProcessor(out Mock<ChangeFeedObserverFactory> factory, out Mock<ChangeFeedObserver> observer);
processor.ApplyBuildConfiguration(
leaseStoreManager.Object,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
new ChangeFeedProcessorOptions(),
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
await processor.StartAsync();
Mock.Get(leaseStore.Object)
.Verify(store => store.IsInitializedAsync(), Times.Once);
Mock.Get(leaseContainer.Object)
.Verify(store => store.GetOwnedLeasesAsync(), Times.Once);
}
finally
{
if (processor != null)
{
await processor.StopAsync();
}
}
}
[TestMethod]
public async Task StartAsync_SetsStartTime_WhenNoStartOptionsProvided()
{
Mock<DocumentServiceLeaseStore> leaseStore = new Mock<DocumentServiceLeaseStore>();
leaseStore.Setup(l => l.IsInitializedAsync()).ReturnsAsync(true);
Mock<DocumentServiceLeaseContainer> leaseContainer = new Mock<DocumentServiceLeaseContainer>();
leaseContainer.Setup(l => l.GetOwnedLeasesAsync()).Returns(Task.FromResult(Enumerable.Empty<DocumentServiceLease>()));
leaseContainer.Setup(l => l.GetAllLeasesAsync()).ReturnsAsync(new List<DocumentServiceLease>());
Mock<DocumentServiceLeaseStoreManager> leaseStoreManager = new Mock<DocumentServiceLeaseStoreManager>();
leaseStoreManager.Setup(l => l.LeaseContainer).Returns(leaseContainer.Object);
leaseStoreManager.Setup(l => l.LeaseManager).Returns(Mock.Of<DocumentServiceLeaseManager>);
leaseStoreManager.Setup(l => l.LeaseStore).Returns(leaseStore.Object);
leaseStoreManager.Setup(l => l.LeaseCheckpointer).Returns(Mock.Of<DocumentServiceLeaseCheckpointer>);
ChangeFeedProcessorOptions options = new ChangeFeedProcessorOptions();
ChangeFeedProcessorCore processor = null;
try
{
processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
leaseStoreManager.Object,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
options,
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
await processor.StartAsync();
Assert.IsTrue(options.StartTime.HasValue);
Assert.AreEqual(DateTimeKind.Utc, options.StartTime.Value.Kind);
}
finally
{
if (processor != null)
{
await processor.StopAsync();
}
}
}
[TestMethod]
public async Task StartAsync_DoesNotOverrideExplicitStartTime()
{
Mock<DocumentServiceLeaseStore> leaseStore = new Mock<DocumentServiceLeaseStore>();
leaseStore.Setup(l => l.IsInitializedAsync()).ReturnsAsync(true);
Mock<DocumentServiceLeaseContainer> leaseContainer = new Mock<DocumentServiceLeaseContainer>();
leaseContainer.Setup(l => l.GetOwnedLeasesAsync()).Returns(Task.FromResult(Enumerable.Empty<DocumentServiceLease>()));
leaseContainer.Setup(l => l.GetAllLeasesAsync()).ReturnsAsync(new List<DocumentServiceLease>());
Mock<DocumentServiceLeaseStoreManager> leaseStoreManager = new Mock<DocumentServiceLeaseStoreManager>();
leaseStoreManager.Setup(l => l.LeaseContainer).Returns(leaseContainer.Object);
leaseStoreManager.Setup(l => l.LeaseManager).Returns(Mock.Of<DocumentServiceLeaseManager>);
leaseStoreManager.Setup(l => l.LeaseStore).Returns(leaseStore.Object);
leaseStoreManager.Setup(l => l.LeaseCheckpointer).Returns(Mock.Of<DocumentServiceLeaseCheckpointer>);
DateTime explicitStartTime = DateTime.UtcNow.AddMinutes(-5);
ChangeFeedProcessorOptions options = new ChangeFeedProcessorOptions
{
StartTime = explicitStartTime,
};
ChangeFeedProcessorCore processor = null;
try
{
processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
leaseStoreManager.Object,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
options,
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
await processor.StartAsync();
Assert.AreEqual(explicitStartTime, options.StartTime);
}
finally
{
if (processor != null)
{
await processor.StopAsync();
}
}
}
[TestMethod]
public async Task StartAsync_DoesNotSetStartTime_WhenStartFromBeginning()
{
Mock<DocumentServiceLeaseStore> leaseStore = new Mock<DocumentServiceLeaseStore>();
leaseStore.Setup(l => l.IsInitializedAsync()).ReturnsAsync(true);
Mock<DocumentServiceLeaseContainer> leaseContainer = new Mock<DocumentServiceLeaseContainer>();
leaseContainer.Setup(l => l.GetOwnedLeasesAsync()).Returns(Task.FromResult(Enumerable.Empty<DocumentServiceLease>()));
leaseContainer.Setup(l => l.GetAllLeasesAsync()).ReturnsAsync(new List<DocumentServiceLease>());
Mock<DocumentServiceLeaseStoreManager> leaseStoreManager = new Mock<DocumentServiceLeaseStoreManager>();
leaseStoreManager.Setup(l => l.LeaseContainer).Returns(leaseContainer.Object);
leaseStoreManager.Setup(l => l.LeaseManager).Returns(Mock.Of<DocumentServiceLeaseManager>);
leaseStoreManager.Setup(l => l.LeaseStore).Returns(leaseStore.Object);
leaseStoreManager.Setup(l => l.LeaseCheckpointer).Returns(Mock.Of<DocumentServiceLeaseCheckpointer>);
ChangeFeedProcessorOptions options = new ChangeFeedProcessorOptions
{
StartFromBeginning = true,
};
ChangeFeedProcessorCore processor = null;
try
{
processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
leaseStoreManager.Object,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
options,
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
await processor.StartAsync();
Assert.IsNull(options.StartTime);
}
finally
{
if (processor != null)
{
await processor.StopAsync();
}
}
}
// Defends #5268 (https://github.com/Azure/azure-cosmos-dotnet-v3/issues/5268) — AC7.
// Symmetric companion to the AVAD test below: explicitly sets Mode = LatestVersion (rather
// than relying on the default at ChangeFeedProcessorOptions.cs) so that a future contributor
// over-broadening the Mode != AllVersionsAndDeletes guard regresses #5268 loudly.
//
// Assertion uses start/end-of-call bracket bounds instead of a fixed-tolerance window so the
// test cannot be invalidated by CI slowness (a future flake here would silently quarantine
// the regression fence — see PR #5852 review for context).
[TestMethod]
public async Task StartAsync_SetsStartTime_WhenLatestVersionMode_Explicit()
{
Mock<DocumentServiceLeaseStore> leaseStore = new Mock<DocumentServiceLeaseStore>();
leaseStore.Setup(l => l.IsInitializedAsync()).ReturnsAsync(true);
Mock<DocumentServiceLeaseContainer> leaseContainer = new Mock<DocumentServiceLeaseContainer>();
leaseContainer.Setup(l => l.GetOwnedLeasesAsync()).Returns(Task.FromResult(Enumerable.Empty<DocumentServiceLease>()));
leaseContainer.Setup(l => l.GetAllLeasesAsync()).ReturnsAsync(new List<DocumentServiceLease>());
Mock<DocumentServiceLeaseStoreManager> leaseStoreManager = new Mock<DocumentServiceLeaseStoreManager>();
leaseStoreManager.Setup(l => l.LeaseContainer).Returns(leaseContainer.Object);
leaseStoreManager.Setup(l => l.LeaseManager).Returns(Mock.Of<DocumentServiceLeaseManager>);
leaseStoreManager.Setup(l => l.LeaseStore).Returns(leaseStore.Object);
leaseStoreManager.Setup(l => l.LeaseCheckpointer).Returns(Mock.Of<DocumentServiceLeaseCheckpointer>);
ChangeFeedProcessorOptions options = new ChangeFeedProcessorOptions
{
Mode = ChangeFeedMode.LatestVersion,
};
ChangeFeedProcessorCore processor = null;
try
{
processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
leaseStoreManager.Object,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
options,
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
DateTime lowerBound = DateTime.UtcNow.AddSeconds(-1);
await processor.StartAsync();
DateTime upperBound = DateTime.UtcNow.AddSeconds(-1).AddMilliseconds(1);
Assert.IsTrue(options.StartTime.HasValue);
Assert.AreEqual(DateTimeKind.Utc, options.StartTime.Value.Kind);
Assert.IsTrue(
options.StartTime.Value >= lowerBound,
$"StartTime {options.StartTime.Value:O} is earlier than pre-call lowerBound {lowerBound:O}.");
Assert.IsTrue(
options.StartTime.Value <= upperBound,
$"StartTime {options.StartTime.Value:O} is later than post-call upperBound {upperBound:O}.");
}
finally
{
if (processor != null)
{
await processor.StopAsync();
}
}
}
// Defends #5268 (https://github.com/Azure/azure-cosmos-dotnet-v3/issues/5268) AND
// #5846 (https://github.com/Azure/azure-cosmos-dotnet-v3/issues/5846).
//
// AVAD push processor cold-start MUST NOT have StartTime backfilled by the #5617 guard:
// 1. AVAD uses LSN-based continuation (IfNoneMatch: *), not RFC1123 IfModifiedSince,
// so the seconds-precision rounding issue from #5268 does not apply.
// 2. The AVAD endpoint rejects an explicit StartTime on a null-continuation lease with
// HTTP 400 — the regression introduced by #5617 and tracked as #5846.
//
// Paired with the LatestVersion test above, this forms a dual-fence: dropping the mode
// guard regresses #5846 (this test fails); over-broadening it regresses #5268 (the
// LatestVersion test fails). Do not delete either without re-validating both issues.
// Functional guard landed via PR #5825; this PR (#5852) carries the cited comment fence
// and customer-facing changelog entry.
[TestMethod]
public async Task StartAsync_DoesNotSetStartTime_WhenAllVersionsAndDeletes()
{
Mock<DocumentServiceLeaseStore> leaseStore = new Mock<DocumentServiceLeaseStore>();
leaseStore.Setup(l => l.IsInitializedAsync()).ReturnsAsync(true);
Mock<DocumentServiceLeaseContainer> leaseContainer = new Mock<DocumentServiceLeaseContainer>();
leaseContainer.Setup(l => l.GetOwnedLeasesAsync()).Returns(Task.FromResult(Enumerable.Empty<DocumentServiceLease>()));
leaseContainer.Setup(l => l.GetAllLeasesAsync()).ReturnsAsync(new List<DocumentServiceLease>());
Mock<DocumentServiceLeaseStoreManager> leaseStoreManager = new Mock<DocumentServiceLeaseStoreManager>();
leaseStoreManager.Setup(l => l.LeaseContainer).Returns(leaseContainer.Object);
leaseStoreManager.Setup(l => l.LeaseManager).Returns(Mock.Of<DocumentServiceLeaseManager>);
leaseStoreManager.Setup(l => l.LeaseStore).Returns(leaseStore.Object);
leaseStoreManager.Setup(l => l.LeaseCheckpointer).Returns(Mock.Of<DocumentServiceLeaseCheckpointer>);
ChangeFeedProcessorOptions options = new ChangeFeedProcessorOptions
{
Mode = ChangeFeedMode.AllVersionsAndDeletes,
};
ChangeFeedProcessorCore processor = null;
try
{
processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
leaseStoreManager.Object,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
options,
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
await processor.StartAsync();
Assert.IsNull(
options.StartTime,
"StartTime must remain null for AllVersionsAndDeletes mode (see leading comment).");
}
finally
{
if (processor != null)
{
await processor.StopAsync();
}
}
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public async Task StartAsync_ThrowsWhenAllVersionsAndDeletes_WithStartFromBeginning()
{
ChangeFeedProcessorOptions options = new ChangeFeedProcessorOptions
{
Mode = ChangeFeedMode.AllVersionsAndDeletes,
StartFromBeginning = true,
};
Mock<DocumentServiceLeaseStoreManager> leaseStoreManager = new Mock<DocumentServiceLeaseStoreManager>();
leaseStoreManager.Setup(l => l.LeaseContainer).Returns(Mock.Of<DocumentServiceLeaseContainer>);
leaseStoreManager.Setup(l => l.LeaseManager).Returns(Mock.Of<DocumentServiceLeaseManager>);
leaseStoreManager.Setup(l => l.LeaseStore).Returns(Mock.Of<DocumentServiceLeaseStore>);
leaseStoreManager.Setup(l => l.LeaseCheckpointer).Returns(Mock.Of<DocumentServiceLeaseCheckpointer>);
ChangeFeedProcessorCore processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
leaseStoreManager.Object,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
options,
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
await processor.StartAsync();
}
[TestMethod]
[ExpectedException(typeof(InvalidOperationException))]
public async Task StartAsync_ThrowsWhenAllVersionsAndDeletes_WithStartTime()
{
ChangeFeedProcessorOptions options = new ChangeFeedProcessorOptions
{
Mode = ChangeFeedMode.AllVersionsAndDeletes,
StartTime = DateTime.UtcNow,
};
Mock<DocumentServiceLeaseStoreManager> leaseStoreManager = new Mock<DocumentServiceLeaseStoreManager>();
leaseStoreManager.Setup(l => l.LeaseContainer).Returns(Mock.Of<DocumentServiceLeaseContainer>);
leaseStoreManager.Setup(l => l.LeaseManager).Returns(Mock.Of<DocumentServiceLeaseManager>);
leaseStoreManager.Setup(l => l.LeaseStore).Returns(Mock.Of<DocumentServiceLeaseStore>);
leaseStoreManager.Setup(l => l.LeaseCheckpointer).Returns(Mock.Of<DocumentServiceLeaseCheckpointer>);
ChangeFeedProcessorCore processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
leaseStoreManager.Object,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
options,
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
await processor.StartAsync();
}
[TestMethod]
public async Task ObserverIsCreated()
{
IEnumerable<DocumentServiceLease> ownedLeases = new List<DocumentServiceLease>()
{
new DocumentServiceLeaseCore()
{
LeaseId = "0",
LeaseToken = "0"
}
};
Mock<DocumentServiceLeaseStore> leaseStore = new Mock<DocumentServiceLeaseStore>();
leaseStore.Setup(l => l.IsInitializedAsync()).ReturnsAsync(true);
Mock<DocumentServiceLeaseContainer> leaseContainer = new Mock<DocumentServiceLeaseContainer>();
leaseContainer.Setup(l => l.GetOwnedLeasesAsync()).Returns(Task.FromResult(ownedLeases));
leaseContainer.Setup(l => l.GetAllLeasesAsync()).ReturnsAsync(new List<DocumentServiceLease>());
Mock<DocumentServiceLeaseStoreManager> leaseStoreManager = new Mock<DocumentServiceLeaseStoreManager>();
leaseStoreManager.Setup(l => l.LeaseContainer).Returns(leaseContainer.Object);
leaseStoreManager.Setup(l => l.LeaseManager).Returns(Mock.Of<DocumentServiceLeaseManager>);
leaseStoreManager.Setup(l => l.LeaseStore).Returns(leaseStore.Object);
leaseStoreManager.Setup(l => l.LeaseCheckpointer).Returns(Mock.Of<DocumentServiceLeaseCheckpointer>);
ChangeFeedProcessorCore processor = null;
try
{
processor = ChangeFeedProcessorCoreTests.CreateProcessor(out Mock<ChangeFeedObserverFactory> factory, out Mock<ChangeFeedObserver> observer);
processor.ApplyBuildConfiguration(
leaseStoreManager.Object,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
new ChangeFeedProcessorOptions(),
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
await processor.StartAsync();
Mock.Get(factory.Object)
.Verify(mock => mock.CreateObserver(), Times.Once);
Mock.Get(observer.Object)
.Verify(mock => mock.OpenAsync(It.Is<string>((lt) => lt == ownedLeases.First().CurrentLeaseToken)), Times.Once);
}
finally
{
if (processor != null)
{
await processor.StopAsync();
}
}
}
[TestMethod]
public async Task StopAsync()
{
Mock<DocumentServiceLeaseStore> leaseStore = new Mock<DocumentServiceLeaseStore>();
leaseStore.Setup(l => l.IsInitializedAsync()).ReturnsAsync(true);
Mock<DocumentServiceLeaseContainer> leaseContainer = new Mock<DocumentServiceLeaseContainer>();
leaseContainer.Setup(l => l.GetOwnedLeasesAsync()).Returns(Task.FromResult(Enumerable.Empty<DocumentServiceLease>()));
leaseContainer.Setup(l => l.GetAllLeasesAsync()).ReturnsAsync(new List<DocumentServiceLease>());
Mock<DocumentServiceLeaseStoreManager> leaseStoreManager = new Mock<DocumentServiceLeaseStoreManager>();
leaseStoreManager.Setup(l => l.LeaseContainer).Returns(leaseContainer.Object);
leaseStoreManager.Setup(l => l.LeaseManager).Returns(Mock.Of<DocumentServiceLeaseManager>);
leaseStoreManager.Setup(l => l.LeaseStore).Returns(leaseStore.Object);
leaseStoreManager.Setup(l => l.LeaseCheckpointer).Returns(Mock.Of<DocumentServiceLeaseCheckpointer>);
ChangeFeedProcessorCore processor = ChangeFeedProcessorCoreTests.CreateProcessor(out Mock<ChangeFeedObserverFactory> factory, out Mock<ChangeFeedObserver> observer);
processor.ApplyBuildConfiguration(
leaseStoreManager.Object,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
new ChangeFeedProcessorOptions(),
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
await processor.StartAsync();
await processor.StopAsync();
Mock.Get(leaseContainer.Object)
.Verify(store => store.GetAllLeasesAsync(), Times.Exactly(2));
}
[TestMethod]
public async Task StopAsync_CallsShutdownAsync()
{
Mock<DocumentServiceLeaseStore> leaseStore = new Mock<DocumentServiceLeaseStore>();
leaseStore.Setup(l => l.IsInitializedAsync()).ReturnsAsync(true);
Mock<DocumentServiceLeaseContainer> leaseContainer = new Mock<DocumentServiceLeaseContainer>();
leaseContainer.Setup(l => l.GetOwnedLeasesAsync()).Returns(Task.FromResult(Enumerable.Empty<DocumentServiceLease>()));
leaseContainer.Setup(l => l.GetAllLeasesAsync()).ReturnsAsync(new List<DocumentServiceLease>());
Mock<DocumentServiceLeaseStoreManager> leaseStoreManager = new Mock<DocumentServiceLeaseStoreManager>();
leaseStoreManager.Setup(l => l.LeaseContainer).Returns(leaseContainer.Object);
leaseStoreManager.Setup(l => l.LeaseManager).Returns(Mock.Of<DocumentServiceLeaseManager>);
leaseStoreManager.Setup(l => l.LeaseStore).Returns(leaseStore.Object);
leaseStoreManager.Setup(l => l.LeaseCheckpointer).Returns(Mock.Of<DocumentServiceLeaseCheckpointer>);
leaseStoreManager.Setup(l => l.ShutdownAsync()).Returns(Task.CompletedTask);
ChangeFeedProcessorCore processor = ChangeFeedProcessorCoreTests.CreateProcessor(out Mock<ChangeFeedObserverFactory> factory, out Mock<ChangeFeedObserver> observer);
processor.ApplyBuildConfiguration(
leaseStoreManager.Object,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
new ChangeFeedProcessorOptions(),
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
await processor.StartAsync();
await processor.StopAsync();
leaseStoreManager
.Verify(store => store.ShutdownAsync(), Times.Once);
}
[TestMethod]
public async Task StopAsync_WithInMemoryLeases_PersistsStateToStream()
{
// Arrange — real in-memory store with a real MemoryStream
DocumentServiceLeaseCoreEpk lease = new DocumentServiceLeaseCoreEpk
{
LeaseId = "e2e-lease",
LeaseToken = "0",
ContinuationToken = "e2e-continuation",
Owner = "e2e-owner",
FeedRange = new FeedRangeEpk(new Documents.Routing.Range<string>("", "FF", true, false))
};
ConcurrentDictionary<string, DocumentServiceLease> container = new ConcurrentDictionary<string, DocumentServiceLease>();
container.TryAdd(lease.Id, lease);
MemoryStream leaseState = new MemoryStream();
DocumentServiceLeaseStoreManagerInMemory storeManager = new DocumentServiceLeaseStoreManagerInMemory(container, leaseState);
ChangeFeedProcessorCore processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
storeManager,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
new ChangeFeedProcessorOptions(),
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
// Act — full lifecycle: start → stop (which triggers ShutdownAsync → persist)
await processor.StartAsync();
await processor.StopAsync();
// Assert — stream is usable and contains valid serialized lease state
Assert.IsTrue(leaseState.CanRead, "Stream should still be readable after StopAsync");
Assert.IsTrue(leaseState.Length > 0, "Stream should contain serialized lease data");
// Verify the persisted data deserializes correctly
leaseState.Position = 0;
using (StreamReader sr = new StreamReader(leaseState, leaveOpen: true))
using (Newtonsoft.Json.JsonTextReader jsonReader = new Newtonsoft.Json.JsonTextReader(sr))
{
List<DocumentServiceLease> persisted = Newtonsoft.Json.JsonSerializer.Create()
.Deserialize<List<DocumentServiceLease>>(jsonReader);
Assert.AreEqual(1, persisted.Count);
Assert.AreEqual("e2e-lease", persisted[0].Id);
Assert.AreEqual("e2e-continuation", persisted[0].ContinuationToken);
Assert.IsNotNull(persisted[0].FeedRange);
Assert.IsInstanceOfType(persisted[0].FeedRange, typeof(FeedRangeEpk));
}
}
[TestMethod]
public async Task StopAsync_WhenShutdownAsyncThrows_ExceptionPropagates()
{
// Arrange — set up a processor where ShutdownAsync throws
Mock<DocumentServiceLeaseStore> leaseStore = new Mock<DocumentServiceLeaseStore>();
leaseStore.Setup(l => l.IsInitializedAsync()).ReturnsAsync(true);
Mock<DocumentServiceLeaseContainer> leaseContainer = new Mock<DocumentServiceLeaseContainer>();
leaseContainer.Setup(l => l.GetOwnedLeasesAsync()).Returns(Task.FromResult(Enumerable.Empty<DocumentServiceLease>()));
leaseContainer.Setup(l => l.GetAllLeasesAsync()).ReturnsAsync(new List<DocumentServiceLease>());
Mock<DocumentServiceLeaseStoreManager> leaseStoreManager = new Mock<DocumentServiceLeaseStoreManager>();
leaseStoreManager.Setup(l => l.LeaseContainer).Returns(leaseContainer.Object);
leaseStoreManager.Setup(l => l.LeaseManager).Returns(Mock.Of<DocumentServiceLeaseManager>);
leaseStoreManager.Setup(l => l.LeaseStore).Returns(leaseStore.Object);
leaseStoreManager.Setup(l => l.LeaseCheckpointer).Returns(Mock.Of<DocumentServiceLeaseCheckpointer>);
leaseStoreManager.Setup(l => l.ShutdownAsync()).ThrowsAsync(new InvalidOperationException("Shutdown failed"));
ChangeFeedProcessorCore processor = ChangeFeedProcessorCoreTests.CreateProcessor(out _, out _);
processor.ApplyBuildConfiguration(
leaseStoreManager.Object,
null,
"instanceName",
new ChangeFeedLeaseOptions(),
new ChangeFeedProcessorOptions(),
ChangeFeedProcessorCoreTests.GetMockedContainer("monitored"));
await processor.StartAsync();
// Act & Assert — StopAsync propagates ShutdownAsync exceptions so callers
// know persistence failed.
InvalidOperationException ex = await Assert.ThrowsExceptionAsync<InvalidOperationException>(
() => processor.StopAsync());
Assert.AreEqual("Shutdown failed", ex.Message);
// Assert — ShutdownAsync was still invoked
leaseStoreManager.Verify(l => l.ShutdownAsync(), Times.Once);
}
private static ChangeFeedProcessorCore CreateProcessor(
out Mock<ChangeFeedObserverFactory> factory,
out Mock<ChangeFeedObserver> observer)
{
factory = new Mock<ChangeFeedObserverFactory>();
observer = new Mock<ChangeFeedObserver>();
factory.Setup(f => f.CreateObserver()).Returns(observer.Object);
return new ChangeFeedProcessorCore(factory.Object);
}
public class MyDocument
{
public string id { get; set; }
}
private static ContainerInternal GetMockedContainer(string containerName = null)
{
Mock<ContainerInternal> mockedContainer = MockCosmosUtil.CreateMockContainer(containerName: containerName);
mockedContainer.Setup(c => c.ClientContext).Returns(ChangeFeedProcessorCoreTests.GetMockedClientContext());
string monitoredContainerRid = "V4lVAMl0wuQ=";
mockedContainer.Setup(c => c.GetCachedRIDAsync(It.IsAny<bool>(), It.IsAny<ITrace>(), It.IsAny<CancellationToken>())).ReturnsAsync(monitoredContainerRid);
Mock<DatabaseInternal> mockedDatabase = MockCosmosUtil.CreateMockDatabase();
mockedContainer.Setup(c => c.Database).Returns(mockedDatabase.Object);
return mockedContainer.Object;
}
private static CosmosClientContext GetMockedClientContext()
{
Mock<CosmosClient> mockClient = new Mock<CosmosClient>();
mockClient.Setup(x => x.Endpoint).Returns(new Uri("http://localhost"));
Mock<CosmosClientContext> mockContext = new Mock<CosmosClientContext>();
mockContext.Setup(x => x.ClientOptions).Returns(MockCosmosUtil.GetDefaultConfiguration());
mockContext.Setup(x => x.DocumentClient).Returns(new MockDocumentClient());
mockContext.Setup(x => x.SerializerCore).Returns(MockCosmosUtil.Serializer);
mockContext.Setup(x => x.Client).Returns(mockClient.Object);
return mockContext.Object;
}
}
}