private void DoTableServiceQueryProjection(TablePayloadFormat format)
        {
            CloudTableClient    tableClient = GenerateCloudTableClient();
            TableServiceContext ctx         = tableClient.GetTableServiceContext();

            SetPayloadFormatOnDataServiceContext(ctx, format, tableClient);

            // Retrieve Entities
            TableServiceQuery <UnionEnitity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name)
                                                      select new UnionEnitity
            {
                A = ent.A,
                RowKey = ent.RowKey
            }).AsTableServiceQuery(ctx);

            List <UnionEnitity> totalResults = query.Execute().ToList();

            Assert.AreEqual(totalResults.Count, totalTestEntities);

            foreach (UnionEnitity ent in totalResults)
            {
                Assert.IsNotNull(ent.A);
                Assert.AreEqual(ent.A, ent.RowKey);
                Assert.IsNull(ent.B);
                Assert.IsNull(ent.C);
                Assert.IsNull(ent.D);
                Assert.IsNull(ent.E);
                Assert.IsNull(ent.F);
            }
        }
Exemple #2
0
        public void TableServiceQueryWithRetryAPM()
        {
            CloudTableClient    tableClient = GenerateCloudTableClient();
            TableServiceContext ctx         = tableClient.GetTableServiceContext();

            for (int m = 0; m < 1000; m++)
            {
                // Insert Entity
                ComplexEntity insertEntity = new ComplexEntity("insert test", m.ToString());
                ctx.AddObject(currentTable.Name, insertEntity);

                if ((m + 1) % 100 == 0)
                {
                    ctx.SaveChangesWithRetries(SaveChangesOptions.Batch);
                }
            }

            TableServiceQuery <ComplexEntity> query = (from ent in ctx.CreateQuery <ComplexEntity>(currentTable.Name)
                                                       select ent).AsTableServiceQuery(ctx);

            TestHelper.ExecuteAPMMethodWithRetry(
                2, // 1 failure, one success
                new[] {
                //Insert upstream network delay to prevent upload to server @ 1000ms / kb
                PerformanceBehaviors.InsertDownstreamNetworkDelay(10000,
                                                                  AzureStorageSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName).Alternating(true)),
                // After 100 ms return throttle message
                DelayedActionBehaviors.ExecuteAfter(Actions.ThrottleTableRequest,
                                                    100,
                                                    AzureStorageSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName).Alternating(true))
            },
                (options, opContext, callback, state) => query.BeginExecuteSegmented(null, (TableRequestOptions)options, opContext, callback, state),
                query.EndExecuteSegmented);
        }
        public void TableServiceQueryExecuteSegmentedBasicTask()
        {
            CloudTableClient    tableClient = GenerateCloudTableClient();
            TableServiceContext ctx         = tableClient.GetTableServiceContext();

            // Retrieve Entities
            TableServiceQuery <BaseEntity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name)
                                                    select ent).AsTableServiceQuery(ctx);

            List <BaseEntity> totalResults         = new List <BaseEntity>();
            TableQuerySegment <BaseEntity> segment = null;

            do
            {
                segment = query.ExecuteSegmentedAsync(segment != null ? segment.ContinuationToken : null).Result;
                if (totalResults.Count == 0)
                {
                    // Assert first segment has continuation token

                    Assert.IsNotNull(segment.ContinuationToken);
                }

                totalResults.AddRange(segment);
            }while (segment.ContinuationToken != null);

            Assert.AreEqual(totalResults.Count, totalTestEntities);
        }
        private void DoTableServiceQueryExecuteSegmentedWithTake(TablePayloadFormat format)
        {
            CloudTableClient    tableClient = GenerateCloudTableClient();
            TableServiceContext ctx         = tableClient.GetTableServiceContext();

            SetPayloadFormatOnDataServiceContext(ctx, format, tableClient);

            int takeCount = 150;

            // Retrieve Entities
            TableServiceQuery <BaseEntity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name)
                                                    select ent).Take(takeCount).AsTableServiceQuery(ctx);

            List <BaseEntity> totalResults         = new List <BaseEntity>();
            TableQuerySegment <BaseEntity> segment = null;

            int segmentCount = 0;

            do
            {
                segment = query.ExecuteSegmented(segment != null ? segment.ContinuationToken : null);
                if (totalResults.Count == 0)
                {
                    // Assert first segment has continuation token
                    Assert.IsNotNull(segment.ContinuationToken);
                }

                totalResults.AddRange(segment);
                segmentCount++;
            }while (segment.ContinuationToken != null);

            Assert.IsTrue(segmentCount >= totalTestEntities / takeCount);
            Assert.AreEqual(totalResults.Count, totalTestEntities);
        }
        public void TableServiceQueryExecuteBasic()
        {
            CloudTableClient    tableClient = GenerateCloudTableClient();
            TableServiceContext ctx         = tableClient.GetTableServiceContext();

            // Retrieve Entities
            TableServiceQuery <BaseEntity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name)
                                                    select ent).AsTableServiceQuery(ctx);

            List <BaseEntity> totalResults = query.Execute().ToList();

            Assert.AreEqual(totalResults.Count, totalTestEntities);
        }
        private void DoTableServiceQueryExecuteBasic(TablePayloadFormat format)
        {
            CloudTableClient    tableClient = GenerateCloudTableClient();
            TableServiceContext ctx         = tableClient.GetTableServiceContext();

            SetPayloadFormatOnDataServiceContext(ctx, format, tableClient);

            // Retrieve Entities
            TableServiceQuery <BaseEntity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name)
                                                    select ent).AsTableServiceQuery(ctx);

            List <BaseEntity> totalResults = query.Execute().ToList();

            Assert.AreEqual(totalResults.Count, totalTestEntities);
        }
        public void TableServiceQueryExecuteSecondary()
        {
            CloudTableClient tableClient = GenerateCloudTableClient();

            tableClient.LocationMode = RetryPolicies.LocationMode.SecondaryOnly;
            TableServiceContext ctx = tableClient.GetTableServiceContext();

            // Retrieve Entities
            TableServiceQuery <BaseEntity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name)
                                                    select ent).AsTableServiceQuery(ctx);

            TestHelper.ExpectedException <InvalidOperationException>(
                () => query.Execute().ToList(),
                "Even queries cannot be sent to secondary location when using TableServiceContext");
        }
        private void DoTableServiceQueryProjectionWithUpdate(TablePayloadFormat format)
        {
            CloudTableClient    tableClient = GenerateCloudTableClient();
            TableServiceContext ctx         = tableClient.GetTableServiceContext();

            SetPayloadFormatOnDataServiceContext(ctx, format, tableClient);

            // Retrieve Entities
            TableServiceQuery <UnionEnitity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name)
                                                      select new UnionEnitity
            {
                A = ent.A,
                B = ent.B
            }).Take(20).AsTableServiceQuery(ctx);



            List <UnionEnitity> totalResults = query.Execute().ToList();

            foreach (UnionEnitity ent in totalResults)
            {
                Assert.IsNotNull(ent.A);
                Assert.IsNotNull(ent.B);
                ent.PartitionKey = string.Empty;
                ent.RowKey       = string.Empty;
                ent.B           += "_updated";
                ctx.UpdateObject(ent);
            }

            ctx.SaveChangesWithRetries(SaveChangesOptions.Batch);

            TableServiceContext queryContext = tableClient.GetTableServiceContext();

            // Verify update
            TableServiceQuery <BaseEntity> secondQuery = (from ent in queryContext.CreateQuery <BaseEntity>(currentTable.Name)
                                                          select ent).Take(20).AsTableServiceQuery(queryContext);

            foreach (BaseEntity ent in secondQuery.Execute())
            {
                Assert.IsTrue(ent.B.EndsWith("_updated"));
            }
        }
        public void TableServiceQueryExecuteSegmentedBasicAPM()
        {
            CloudTableClient    tableClient = GenerateCloudTableClient();
            TableServiceContext ctx         = tableClient.GetTableServiceContext();

            // Retrieve Entities
            TableServiceQuery <BaseEntity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name)
                                                    select ent).AsTableServiceQuery(ctx);

            List <BaseEntity> totalResults         = new List <BaseEntity>();
            TableQuerySegment <BaseEntity> segment = null;

            do
            {
                using (ManualResetEvent evt = new ManualResetEvent(false))
                {
                    IAsyncResult result = null;
                    query.BeginExecuteSegmented(segment != null ? segment.ContinuationToken : null,
                                                (res) =>
                    {
                        result = res;
                        evt.Set();
                    }, null);
                    evt.WaitOne();

                    segment = query.EndExecuteSegmented(result);
                }

                if (totalResults.Count == 0)
                {
                    // Assert first segment has continuation token

                    Assert.IsNotNull(segment.ContinuationToken);
                }

                totalResults.AddRange(segment);
            }while (segment.ContinuationToken != null);

            Assert.AreEqual(totalResults.Count, totalTestEntities);
        }
