/
PartitionSupervisorTests.cs
160 lines (133 loc) · 7.04 KB
/
PartitionSupervisorTests.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
//----------------------------------------------------------------
// Copyright (c) Microsoft Corporation. Licensed under the MIT license.
//----------------------------------------------------------------
namespace Microsoft.Azure.Documents.ChangeFeedProcessor.UnitTests.PartitionManagement
{
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.Documents.ChangeFeedProcessor.Exceptions;
using Microsoft.Azure.Documents.ChangeFeedProcessor.PartitionManagement;
using Microsoft.Azure.Documents.ChangeFeedProcessor.FeedProcessing;
using Moq;
using Xunit;
[Trait("Category", "Gated")]
public class PartitionSupervisorTests : IDisposable
{
private readonly ILease lease;
private readonly ILeaseRenewer leaseRenewer;
private readonly IPartitionProcessor partitionProcessor;
private readonly IChangeFeedObserver observer;
private readonly CancellationTokenSource shutdownToken = new CancellationTokenSource(TimeSpan.FromMinutes(5));
private readonly PartitionSupervisor sut;
public PartitionSupervisorTests()
{
lease = Mock.Of<ILease>();
Mock.Get(lease)
.Setup(l => l.PartitionId)
.Returns("partitionId");
leaseRenewer = Mock.Of<ILeaseRenewer>();
partitionProcessor = Mock.Of<IPartitionProcessor>();
observer = Mock.Of<IChangeFeedObserver>();
sut = new PartitionSupervisor(lease, observer, partitionProcessor, leaseRenewer);
}
[Fact]
public async Task RunObserver_ShouldCancelTasks_WhenTokenCanceled()
{
Task renewerTask = Task.FromResult(false);
Mock.Get(leaseRenewer)
.Setup(renewer => renewer.RunAsync(It.IsAny<CancellationToken>()))
.Returns<CancellationToken>(token => renewerTask = Task.Delay(TimeSpan.FromMinutes(1), token));
Task processorTask = Task.FromResult(false);
Mock.Get(partitionProcessor)
.Setup(processor => processor.RunAsync(It.IsAny<CancellationToken>()))
.Returns<CancellationToken>(token => processorTask = Task.Delay(TimeSpan.FromMinutes(1), token));
Task supervisorTask = sut.RunAsync(shutdownToken.Token);
Task delay = Task.Delay(TimeSpan.FromMilliseconds(100));
Task finished = await Task.WhenAny(supervisorTask, delay).ConfigureAwait(false);
Assert.Equal(delay, finished);
shutdownToken.Cancel();
await supervisorTask.ConfigureAwait(false);
Assert.True(renewerTask.IsCanceled);
Assert.True(processorTask.IsCanceled);
Mock.Get(partitionProcessor)
.Verify(processor => processor.RunAsync(It.IsAny<CancellationToken>()), Times.Once);
Mock.Get(observer)
.Verify(feedObserver => feedObserver
.CloseAsync(It.Is<ChangeFeedObserverContext>(context => context.PartitionKeyRangeId == lease.PartitionId),
ChangeFeedObserverCloseReason.Shutdown));
}
[Fact]
public async Task RunObserver_ShouldCancelProcessor_IfRenewerFailed()
{
Task processorTask = Task.FromResult(false);
Mock.Get(leaseRenewer)
.Setup(renewer => renewer.RunAsync(It.IsAny<CancellationToken>()))
.ThrowsAsync(new LeaseLostException());
Mock.Get(partitionProcessor)
.Setup(processor => processor.RunAsync(It.IsAny<CancellationToken>()))
.Returns<CancellationToken>(token => processorTask = Task.Delay(TimeSpan.FromMinutes(1), token));
Exception exception = await Record.ExceptionAsync(() => sut.RunAsync(shutdownToken.Token)).ConfigureAwait(false);
Assert.IsType<LeaseLostException>(exception);
Assert.True(processorTask.IsCanceled);
Mock.Get(observer)
.Verify(feedObserver => feedObserver
.CloseAsync(It.Is<ChangeFeedObserverContext>(context => context.PartitionKeyRangeId == lease.PartitionId),
ChangeFeedObserverCloseReason.LeaseLost));
}
[Fact]
public async Task RunObserver_ShouldCancelRenewer_IfProcessorFailed()
{
Task renewerTask = Task.FromResult(false);
Mock.Get(leaseRenewer)
.Setup(renewer => renewer.RunAsync(It.IsAny<CancellationToken>()))
.Returns<CancellationToken>(token => renewerTask = Task.Delay(TimeSpan.FromMinutes(1), token));
Mock.Get(partitionProcessor)
.Setup(processor => processor.RunAsync(It.IsAny<CancellationToken>()))
.ThrowsAsync(new Exception("processorException"));
Exception exception = await Record.ExceptionAsync(() => sut.RunAsync(shutdownToken.Token)).ConfigureAwait(false);
Assert.IsType<Exception>(exception);
Assert.Equal("processorException", exception.Message);
Assert.True(renewerTask.IsCanceled);
Mock.Get(observer)
.Verify(feedObserver => feedObserver
.CloseAsync(It.Is<ChangeFeedObserverContext>(context => context.PartitionKeyRangeId == lease.PartitionId),
ChangeFeedObserverCloseReason.Unknown));
}
[Fact]
public async Task RunObserver_ShouldCloseWithObserverError_IfObserverFailed()
{
Mock.Get(partitionProcessor)
.Setup(processor => processor.RunAsync(It.IsAny<CancellationToken>()))
.ThrowsAsync(new ObserverException(new Exception()));
Exception exception = await Record.ExceptionAsync(() => sut.RunAsync(shutdownToken.Token)).ConfigureAwait(false);
Mock.Get(observer)
.Verify(feedObserver => feedObserver
.CloseAsync(It.Is<ChangeFeedObserverContext>(context => context.PartitionKeyRangeId == lease.PartitionId),
ChangeFeedObserverCloseReason.ObserverError));
}
[Fact]
public async Task RunObserver_ShouldPassPartitionToObserver_WhenExecuted()
{
Mock.Get(observer)
.Setup(feedObserver => feedObserver.ProcessChangesAsync(It.IsAny<ChangeFeedObserverContext>(), It.IsAny<IReadOnlyList<Document>>(), It.IsAny<CancellationToken>()))
.Callback(() => shutdownToken.Cancel());
await sut.RunAsync(shutdownToken.Token).ConfigureAwait(false);
Mock.Get(observer)
.Verify(feedObserver => feedObserver
.OpenAsync(It.Is<ChangeFeedObserverContext>(context => context.PartitionKeyRangeId == lease.PartitionId)));
}
[Fact]
public void Dispose_ShouldWork_WithoutRun()
{
Exception exception = Record.Exception(() => sut.Dispose());
Assert.Null(exception);
}
public void Dispose()
{
sut.Dispose();
shutdownToken.Dispose();
}
}
}