public override ChangeFeedProcessorBuilder GetChangeFeedProcessorBuilder <T>(
            string processorName,
            ChangesHandler <T> onChangesDelegate)
        {
            CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(null);

            using (diagnosticsContext.CreateScope("GetChangeFeedProcessorBuilder"))
            {
                return(this.Container.GetChangeFeedProcessorBuilder(
                           processorName,
                           async(IReadOnlyCollection <JObject> documents, CancellationToken cancellationToken) =>
                {
                    List <T> decryptedItems = new List <T>(documents.Count);

                    foreach (JObject document in documents)
                    {
                        JObject decryptedDocument = await this.EncryptionProcessor.DecryptAsync(
                            document,
                            diagnosticsContext,
                            cancellationToken);

                        decryptedItems.Add(decryptedDocument.ToObject <T>());
                    }

                    // Call the original passed in delegate
                    await onChangesDelegate(decryptedItems, cancellationToken);
                }));
            }
        }
        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);
Ejemplo n.º 4
0
 public override ChangeFeedProcessorBuilder GetChangeFeedProcessorBuilder <T>(
     string processorName,
     ChangesHandler <T> onChangesDelegate)
 {
     // TODO: need client SDK to expose underlying feedIterator to make decryption work for this scenario
     // Issue #1484
     return(this.container.GetChangeFeedProcessorBuilder(
                processorName,
                onChangesDelegate));
 }
        public override ChangeFeedProcessorBuilder GetChangeFeedProcessorBuilder <T>(
            string processorName,
            ChangesHandler <T> onChangesDelegate)
        {
            CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(null);

            using (diagnosticsContext.CreateScope("GetChangeFeedProcessorBuilder"))
            {
                return(this.container.GetChangeFeedProcessorBuilder(
                           processorName,
                           async(IReadOnlyCollection <JObject> documents, CancellationToken cancellationToken) =>
                {
                    List <T> decryptedItems = new List <T>(documents.Count);

                    foreach (JObject document in documents)
                    {
                        EncryptionSettings encryptionSettings = await this.GetOrUpdateEncryptionSettingsFromCacheAsync(obsoleteEncryptionSettings: null, cancellationToken: cancellationToken);
                        try
                        {
                            JObject decryptedDocument = await EncryptionProcessor.DecryptAsync(
                                document,
                                encryptionSettings,
                                diagnosticsContext,
                                cancellationToken);

                            decryptedItems.Add(decryptedDocument.ToObject <T>());
                        }

                        // we cannot rely currently on a specific exception, this is due to the fact that the run time issue can be variable,
                        // we can hit issue with either Json serialization say an item was not encrypted but the policy shows it as encrypted,
                        // or we could hit a MicrosoftDataEncryptionException from MDE lib etc.
                        catch (Exception)
                        {
                            // most likely the encryption policy has changed.
                            encryptionSettings = await this.GetOrUpdateEncryptionSettingsFromCacheAsync(
                                obsoleteEncryptionSettings: encryptionSettings,
                                cancellationToken: cancellationToken);

                            JObject decryptedDocument = await EncryptionProcessor.DecryptAsync(
                                document,
                                encryptionSettings,
                                diagnosticsContext,
                                cancellationToken);

                            decryptedItems.Add(decryptedDocument.ToObject <T>());
                        }
                    }

                    // Call the original passed in delegate
                    await onChangesDelegate(decryptedItems, cancellationToken);
                }));
            }
        }
Ejemplo n.º 6
0
 protected virtual void OnChanged()
 {
     // if there are subscribers
     if (ChangesHandler != null)
     {
         ChangesHandler?.Invoke(this, new FlightEventsArgs()
         {
             FlightNumber = flightNumber,
             FlightAction = "changed to " + cbxChangeRoute.SelectedValue,
             DateTime     = DateTime.Now
         });
     }
 }
Ejemplo n.º 7
0
 protected virtual void OnStarted()
 {
     // if there are subscribers
     if (ChangesHandler != null)
     {
         ChangesHandler?.Invoke(this, new FlightEventsArgs()
         {
             FlightNumber = flightNumber,
             FlightAction = "started",
             DateTime     = DateTime.Now
         });
     }
 }
