Beispiel #1
0
        public void ValidateSerialization_NullFields()
        {
            ChangeFeedProcessorUserException originalException = new ChangeFeedProcessorUserException(null, null);
            string json = JsonConvert.SerializeObject(originalException);
            ChangeFeedProcessorUserException deserializedException = JsonConvert.DeserializeObject <ChangeFeedProcessorUserException>(json);

            Assert.AreEqual(originalException.Message, deserializedException.Message);
            Assert.IsNull(deserializedException.InnerException);
        }
Beispiel #2
0
        public void ValidateConstructor()
        {
            ResponseMessage responseMessage = new ResponseMessage();
            ChangeFeedObserverContextCore  observerContext            = new ChangeFeedObserverContextCore(Guid.NewGuid().ToString(), feedResponse: responseMessage, Mock.Of <PartitionCheckpointer>());
            ChangeFeedProcessorContextCore changeFeedProcessorContext = new ChangeFeedProcessorContextCore(observerContext);
            Exception exception = new Exception("randomMessage");
            ChangeFeedProcessorUserException ex = new ChangeFeedProcessorUserException(exception, changeFeedProcessorContext);

            Assert.AreEqual(exception.Message, ex.InnerException.Message);
            Assert.AreEqual(exception, ex.InnerException);
            Assert.ReferenceEquals(changeFeedProcessorContext, ex.ChangeFeedProcessorContext);
        }
Beispiel #3
0
        public void ValidateSerialization_AllFields()
        {
            ResponseMessage responseMessage = new ResponseMessage();
            ChangeFeedObserverContextCore  observerContext            = new ChangeFeedObserverContextCore(Guid.NewGuid().ToString(), feedResponse: responseMessage, Mock.Of <PartitionCheckpointer>());
            ChangeFeedProcessorContextCore changeFeedProcessorContext = new ChangeFeedProcessorContextCore(observerContext);
            Exception exception = new Exception("randomMessage");
            ChangeFeedProcessorUserException originalException = new ChangeFeedProcessorUserException(exception, changeFeedProcessorContext);

            string json = JsonConvert.SerializeObject(originalException);
            ChangeFeedProcessorUserException deserializedException = JsonConvert.DeserializeObject <ChangeFeedProcessorUserException>(json);

            Assert.AreEqual(originalException.Message, deserializedException.Message);
            Assert.AreEqual(originalException.InnerException.Message, deserializedException.InnerException.Message);
        }
        public void ValidateSerialization_NullFields()
        {
            ChangeFeedProcessorUserException originalException = new ChangeFeedProcessorUserException(null, null);

            byte[]          buffer    = new byte[4096];
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    stream1   = new MemoryStream(buffer);
            MemoryStream    stream2   = new MemoryStream(buffer);

            formatter.Serialize(stream1, originalException);
            ChangeFeedProcessorUserException deserializedException = (ChangeFeedProcessorUserException)formatter.Deserialize(stream2);

            Assert.AreEqual(originalException.Message, deserializedException.Message);
            Assert.IsNull(deserializedException.InnerException);
        }
        public void ValidateSerialization_AllFields()
        {
            ResponseMessage responseMessage = new ResponseMessage();
            ChangeFeedObserverContextCore  observerContext            = new ChangeFeedObserverContextCore(Guid.NewGuid().ToString(), feedResponse: responseMessage, Mock.Of <PartitionCheckpointer>());
            ChangeFeedProcessorContextCore changeFeedProcessorContext = new ChangeFeedProcessorContextCore(observerContext);
            Exception exception = new Exception("randomMessage");
            ChangeFeedProcessorUserException originalException = new ChangeFeedProcessorUserException(exception, changeFeedProcessorContext);

            byte[]          buffer    = new byte[4096];
            BinaryFormatter formatter = new BinaryFormatter();
            MemoryStream    stream1   = new MemoryStream(buffer);
            MemoryStream    stream2   = new MemoryStream(buffer);

            formatter.Serialize(stream1, originalException);
            ChangeFeedProcessorUserException deserializedException = (ChangeFeedProcessorUserException)formatter.Deserialize(stream2);

            Assert.AreEqual(originalException.Message, deserializedException.Message);
            Assert.AreEqual(originalException.InnerException.Message, deserializedException.InnerException.Message);
        }
        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));
        }
        public async Task TestWithRunningProcessor_WithManualCheckpoint()
        {
            int              leaseAcquireCount    = 0;
            int              leaseReleaseCount    = 0;
            int              errorCount           = 0;
            Exception        exceptionToPropagate = new Exception("Stop here");
            int              partitionKey         = 0;
            ManualResetEvent allDocsProcessed     = new ManualResetEvent(false);

            int    processedDocCount      = 0;
            string accumulator            = string.Empty;
            ChangeFeedProcessor processor = this.Container
                                            .GetChangeFeedProcessorBuilderWithManualCheckpoint("test", async(ChangeFeedProcessorContext context, IReadOnlyCollection <dynamic> docs, Func <Task> checkpointAsync, CancellationToken token) =>
            {
                this.ValidateContext(context);
                processedDocCount += docs.Count();
                foreach (dynamic doc in docs)
                {
                    accumulator += doc.id.ToString() + ".";
                }

                if (processedDocCount == 3)
                {
                    // Throwing on the 3rd document, since we checkpointed only on the 1st, we would repeat 2nd and 3rd
                    throw exceptionToPropagate;
                }

                if (processedDocCount == 1)
                {
                    // Checkpointing on the first document to be able to have a point to rollback to
                    await checkpointAsync();
                }

                if (processedDocCount == 12)
                {
                    allDocsProcessed.Set();
                }
            })
                                            .WithInstanceName("random")
                                            .WithMaxItems(1)
                                            .WithLeaseAcquireNotification((string leaseToken) =>
            {
                leaseAcquireCount++;
                return(Task.CompletedTask);
            })
                                            .WithLeaseReleaseNotification((string leaseToken) =>
            {
                leaseReleaseCount++;
                return(Task.CompletedTask);
            })
                                            .WithErrorNotification((string leaseToken, Exception exception) =>
            {
                errorCount++;
                ChangeFeedProcessorUserException cfpException = exception as ChangeFeedProcessorUserException;
                Assert.IsNotNull(cfpException);
                Assert.ReferenceEquals(exceptionToPropagate, exception.InnerException);
                this.ValidateContext(cfpException.ChangeFeedProcessorContext);
                return(Task.CompletedTask);
            })
                                            .WithLeaseContainer(this.LeaseContainer).Build();

            // Start the processor, insert 1 document to generate a checkpoint
            await processor.StartAsync();

            await Task.Delay(BaseChangeFeedClientHelper.ChangeFeedSetupTime);

            foreach (int id in Enumerable.Range(0, 10))
            {
                await this.Container.CreateItemAsync <dynamic>(new { id = id.ToString(), pk = partitionKey });
            }

            bool isStartOk = allDocsProcessed.WaitOne(30 * BaseChangeFeedClientHelper.ChangeFeedSetupTime);
            await processor.StopAsync();

            Assert.IsTrue(isStartOk, "Timed out waiting for docs to process");
            Assert.AreEqual("0.1.2.1.2.3.4.5.6.7.8.9.", accumulator);

            // Make sure the notification APIs got all the events
            Assert.IsTrue(leaseAcquireCount > 0);
            Assert.IsTrue(leaseReleaseCount > 0);
            Assert.AreEqual(1, errorCount);
        }