Exemplo n.º 1
0
        public void Compare_Repos_July_vs_Today(string owner)
        {
            NotificationBuilder         nbuilder        = new NotificationBuilder();
            NotificationChannelsBuilder channelsBuilder = new NotificationChannelsBuilder().UseSmtpPickupDirectory(@"c:\work").UseSmtpPickupDirectory(@"c:\work2\send");

            List <RepositoryEvent> Events = new List <RepositoryEvent>();
            ITableSnapshotBuilder  builder;

            builder = RepositoryListSnapshotBuilder.CreateInMemorySnapshotFromRequest(Resources.GetRepositoryResponse(owner, RepositoryResponseGeneration.July));
            ITableSnapshot microsoftSnapshotJuly = builder.Build();

            builder = RepositoryListSnapshotBuilder.CreateInMemorySnapshotFromRequest(this.Extractor.GetMetadataAsynch(this.Token, owner).Result);
            ITableSnapshot microsoftSnapshotToday = builder.Build();

            TableDiffByLookup differ = new TableDiffByLookup(microsoftSnapshotJuly, microsoftSnapshotToday);


            differ.DifferencesDelegate = (deletedRecord, inserted) =>
            {
                EventType   et       = EventType.Created;
                IDataRecord template = inserted;
                if ((deletedRecord != null) && (inserted != null))
                {
                    et       = EventType.Modified;
                    template = inserted;
                }

                if ((deletedRecord != null) && (inserted == null))
                {
                    et       = EventType.Deleted;
                    template = deletedRecord;
                    //RepositoryTableSnapshot.CreatedAtFieldName
                }

                RepositoryEvent ev = EventFactory.CreateRepositoryEvent(template, et);
                nbuilder.AddEvent(ev);
            };
            differ.Execute();
            //Assert.Equal(1312, differ.OldSnapshotRecordCount);

            //create Notification
            //Deliver Notification

            nbuilder.AddChannels(channelsBuilder);

            List <Notification> toSend = new List <Notification>();

            for (int i = 0; i < 5; i++)
            {
                Notification noti = nbuilder.Build();
                noti.From = new NotificationAddress()
                {
                    Identifier = "*****@*****.**"
                };
                noti.To = new NotificationAddress[] { noti.From };
                toSend.Add(noti);
            }

            Postman.DeliverNotification(toSend);
        }
        public void Repository_Diff()
        {
            ITableSnapshotBuilder builder;

            builder = RepositoryListSnapshotBuilder.CreateInMemorySnapshotFromRequest(Resources.GetRepositoryResponse("Microsoft", RepositoryResponseGeneration.July));
            ITableSnapshot microsoftSnapshotJuly = builder.Build();

            builder = RepositoryListSnapshotBuilder.CreateInMemorySnapshotFromRequest(Resources.GetRepositoryResponse("Xamarin", RepositoryResponseGeneration.July));
            ITableSnapshot xamarinSnapshotJuly = builder.Build();

            TableDiffByLookup differ = new TableDiffByLookup(microsoftSnapshotJuly, xamarinSnapshotJuly);

            differ.DifferencesDelegate = (deletedRecord, inserted) =>
            {
                System.Diagnostics.Debug.WriteLine("");
            };
            differ.Execute();
            Assert.Equal(136, differ.NewSnapshotRecordCount);
            Assert.Equal(1312, differ.OldSnapshotRecordCount);
        }
Exemplo n.º 3
0
        public static IEvent[] CreateEvents(string key, Func <string, ITableSnapshotBuilder> currentQuerySnapshotBuilderDelegate, Func <string, ITableSnapshotBuilder> lastSnapshotBuilderDelegate, SnapshotRepository snapshotRepository, bool persistAsLastSnapshot, ILogger logger, Func <IDataRecord, EventType, IEvent> factoryCallback)
        {
            Guard.ArgumentNotNullOrEmptyString(key, nameof(key));
            Guard.ArgumentNotNull(currentQuerySnapshotBuilderDelegate, nameof(currentQuerySnapshotBuilderDelegate));
            Guard.ArgumentNotNull(lastSnapshotBuilderDelegate, nameof(lastSnapshotBuilderDelegate));
            Guard.AssertNotNull(logger);
            Guard.AssertNotNull(snapshotRepository);
            List <IEvent> events = new List <IEvent>();

            using (DisposableFile temporarySnapshot = DisposableFile.GetTempFile())
            {
                Task <ITableSnapshot> queryNow = Task.Factory.StartNew <ITableSnapshot>(() =>
                {
                    ITableSnapshotBuilder builder      = currentQuerySnapshotBuilderDelegate(temporarySnapshot.Path);
                    ITableSnapshot snapShotFromRequest = builder.Build();
                    return(snapShotFromRequest);
                });

                Task <ITableSnapshot> lastSnapshot1 = Task.Factory.StartNew <ITableSnapshot>(() =>
                {
                    string localTempFile               = Path.GetTempFileName(); //DELETE The File at the end ??
                    ITableSnapshotBuilder builder      = lastSnapshotBuilderDelegate(localTempFile);
                    ITableSnapshot snapShotFromRequest = builder.Build();
                    return(snapShotFromRequest);
                });


                int msTimeoutLoggingSteps = 1000;
                while (Task.WaitAll(new Task[] { queryNow, lastSnapshot1 }, msTimeoutLoggingSteps) == false)
                {
                    logger.LogTrace("CreateEvents.Wait-Loop.{0}ms for '{1}'", msTimeoutLoggingSteps, key);
                }


                try
                {
                    TableDiffByLookup differ = new TableDiffByLookup(lastSnapshot1.Result, queryNow.Result);
                    differ.DifferencesDelegate = (deletedRecord, inserted) =>
                    {
                        EventType   et       = EventType.Created;
                        IDataRecord template = inserted;
                        if ((deletedRecord != null) && (inserted != null))
                        {
                            et       = EventType.Modified;
                            template = inserted;
                        }

                        if ((deletedRecord != null) && (inserted == null))
                        {
                            et       = EventType.Deleted;
                            template = deletedRecord;
                            //RepositoryTableSnapshot.CreatedAtFieldName
                        }
                        if (et != EventType.Modified)
                        {
                            //IEvent ev = //EventFactory.CreateRepositoryEvent(template, et);
                            IEvent ev = factoryCallback(template, et);
                            Guard.AssertNotNull(ev);
                            events.Add(ev);
                        }
                        else
                        {
                            // we ignore modifications!
                        }
                    };
                    differ.Execute();
                }
                finally
                {
                    if (queryNow.IsCompletedSuccessfully())
                    {
                        queryNow.Result.Dispose();
                    }
                    if (lastSnapshot1.IsCompletedSuccessfully())
                    {
                        lastSnapshot1.Result.Dispose();
                    }
                }
                if (persistAsLastSnapshot)
                {
                    snapshotRepository.Push(key, temporarySnapshot.Path);
                }
                else
                {
                    logger.LogInformation("Snapshot {0} NOT persited!", key);
                }
            } //using temporary snapshotfile disposable

            return(events.ToArray());
            //throw new NotImplementedException();
        }