private UpdateCascadeOperation WaitOperationToComplete(Category category)
 {
     while (true)
     {
         using (var session = this.Store.OpenSession())
         {
             var operation = session.Load <UpdateCascadeOperation>(UpdateCascadeOperation.GetId(category.Id, category.Etag.Value));
             if (operation != null && operation.IsCompleted)
             {
                 return(operation);
             }
             Thread.Sleep(10);
         }
     }
 }
        public void UpdateCascadeShouldWork()
        {
            using (var session = this.Store.OpenSession())
            {
                var startedTime = DateTime.UtcNow;
                var category    = session.Load <Category>("Categories/1");
                var newName     = category.Name + " new Name";
                category.Name = newName;
                session.SaveChanges();
                category.Etag = session.Advanced.GetMetadataFor(category).Value <Guid>("@etag");
                UpdateCascadeOperation operation = null;

                var waitTaks = Task.Factory.StartNew(() =>
                {
                    operation = WaitOperationToComplete(category);
                });

                if (!waitTaks.Wait(TimeSpan.FromSeconds(20)))
                {
                    throw new TimeoutException("The cascade operation did not completed within the allotted time");
                }



                Assert.Equal(UpdateCascadeOperation.GetId(category.Id, category.Etag.Value), operation.Id);
                Assert.Equal(true, operation.IsCompleted);
                Assert.Equal(1, operation.CollectionOperations.Count);
                Assert.Equal(category.Etag, operation.ReferencedDocEtag);
                Assert.Equal(category.Id, operation.ReferencedDocId);
                Assert.Equal(UpdateCascadeOperationStatus.CompletedSuccessfully, operation.Status);
                Assert.Equal(UpdateCascadeSetting.GetId("Categories"), operation.UpdateCascadeSettingId);
                Assert.Equal(productsPerCategory, operation.UpdatedDocumentCount);
                Assert.InRange(operation.StartedDate.Value, startedTime, DateTime.UtcNow);
                Assert.InRange(operation.CompletedDate.Value, startedTime, DateTime.UtcNow);

                for (int productNumber = 1; productNumber <= productsPerCategory; productNumber++)
                {
                    var productId = category.Id + "/Products/" + productNumber.ToString();
                    var product   = session.Load <Product>(productId);
                    Assert.Equal(category.Name, product.Category.Name);
                    Assert.Equal(category.Etag, product.Category.Etag);
                }
            }
        }