-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Change Feed Processor: Fixes failures during initialization (#1886)
* errors * tests * removing unused usings Co-authored-by: j82w <j82w@users.noreply.github.com>
- Loading branch information
Showing
2 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...mos/tests/Microsoft.Azure.Cosmos.Tests/ChangeFeed/DocumentServiceLeaseStoreCosmosTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
//------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
//------------------------------------------------------------ | ||
|
||
namespace Microsoft.Azure.Cosmos.ChangeFeed.Tests | ||
{ | ||
using System; | ||
using System.IO; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Azure.Cosmos.ChangeFeed.LeaseManagement; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
|
||
[TestClass] | ||
[TestCategory("ChangeFeed")] | ||
|
||
public class DocumentServiceLeaseStoreCosmosTests | ||
{ | ||
[TestMethod] | ||
public async Task IsInitializedAsync_IfExists() | ||
{ | ||
string prefix = "prefix"; | ||
Mock<Container> container = new Mock<Container>(); | ||
container.Setup(c => c.ReadItemStreamAsync( | ||
It.IsAny<string>(), | ||
It.IsAny<PartitionKey>(), | ||
It.IsAny<ItemRequestOptions>(), | ||
It.IsAny<CancellationToken>())).ReturnsAsync(new ResponseMessage(System.Net.HttpStatusCode.OK)); | ||
Mock<RequestOptionsFactory> requestOptionsFactory = new Mock<RequestOptionsFactory>(); | ||
DocumentServiceLeaseStoreCosmos documentServiceLeaseStoreCosmos = new DocumentServiceLeaseStoreCosmos( | ||
container.Object, | ||
prefix, | ||
requestOptionsFactory.Object); | ||
|
||
Assert.IsTrue(await documentServiceLeaseStoreCosmos.IsInitializedAsync()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task IsInitializedAsync_IfDoesNotExist() | ||
{ | ||
string prefix = "prefix"; | ||
Mock<Container> container = new Mock<Container>(); | ||
container.Setup(c => c.ReadItemStreamAsync( | ||
It.IsAny<string>(), | ||
It.IsAny<PartitionKey>(), | ||
It.IsAny<ItemRequestOptions>(), | ||
It.IsAny<CancellationToken>())).ReturnsAsync(new ResponseMessage(System.Net.HttpStatusCode.NotFound)); | ||
Mock<RequestOptionsFactory> requestOptionsFactory = new Mock<RequestOptionsFactory>(); | ||
DocumentServiceLeaseStoreCosmos documentServiceLeaseStoreCosmos = new DocumentServiceLeaseStoreCosmos( | ||
container.Object, | ||
prefix, | ||
requestOptionsFactory.Object); | ||
|
||
Assert.IsFalse(await documentServiceLeaseStoreCosmos.IsInitializedAsync()); | ||
} | ||
|
||
[TestMethod] | ||
public async Task AcquireInitializationLockAsync_ConcurrentConflict() | ||
{ | ||
string prefix = "prefix"; | ||
Mock<Container> container = new Mock<Container>(); | ||
container.Setup(c => c.CreateItemStreamAsync( | ||
It.IsAny<Stream>(), | ||
It.IsAny<PartitionKey>(), | ||
It.IsAny<ItemRequestOptions>(), | ||
It.IsAny<CancellationToken>())).ReturnsAsync(new ResponseMessage(System.Net.HttpStatusCode.Conflict)); | ||
Mock<RequestOptionsFactory> requestOptionsFactory = new Mock<RequestOptionsFactory>(); | ||
DocumentServiceLeaseStoreCosmos documentServiceLeaseStoreCosmos = new DocumentServiceLeaseStoreCosmos( | ||
container.Object, | ||
prefix, | ||
requestOptionsFactory.Object); | ||
|
||
Assert.IsFalse(await documentServiceLeaseStoreCosmos.AcquireInitializationLockAsync(TimeSpan.FromSeconds(1))); | ||
} | ||
|
||
[TestMethod] | ||
[ExpectedException(typeof(CosmosException))] | ||
public async Task AcquireInitializationLockAsync_OnFailure() | ||
{ | ||
string prefix = "prefix"; | ||
Mock<Container> container = new Mock<Container>(); | ||
container.Setup(c => c.CreateItemStreamAsync( | ||
It.IsAny<Stream>(), | ||
It.IsAny<PartitionKey>(), | ||
It.IsAny<ItemRequestOptions>(), | ||
It.IsAny<CancellationToken>())).ReturnsAsync(new ResponseMessage(System.Net.HttpStatusCode.TooManyRequests)); | ||
Mock<RequestOptionsFactory> requestOptionsFactory = new Mock<RequestOptionsFactory>(); | ||
DocumentServiceLeaseStoreCosmos documentServiceLeaseStoreCosmos = new DocumentServiceLeaseStoreCosmos( | ||
container.Object, | ||
prefix, | ||
requestOptionsFactory.Object); | ||
|
||
await documentServiceLeaseStoreCosmos.AcquireInitializationLockAsync(TimeSpan.FromSeconds(1)); | ||
} | ||
|
||
[TestMethod] | ||
public async Task AcquireInitializationLockAsync_Success() | ||
{ | ||
string prefix = "prefix"; | ||
string etag = Guid.NewGuid().ToString(); | ||
|
||
dynamic lockDocument = new { id = Guid.NewGuid().ToString() }; | ||
|
||
ResponseMessage response = new ResponseMessage(System.Net.HttpStatusCode.OK) | ||
{ | ||
Content = new CosmosJsonDotNetSerializer().ToStream(lockDocument) | ||
}; | ||
|
||
Mock<Container> container = new Mock<Container>(); | ||
container.Setup(c => c.CreateItemStreamAsync( | ||
It.IsAny<Stream>(), | ||
It.IsAny<PartitionKey>(), | ||
It.IsAny<ItemRequestOptions>(), | ||
It.IsAny<CancellationToken>())).ReturnsAsync(response); | ||
|
||
Mock<RequestOptionsFactory> requestOptionsFactory = new Mock<RequestOptionsFactory>(); | ||
DocumentServiceLeaseStoreCosmos documentServiceLeaseStoreCosmos = new DocumentServiceLeaseStoreCosmos( | ||
container.Object, | ||
prefix, | ||
requestOptionsFactory.Object); | ||
|
||
Assert.IsTrue(await documentServiceLeaseStoreCosmos.AcquireInitializationLockAsync(TimeSpan.FromSeconds(1))); | ||
} | ||
} | ||
} |