Exemple #10
0
        public void TableTestSegmentedQueryCancellation()
        {
            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);
            }
            ctx.SaveChangesWithRetries();

            TableServiceQuery <BaseEntity> query = (from ent in ctx.CreateQuery <BaseEntity>(currentTable.Name)
                                                    select ent).AsTableServiceQuery(ctx);


            TestHelper.ExecuteAPMMethodWithCancellation(4000,
                                                        new[] { DelayBehaviors.DelayAllRequestsIf(4000 * 3, XStoreSelectors.TableTraffic().IfHostNameContains(tableClient.Credentials.AccountName)) },
                                                        (options, opContext, callback, state) => query.BeginExecuteSegmented(null, (TableRequestOptions)options, opContext, callback, state),
                                                        (res) => query.EndExecuteSegmented(res));
        }
Exemple #11
0
        public void TableServiceContextConcurrencyAllowsOnlySingleOperationAtOnce()
        {
            CloudTableClient    tableClient  = GenerateCloudTableClient();
            TableServiceContext tableContext = tableClient.GetTableServiceContext();

            // insert entities to query against
            for (int i = 0; i < 5; i++)
            {
                for (int m = 0; m < 100; m++)
                {
                    BaseEntity ent = new BaseEntity("testpartition" + i, m.ToString());
                    ent.Randomize();
                    ent.A = ent.RowKey;
                    tableContext.AddObject(currentTable.Name, ent);
                }

                tableContext.SaveChangesWithRetries(SaveChangesOptions.Batch);
            }

            List <OperationContext> opContexts = new List <OperationContext>();
            object   lockerObj = new object();
            DateTime start     = DateTime.Now;

            int threadsRunning = 0;

            Exception lastEx = null;

            // Start 10 simultaneous threads to query entities associated with same context.
            for (int j = 0; j < 10; j++)
            {
                opContexts.Add(new OperationContext());
                Thread newThread = new Thread((arg) =>
                {
                    Interlocked.Increment(ref threadsRunning);
                    try
                    {
                        lock (lockerObj)
                        {
                            Monitor.Wait(lockerObj);
                        }

                        TableServiceQuery <BaseEntity> query = (from ent in tableContext.CreateQuery <BaseEntity>(currentTable.Name)
                                                                select ent).AsTableServiceQuery(tableContext);

                        Debug.WriteLine(String.Format("Thread {0} start operation @ {1}", Thread.CurrentThread.ManagedThreadId, (DateTime.Now - start).TotalMilliseconds));

                        try
                        {
                            query.Execute(null, arg as OperationContext).ToList();
                        }
                        catch (Exception)
                        {
                            // no op, expected to have some exceptions
                        }

                        Debug.WriteLine(String.Format("Thread {0} end operation @ {1}", Thread.CurrentThread.ManagedThreadId, (DateTime.Now - start).TotalMilliseconds));
                    }
                    catch (Exception ex)
                    {
                        lastEx = ex;
                    }
                    finally
                    {
                        Interlocked.Decrement(ref threadsRunning);
                    }
                });

                newThread.Start(opContexts[j]);
            }

            // Wait for all threads to start
            while (Interlocked.CompareExchange(ref threadsRunning, 10, 10) < 10)
            {
                Thread.Sleep(200);
            }

            // pulse all threads
            lock (lockerObj)
            {
                Monitor.PulseAll(lockerObj);
            }

            // Wait for all threads to complete
            while (Interlocked.CompareExchange(ref threadsRunning, -1, 0) > -1)
            {
                Thread.Sleep(200);
            }

            if (lastEx != null)
            {
                throw lastEx;
            }

            foreach (OperationContext opContext in opContexts)
            {
                if (opContext.LastResult == null || opContext.LastResult.StartTime == null || opContext.LastResult.EndTime == null)
                {
                    continue;
                }

                TestHelper.AssertNAttempts(opContext, 1);

                RequestResult currRes = opContext.LastResult;

                // Make sure this results start time does not occur in between any other results start & end time
                var overlappingResults = (from ctx in opContexts
                                          where ctx.LastResult != null && ctx.LastResult != currRes &&
                                          ctx.LastResult.StartTime != null && ctx.LastResult.EndTime != null &&
                                          ctx.LastResult.StartTime.Ticks <currRes.StartTime.Ticks &&
                                                                          ctx.LastResult.EndTime.Ticks> currRes.StartTime.Ticks
                                          select ctx.LastResult);

                Assert.AreEqual(overlappingResults.Count(), 0, "Detected overlapping query");
            }
        }