Created
February 16, 2017 01:41
-
-
Save dcomartin/b6a49f9b5c400a4f731862e53360dbf7 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Linq; | |
using System.Net; | |
using System.Threading.Tasks; | |
using Microsoft.Azure.Documents; | |
using Microsoft.Azure.Documents.Client; | |
using Shouldly; | |
using Xunit; | |
namespace Demo | |
{ | |
public class OptimtimisticConcurrencyTests | |
{ | |
private readonly DocumentClient _client; | |
private const string EndpointUrl = "https://localhost:8081"; | |
private const string AuthorizationKey = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="; | |
private const string DatabaseId = "ConcurrencyDemo"; | |
private const string CollectionId = "Customers"; | |
public OptimtimisticConcurrencyTests() | |
{ | |
_client = new DocumentClient(new Uri(EndpointUrl), AuthorizationKey); | |
} | |
[Fact] | |
public async Task Should_Throw_With_PreconditionFailed() | |
{ | |
// Setup our Database and add a new Customer | |
var dbSetup = new DatabaseSetup(_client); | |
await dbSetup.Init(DatabaseId, CollectionId); | |
var addCustomer = new Customer(Guid.NewGuid().ToString(), "Demo"); | |
await dbSetup.AddCustomer(addCustomer); | |
// Fetch out the Document (Customer) | |
var document = (from f in dbSetup.Client.CreateDocumentQuery(dbSetup.Collection.SelfLink) | |
where f.Id == addCustomer.Id | |
select f).AsEnumerable().FirstOrDefault(); | |
// Cast the Document to our Customer & make a data change | |
var editCustomer = (Customer) (dynamic) document; | |
editCustomer.Name = "Changed"; | |
// Using Access Conditions gives us the ability to use the ETag from our fetched document for optimistic concurrency. | |
var ac = new AccessCondition {Condition = document.ETag, Type = AccessConditionType.IfMatch}; | |
// Replace our document, which will succeed with the correct ETag | |
await dbSetup.Client.ReplaceDocumentAsync(document.SelfLink, editCustomer, | |
new RequestOptions {AccessCondition = ac}); | |
// Replace again, which will fail since our (same) ETag is now invalid | |
var ex = await dbSetup.Client.ReplaceDocumentAsync(document.SelfLink, editCustomer, | |
new RequestOptions {AccessCondition = ac}).ShouldThrowAsync<DocumentClientException>(); | |
ex.StatusCode.ShouldBe(HttpStatusCode.PreconditionFailed); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment