public async Task TestOperationCanceledExceptionAsync()
        {
            int    seed = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
            Random rand = new Random(seed);
            IDocumentClientRetryPolicy retryPolicy = new MockRetryPolicy(rand);
            ComparableTaskScheduler    scheduler   = new ComparableTaskScheduler(1);

            DocumentProducer <int> producer = new DocumentProducer <int>(
                scheduler,
                (continuation, pageSize) => null,
                new PartitionKeyRange {
                Id = "test", MinInclusive = "", MaxExclusive = "ff"
            },
                p => 0,
                (request, token) =>
            {
                scheduler.Stop();
                throw new Exception();
            },
                () => retryPolicy,
                (produer, size, ru, queryMetrics, token, length) => { },
                Guid.NewGuid()
                )
            {
                PageSize = 1000
            };

            await producer.MoveNextAsync(new CancellationTokenSource().Token);
        }
        public async Task ConcurrentMoveNextTryScheduleTestAsync()
        {
            int    seed     = (int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds;
            Random rand     = new Random(seed);
            int    maxValue = 100;
            int    trials   = 1000;
            int    maxTicks = 100;

            IEnumerable <int>          expectedValues = Enumerable.Range(1, maxValue);
            IDocumentClientRetryPolicy retryPolicy    = new MockRetryPolicy(rand);
            ComparableTaskScheduler    scheduler      = new ComparableTaskScheduler(1);

            for (int trial = 0; trial < trials; ++trial)
            {
                DocumentProducer <int> producer = new DocumentProducer <int>(
                    scheduler,
                    (continuation, pageSize) => DocumentServiceRequest.Create(
                        OperationType.Query,
                        "/dbs/db/colls/coll",
                        ResourceType.Document,
                        new MemoryStream(Encoding.UTF8.GetBytes(continuation)),
                        AuthorizationTokenType.PrimaryMasterKey,
                        new StringKeyValueCollection
                {
                    { HttpConstants.HttpHeaders.Continuation, continuation }
                }),
                    new PartitionKeyRange {
                    Id = "test", MinInclusive = "", MaxExclusive = "ff"
                },
                    p => 0,
                    (request, token) =>
                {
                    if (rand.Next(4) == 0)
                    {
                        throw new Exception();
                    }

                    if (rand.Next(10) == 0)
                    {
                        return(Task.FromResult(new FeedResponse <int>(new int[] { }, 0, request.Headers)));
                    }

                    using (StreamReader reader = new StreamReader(request.Body))
                    {
                        int value = int.Parse(reader.ReadToEnd()) + 1;
                        INameValueCollection headers = new StringKeyValueCollection
                        {
                            { HttpConstants.HttpHeaders.Continuation, value >= maxValue? null : value.ToString(CultureInfo.InvariantCulture) }
                        };
                        return(Task.FromResult(new FeedResponse <int>(new int[] { value }, 1, headers)));
                    }
                },
                    () => retryPolicy,
                    (produer, size, ru, queryMetrics, token, length) => { },
                    Guid.NewGuid(),
                    1000,
                    "0");

                Timer timer = new Timer(
                    (state) => producer.TryScheduleFetch(TimeSpan.FromTicks(rand.Next(maxTicks))),
                    null,
                    TimeSpan.FromTicks(rand.Next(maxTicks)),
                    TimeSpan.FromTicks(rand.Next(maxTicks)));

                List <int> actualValues             = new List <int>();
                CancellationTokenSource tokenSource = new CancellationTokenSource(5000);
                while (await producer.MoveNextAsync(tokenSource.Token))
                {
                    actualValues.Add(producer.Current);
                }

                Assert.AreEqual(
                    string.Join(", ", expectedValues),
                    string.Join(", ", actualValues),
                    string.Format(CultureInfo.InvariantCulture, "seed: {0}", seed));
            }
        }