public void TableServiceContextSaveChangeWithRetriesAPM() { CloudTableClient tableClient = GenerateCloudTableClient(); // Get data context. TableServiceContext context = tableClient.GetTableServiceContext(); // Create the new entity. BaseEntity entity = new BaseEntity("Hello", "world."); using (AutoResetEvent waitHandle = new AutoResetEvent(false)) { // Add the entity. context.AttachTo(this.currentTable.Name, entity); context.UpdateObject(entity); // Save changes to the service. IAsyncResult result = context.BeginSaveChangesWithRetries(ar => waitHandle.Set(), null); waitHandle.WaitOne(); context.EndSaveChangesWithRetries(result); BaseEntity entity2 = new BaseEntity("Insert", "foo."); context.AttachTo(this.currentTable.Name, entity2); context.UpdateObject(entity2); // Test again with different parameters result = context.BeginSaveChangesWithRetries(SaveChangesOptions.None, ar => waitHandle.Set(), null); waitHandle.WaitOne(); context.EndSaveChangesWithRetries(result); // Retrieve Entities & Verify BaseEntity retrievedEntity = (from ent in context.CreateQuery <BaseEntity>(currentTable.Name) where ent.PartitionKey == entity2.PartitionKey && ent.RowKey == entity2.RowKey select ent).AsTableServiceQuery(context).Execute().FirstOrDefault(); Assert.IsNotNull(retrievedEntity); retrievedEntity = (from ent in context.CreateQuery <BaseEntity>(currentTable.Name) where ent.PartitionKey == entity.PartitionKey && ent.RowKey == entity.RowKey select ent).AsTableServiceQuery(context).Execute().FirstOrDefault(); Assert.IsNotNull(retrievedEntity); } // Test Dispose context.Dispose(); }
public void TableServiceContextOperationContextStartEndTimeAPM() { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext ctx = tableClient.GetTableServiceContext(); DateTime start = DateTime.Now; for (int m = 0; m < 10; m++) { BaseEntity ent = new BaseEntity("testpartition", m.ToString()); ent.Randomize(); ent.A = ent.RowKey; ctx.AddObject(currentTable.Name, ent); } OperationContext opContext = new OperationContext(); using (ManualResetEvent evt = new ManualResetEvent(false)) { ctx.BeginSaveChangesWithRetries(SaveChangesOptions.None, null, opContext, (res) => { ctx.EndSaveChangesWithRetries(res); evt.Set(); }, null); evt.WaitOne(); } Assert.IsNotNull(opContext.StartTime, "StartTime not set"); Assert.IsTrue(opContext.StartTime >= start, "StartTime not set correctly"); Assert.IsNotNull(opContext.EndTime, "EndTime not set"); Assert.IsTrue(opContext.EndTime <= DateTime.Now, "EndTime not set correctly"); }
public void TableServiceContextRequestResultsAPM() { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext ctx = tableClient.GetTableServiceContext(); for (int m = 0; m < 10; m++) { BaseEntity ent = new BaseEntity("testpartition", m.ToString()); ent.Randomize(); ent.A = ent.RowKey; ctx.AddObject(currentTable.Name, ent); } OperationContext opContext = new OperationContext(); using (ManualResetEvent evt = new ManualResetEvent(false)) { ctx.BeginSaveChangesWithRetries(SaveChangesOptions.None, null, opContext, (res) => { ctx.EndSaveChangesWithRetries(res); evt.Set(); }, null); evt.WaitOne(); } TestHelper.AssertNAttempts(opContext, 10); }
public void TableServiceContextOperationContextEventsAPM() { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext ctx = tableClient.GetTableServiceContext(); for (int m = 0; m < 10; m++) { BaseEntity ent = new BaseEntity("testpartition", m.ToString()); ent.Randomize(); ent.A = ent.RowKey; ctx.AddObject(currentTable.Name, ent); } List <string> sends = new List <string>(); List <string> receives = new List <string>(); OperationContext opContext = new OperationContext(); opContext.SendingRequest += (sender, args) => sends.Add(args.RequestInformation.StartTime.Ticks.ToString()); using (ManualResetEvent evt = new ManualResetEvent(false)) { ctx.BeginSaveChangesWithRetries(SaveChangesOptions.None, null, opContext, (res) => { ctx.EndSaveChangesWithRetries(res); evt.Set(); }, null); evt.WaitOne(); } TestHelper.AssertNAttempts(opContext, 10); Assert.AreEqual(sends.Count(), 10); }
public void TableServiceContextTimeoutDuringSaveChangesNonBatchAPM() { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext ctx = tableClient.GetTableServiceContext(); for (int m = 0; m < 100; m++) { BaseEntity ent = new BaseEntity("testpartition", m.ToString()); ent.Randomize(); ent.A = ent.RowKey; ctx.AddObject(currentTable.Name, ent); } OperationContext opContext = new OperationContext(); TableRequestOptions requestOptions = new TableRequestOptions() { MaximumExecutionTime = TimeSpan.FromSeconds(5) }; using (HttpMangler proxy = new HttpMangler(false, new[] { DelayBehaviors.DelayAllRequestsIf(2000, XStoreSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName).SkipNSessions(10)) })) { try { using (ManualResetEvent evt = new ManualResetEvent(false)) { IAsyncResult result = ctx.BeginSaveChangesWithRetries(SaveChangesOptions.None, requestOptions, opContext, (res) => { result = res; evt.Set(); }, null); evt.WaitOne(); ctx.EndSaveChangesWithRetries(result); } ctx.SaveChangesWithRetries(SaveChangesOptions.None, requestOptions, opContext); } catch (StorageException ex) { Assert.AreEqual(ex.RequestInformation.HttpStatusCode, (int)HttpStatusCode.RequestTimeout); Assert.AreEqual("The client could not finish the operation within specified timeout.", ex.Message); Assert.IsTrue(ex.InnerException is TimeoutException); } } }
public void BatchInsertAPM() { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext ctx = tableClient.GetTableServiceContext(); // Insert Entities SortedDictionary <string, ComplexEntity> entities = new SortedDictionary <string, ComplexEntity>(); for (int i = 0; i < 100; i++) { ComplexEntity insertEntity = new ComplexEntity("insert test", "foo" + i); entities.Add(insertEntity.RowKey, insertEntity); ctx.AddObject(currentTable.Name, insertEntity); } DataServiceResponse response; using (ManualResetEvent evt = new ManualResetEvent(false)) { IAsyncResult asyncRes = null; ctx.BeginSaveChangesWithRetries(SaveChangesOptions.Batch, (res) => { asyncRes = res; evt.Set(); }, null); evt.WaitOne(); response = ctx.EndSaveChangesWithRetries(asyncRes); } Assert.AreEqual((int)HttpStatusCode.Accepted, response.BatchStatusCode); // Retrieve Entities List <ComplexEntity> retrievedEntities = (from ent in ctx.CreateQuery <ComplexEntity>(currentTable.Name) where ent.PartitionKey == entities.First().Value.PartitionKey select ent).AsTableServiceQuery(ctx).Execute().ToList(); Assert.AreEqual(entities.Count, retrievedEntities.Count); foreach (ComplexEntity retrievedEntity in retrievedEntities) { ComplexEntity.AssertEquality(entities[retrievedEntity.RowKey], retrievedEntity); entities.Remove(retrievedEntity.RowKey); } Assert.AreEqual(0, entities.Count); }
public void TableTestSaveChangesCancellationNonBatch() { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext ctx = tableClient.GetTableServiceContext(); for (int m = 0; m < 100; m++) { // Insert Entity ComplexEntity insertEntity = new ComplexEntity("insert test", m.ToString()); ctx.AddObject(currentTable.Name, insertEntity); } TestHelper.ExecuteAPMMethodWithCancellation(4000, new[] { DelayBehaviors.DelayAllRequestsIf(4000 * 3, XStoreSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName)) }, (options, opContext, callback, state) => ctx.BeginSaveChangesWithRetries(SaveChangesOptions.None, (TableRequestOptions)options, opContext, callback, state), (res) => ctx.EndSaveChangesWithRetries(res)); }
public void TableServiceContextSaveChangesRetryAPM() { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext ctx = tableClient.GetTableServiceContext(); for (int m = 0; m < 100; m++) { // Insert Entity ComplexEntity insertEntity = new ComplexEntity("insert test", m.ToString()); insertEntity.Binary = new byte[20 * 1024]; ctx.AddObject(currentTable.Name, insertEntity); } TestHelper.ExecuteAPMMethodWithRetry(3, new[] { //Insert upstream network delay to prevent upload to server @ 1000ms / kb PerformanceBehaviors.InsertUpstreamNetworkDelay(10000, AzureStorageSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName), new BehaviorOptions(2)), // After 500 ms return throttle message DelayedActionBehaviors.ExecuteAfter(Actions.ThrottleTableRequest, 100, AzureStorageSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName), new BehaviorOptions(2)) }, (options, opContext, callback, state) => ctx.BeginSaveChangesWithRetries(SaveChangesOptions.Batch, (TableRequestOptions)options, opContext, callback, state), (res) => ctx.EndSaveChangesWithRetries(res)); }
public void SingleEntityInsertOrReplaceAPM() { CloudTableClient tableClient = GenerateCloudTableClient(); TableServiceContext ctx = tableClient.GetTableServiceContext(); TableServiceContext queryContext = tableClient.GetTableServiceContext(); queryContext.MergeOption = MergeOption.NoTracking; // Insert Entity BaseEntity baseEntity = new BaseEntity("insert test", "foo"); // Insert Or Merge with no pre-existing entity MergeEntity insertOrReplaceEntity = new MergeEntity(baseEntity.PartitionKey, baseEntity.RowKey); insertOrReplaceEntity.Randomize(); ctx.AttachTo(currentTable.Name, insertOrReplaceEntity, null); ctx.UpdateObject(insertOrReplaceEntity); using (ManualResetEvent evt = new ManualResetEvent(false)) { IAsyncResult asyncRes = null; ctx.BeginSaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate, (res) => { asyncRes = res; evt.Set(); }, null); evt.WaitOne(); ctx.EndSaveChangesWithRetries(asyncRes); } ctx.Detach(insertOrReplaceEntity); // Retrieve Entity & Verify Contents UnionEnitity retrievedEntity = (from ent in queryContext.CreateQuery <UnionEnitity>(currentTable.Name) where ent.PartitionKey == baseEntity.PartitionKey && ent.RowKey == baseEntity.RowKey select ent).AsTableServiceQuery(queryContext).Execute().FirstOrDefault(); Assert.IsNotNull(retrievedEntity); Assert.AreEqual(null, retrievedEntity.A); Assert.AreEqual(null, retrievedEntity.B); Assert.AreEqual(null, retrievedEntity.C); Assert.AreEqual(insertOrReplaceEntity.D, retrievedEntity.D); Assert.AreEqual(insertOrReplaceEntity.E, retrievedEntity.E); Assert.AreEqual(insertOrReplaceEntity.F, retrievedEntity.F); BaseEntity replacedEntity = new BaseEntity("insert test", "foo"); replacedEntity.Randomize(); ctx.AttachTo(currentTable.Name, replacedEntity, null); ctx.UpdateObject(replacedEntity); using (ManualResetEvent evt = new ManualResetEvent(false)) { IAsyncResult asyncRes = null; ctx.BeginSaveChangesWithRetries(SaveChangesOptions.ReplaceOnUpdate, (res) => { asyncRes = res; evt.Set(); }, null); evt.WaitOne(); ctx.EndSaveChangesWithRetries(asyncRes); } // Retrieve Entity & Verify retrievedEntity = (from ent in queryContext.CreateQuery <UnionEnitity>(currentTable.Name) where ent.PartitionKey == baseEntity.PartitionKey && ent.RowKey == baseEntity.RowKey select ent).AsTableServiceQuery(queryContext).Execute().FirstOrDefault(); Assert.IsNotNull(retrievedEntity); Assert.AreEqual(replacedEntity.A, retrievedEntity.A); Assert.AreEqual(replacedEntity.B, retrievedEntity.B); Assert.AreEqual(replacedEntity.C, retrievedEntity.C); Assert.AreEqual(null, retrievedEntity.D); Assert.AreEqual(null, retrievedEntity.E); Assert.AreEqual(null, retrievedEntity.F); }