public async Task WhenDelegateIsTyped_Manual()
        {
            bool executed = false;

            Task changesHandler(ChangeFeedProcessorContext context, IReadOnlyCollection <dynamic> docs, Func <Task> checkpointAsync, CancellationToken token)
            {
                Assert.AreEqual(1, docs.Count);
                Assert.AreEqual("Test", docs.First().id.ToString());
                executed = true;
                return(Task.CompletedTask);
            }

            ChangeFeedObserverFactoryCore <dynamic> changeFeedObserverFactoryCore = new ChangeFeedObserverFactoryCore <dynamic>(changesHandler, this.cosmosSerializerCore);

            ChangeFeedObserver changeFeedObserver = changeFeedObserverFactoryCore.CreateObserver();

            Assert.IsNotNull(changeFeedObserver);

            ResponseMessage responseMessage       = this.BuildResponseMessage();
            ChangeFeedObserverContextCore context = new ChangeFeedObserverContextCore(this.leaseToken, responseMessage, Mock.Of <PartitionCheckpointer>());

            await changeFeedObserver.ProcessChangesAsync(context, responseMessage.Content, CancellationToken.None);

            Assert.IsTrue(executed);
        }
        public async Task WhenDelegateIsStream_Automatic()
        {
            ResponseMessage responseMessage = this.BuildResponseMessage();
            bool            executed        = false;

            Task changesHandler(ChangeFeedProcessorContext context, Stream stream, CancellationToken token)
            {
                Assert.ReferenceEquals(responseMessage.Content, stream);
                executed = true;
                return(Task.CompletedTask);
            }

            ChangeFeedObserverFactoryCore changeFeedObserverFactoryCore = new ChangeFeedObserverFactoryCore(changesHandler);

            ChangeFeedObserver changeFeedObserver = changeFeedObserverFactoryCore.CreateObserver();

            Assert.IsNotNull(changeFeedObserver);


            ChangeFeedObserverContextCore context = new ChangeFeedObserverContextCore(this.leaseToken, responseMessage, Mock.Of <PartitionCheckpointer>());

            await changeFeedObserver.ProcessChangesAsync(context, responseMessage.Content, CancellationToken.None);

            Assert.IsTrue(executed);
        }
        public async Task UsesCustomSerializer()
        {
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(1000);

            ChangesHandler <MyDocument> handler = (changes, cancelationToken) =>
            {
                IReadOnlyList <MyDocument> list = changes as IReadOnlyList <MyDocument>;
                Assert.IsNotNull(list);
                Assert.AreEqual("test", list[0].id);
                return(Task.CompletedTask);
            };
            Mock <PartitionCheckpointer> mockCheckpointer = new Mock <PartitionCheckpointer>();
            Mock <FeedIterator>          mockIterator     = new Mock <FeedIterator>();

            mockIterator.Setup(i => i.ReadNextAsync(It.IsAny <CancellationToken>())).ReturnsAsync(GetResponse(HttpStatusCode.OK, true));
            mockIterator.SetupSequence(i => i.HasMoreResults).Returns(true).Returns(false);

            CustomSerializer serializer = new CustomSerializer();
            ChangeFeedObserverFactoryCore <MyDocument> factory = new ChangeFeedObserverFactoryCore <MyDocument>(handler, new CosmosSerializerCore(serializer));
            FeedProcessorCore processor = new FeedProcessorCore(factory.CreateObserver(), mockIterator.Object, FeedProcessorCoreTests.DefaultSettings, mockCheckpointer.Object);

            try
            {
                await processor.RunAsync(cancellationTokenSource.Token);
            }
            catch (TaskCanceledException)
            {
                // Expected
            }

            Assert.AreEqual(1, serializer.FromStreamCalled, "Should have called FromStream on the custom serializer");
        }
        public async Task ReadAsyncExpectedTimeoutTest()
        {
            ChangesHandler <MyDocument> handler = (changes, cancelationToken) =>
            {
                IReadOnlyList <MyDocument> list = changes as IReadOnlyList <MyDocument>;
                Assert.IsNotNull(list);
                Assert.AreEqual("test", list[0].id);
                return(Task.CompletedTask);
            };
            Mock <PartitionCheckpointer> mockCheckpointer = new Mock <PartitionCheckpointer>();
            Mock <FeedIterator>          mockIterator     = new Mock <FeedIterator>();

            mockIterator.Setup(i => i.ReadNextAsync(It.IsAny <CancellationToken>()))
            .Returns(async() =>
            {
                await Task.Delay(200);
                return(GetResponse(HttpStatusCode.OK, true));
            });
            mockIterator.SetupSequence(i => i.HasMoreResults).Returns(true).Returns(false);

            CustomSerializer serializer = new CustomSerializer();
            ChangeFeedObserverFactoryCore <MyDocument> factory = new ChangeFeedObserverFactoryCore <MyDocument>(handler, new CosmosSerializerCore(serializer));
            ProcessorOptions options = new ProcessorOptions()
            {
                RequestTimeout = TimeSpan.FromMilliseconds(100)
            };
            FeedProcessorCore processor = new FeedProcessorCore(factory.CreateObserver(), mockIterator.Object, options, mockCheckpointer.Object);

            try
            {
                Task runTask = processor.RunAsync(default);
        public async Task ThrowsOnFailedCustomSerializer()
        {
            CancellationTokenSource cancellationTokenSource = new CancellationTokenSource(1000);

            Mock <PartitionCheckpointer> mockCheckpointer = new Mock <PartitionCheckpointer>();
            Mock <FeedIterator>          mockIterator     = new Mock <FeedIterator>();

            mockIterator.Setup(i => i.ReadNextAsync(It.IsAny <CancellationToken>())).ReturnsAsync(GetResponse(HttpStatusCode.OK, true));
            mockIterator.SetupSequence(i => i.HasMoreResults).Returns(true).Returns(false);

            CustomSerializerFails    serializer             = new CustomSerializerFails();
            ChangesHandler <dynamic> handler                = (changes, cancelationToken) => Task.CompletedTask;
            ChangeFeedObserverFactoryCore <dynamic> factory = new ChangeFeedObserverFactoryCore <dynamic>(handler, new CosmosSerializerCore(serializer));
            FeedProcessorCore processor = new FeedProcessorCore(factory.CreateObserver(), mockIterator.Object, FeedProcessorCoreTests.DefaultSettings, mockCheckpointer.Object);

            ChangeFeedProcessorUserException caughtException = await Assert.ThrowsExceptionAsync <ChangeFeedProcessorUserException>(() => processor.RunAsync(cancellationTokenSource.Token));

            Assert.IsInstanceOfType(caughtException.InnerException, typeof(CustomException));
        }