Ejemplo n.º 8
0
        public override ChangeFeedProcessorBuilder GetChangeFeedProcessorBuilder <T>(
            string processorName,
            ChangesHandler <T> onChangesDelegate)
        {
            return(this.container.GetChangeFeedProcessorBuilder(
                       processorName,
                       async(
                           IReadOnlyCollection <JObject> documents,
                           CancellationToken cancellationToken) =>
            {
                List <T> decryptItems = await this.DecryptChangeFeedDocumentsAsync <T>(
                    documents,
                    cancellationToken);

                // Call the original passed in delegate
                await onChangesDelegate(decryptItems, cancellationToken);
            }));
        }
        public override ChangeFeedProcessorBuilder GetChangeFeedProcessorBuilder <T>(
            string processorName,
            ChangesHandler <T> onChangesDelegate)
        {
            CosmosDiagnosticsContext diagnosticsContext = CosmosDiagnosticsContext.Create(null);

            using (diagnosticsContext.CreateScope("GetChangeFeedProcessorBuilder"))
            {
                return(this.container.GetChangeFeedProcessorBuilder(
                           processorName,
                           async(IReadOnlyCollection <JObject> documents, CancellationToken cancellationToken) =>
                {
                    List <T> decryptItems = new List <T>(documents.Count);
                    if (typeof(T) == typeof(DecryptableItem))
                    {
                        foreach (JToken value in documents)
                        {
                            DecryptableItemCore item = new DecryptableItemCore(
                                value,
                                this.Encryptor,
                                this.CosmosSerializer);

                            decryptItems.Add((T)(object)item);
                        }
                    }
                    else
                    {
                        foreach (JObject document in documents)
                        {
                            (JObject decryptedDocument, DecryptionContext _) = await EncryptionProcessor.DecryptAsync(
                                document,
                                this.Encryptor,
                                diagnosticsContext,
                                cancellationToken);

                            decryptItems.Add(decryptedDocument.ToObject <T>());
                        }
                    }

                    // Call the original passed in delegate
                    await onChangesDelegate(decryptItems, cancellationToken);
                }));
            }
        }
        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 override ChangeFeedProcessorBuilder GetChangeFeedProcessorBuilder <T>(
            string processorName,
            ChangesHandler <T> onChangesDelegate)
        {
            if (processorName == null)
            {
                throw new ArgumentNullException(nameof(processorName));
            }

            if (onChangesDelegate == null)
            {
                throw new ArgumentNullException(nameof(onChangesDelegate));
            }

            ChangeFeedObserverFactoryCore <T> observerFactory     = new ChangeFeedObserverFactoryCore <T>(onChangesDelegate);
            ChangeFeedProcessorCore <T>       changeFeedProcessor = new ChangeFeedProcessorCore <T>(observerFactory);

            return(new ChangeFeedProcessorBuilder(
                       processorName: processorName,
                       container: this,
                       changeFeedProcessor: changeFeedProcessor,
                       applyBuilderConfiguration: changeFeedProcessor.ApplyBuildConfiguration));
        }
 public override ChangeFeedProcessorBuilder GetChangeFeedProcessorBuilder <T>(
     string processorName,
     ChangesHandler <T> onChangesDelegate)
 {
     return(this.container.GetChangeFeedProcessorBuilder <T>(processorName, onChangesDelegate));
 }
 public async Task StartAsync(ChangesHandler <EventDataModel> changesHandler)
 {
     this.changesHandler = changesHandler;
     await this.InitClientsAsync();
 }
Ejemplo n.º 14
0
 public override ChangeFeedProcessorBuilder GetChangeFeedProcessorBuilder <T>(string processorName, ChangesHandler <T> onChangesDelegate)
 {
     return(GetChangeFeedEstimatorBuilderReturnValue);
 }
 public ChangeFeedObserverBase(ChangesHandler <T> onChanges)
 {
     this.onChanges = onChanges;
 }
 public override ChangeFeedProcessorBuilder GetChangeFeedProcessorBuilder <T>(string processorName, ChangesHandler <T> onChangesDelegate) => throw new NotImplementedException();
Ejemplo n.º 17
0
 public ChangeFeedObserverFactoryCore(ChangesHandler <T> onChanges)
 {
     this.onChanges = onChanges;
 }