コード例 #1
0
ファイル: EventApi.cs プロジェクト: Nojj02/shoppingcart
            public MessageNumberRange(MessageNumber start, int batchSize)
            {
                if (start == MessageNumber.NotSet)
                {
                    throw new InvalidMessageNumberRangeException("Range cannot start with NotSet value");
                }

                Start = start;
                End   = MessageNumber.New(start.Value + batchSize - 1);
            }
コード例 #2
0
        public async Task TakeAllEvents_ThereIsNoTrackingForLastEventNumberYet()
        {
            var api = new TestEventApi();

            api.SetupTestResource(
                resourceName: "resource",
                newTestEvents: new List <TestResourceCreatedEvent>
            {
                new TestResourceCreatedEvent
                {
                    Id = new Guid("0683f052-40f0-4bff-879e-f4bea94c0ed0")
                },
                new TestResourceCreatedEvent
                {
                    Id = new Guid("C471D99B-2C72-44F6-898F-F0BABCBAC9D7")
                },
                new TestResourceCreatedEvent
                {
                    Id = new Guid("6843FE44-3029-47D3-A9B9-C21A3BAB4397")
                }
            });

            var eventTrackingRepository = new ListEventTrackingRepository(new List <EventTracking>
            {
                EventTracking.Preset("resource", MessageNumber.New(1))
            });

            var eventMonitor = new EventMonitor(api, eventTrackingRepository);

            var handler = new OnAnyEventRecordInListEventHandler <TestResourceCreatedEvent>();

            eventMonitor.Subscribe <TestResourceCreatedEvent>("resource", handler);

            await eventMonitor.Poll();

            Assert.Equal(1, handler.Events.Count);
            Assert.Equal(new Guid("6843FE44-3029-47D3-A9B9-C21A3BAB4397"), handler.Events[0].Id);

            var lastMessageNumber = await eventTrackingRepository.GetLastMessageNumber("resource");

            Assert.Equal(2, lastMessageNumber.Value);
        }
コード例 #3
0
ファイル: EventApi.cs プロジェクト: Nojj02/shoppingcart
        public async Task <IList <object> > GetEventsAfterAsync(string resourceName, MessageNumber lastMessageNumber)
        {
            Console.WriteLine($"GetEventsAfterAsync with Last message number: {lastMessageNumber}");
            var allEvents = new List <object>();

            List <object>      retrievedEventsList;
            MessageNumberRange numberRange;

            if (lastMessageNumber == MessageNumber.NotSet)
            {
                numberRange = new MessageNumberRange(MessageNumber.New(0), _batchSize);

                retrievedEventsList = await GetAsync(resourceName, numberRange);

                allEvents.AddRange(retrievedEventsList);
            }
            else
            {
                var skippedBatches = lastMessageNumber.Value / _batchSize;
                numberRange = new MessageNumberRange(MessageNumber.New(skippedBatches * _batchSize), _batchSize);

                retrievedEventsList = await GetAsync(resourceName, numberRange);

                var skippedBatchSize = lastMessageNumber.Value % _batchSize;
                var eventsListStartingFromLastMessageNumber = retrievedEventsList.Skip(skippedBatchSize + 1).ToList();

                allEvents.AddRange(eventsListStartingFromLastMessageNumber);
            }

            while (retrievedEventsList.Count == _batchSize)
            {
                numberRange         = new MessageNumberRange(MessageNumber.New(numberRange.End.Value + 1), _batchSize);
                retrievedEventsList = await GetAsync(resourceName, numberRange);

                allEvents.AddRange(retrievedEventsList);
            }

            return(allEvents);
        }
コード例 #4
0
        public async Task MultipleCallsUntilAllEventsAreRetrieved_EventsCoverMultipleBatches()
        {
            var httpClientWrapper =
                new FakeEventHttpClientWrapper(
                    resourceName: "test-message-type",
                    events: new List <EventInfo>
            {
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("7a60d915-a25a-4678-b25e-e35a45a2f0c0")
                    }
                },
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("9435310E-098B-4598-A378-5862D9A9E9AE")
                    }
                },
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("73DE4B51-1F0E-4428-8D59-8DFBEA8091BA")
                    }
                },
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("093626F4-7125-41CD-9982-785B7D52BCAA")
                    }
                },
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("C911BCDB-F23C-4FA3-BE57-E2C0EC3793DC")
                    }
                },
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("00DE8FA8-98E7-4E65-8CE0-B3C982BD1B03")
                    }
                },
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("F2E3B971-28C9-474E-9EFA-231C86A673B4")
                    }
                },
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("76027085-3B4C-4724-A88C-375E5AB24E7A")
                    }
                },
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("E34AD640-B4D0-4A31-9A19-7F1B869937C2")
                    }
                }
            });

            var apiHelper = new EventApi("http://localhost/", httpClientWrapper, TestResourceEventConverter.Instance,
                                         3);

            var result = await apiHelper.GetEventsAfterAsync(resourceName : "resource", lastMessageNumber : MessageNumber.New(3));

            Assert.Equal(3, httpClientWrapper.MessagesSent.Count);
            Assert.Equal("/events/resource/3-5", httpClientWrapper.MessagesSent[0].RequestUri.AbsolutePath);
            Assert.Equal("/events/resource/6-8", httpClientWrapper.MessagesSent[1].RequestUri.AbsolutePath);
            Assert.Equal("/events/resource/9-11", httpClientWrapper.MessagesSent[2].RequestUri.AbsolutePath);

            Assert.Equal(5, result.Count);
            Assert.Equal(new Guid("C911BCDB-F23C-4FA3-BE57-E2C0EC3793DC"), ((TestResourceCreatedEvent)result[0]).Id);
            Assert.Equal(new Guid("00DE8FA8-98E7-4E65-8CE0-B3C982BD1B03"), ((TestResourceCreatedEvent)result[1]).Id);
            Assert.Equal(new Guid("F2E3B971-28C9-474E-9EFA-231C86A673B4"), ((TestResourceCreatedEvent)result[2]).Id);
            Assert.Equal(new Guid("76027085-3B4C-4724-A88C-375E5AB24E7A"), ((TestResourceCreatedEvent)result[3]).Id);
            Assert.Equal(new Guid("E34AD640-B4D0-4A31-9A19-7F1B869937C2"), ((TestResourceCreatedEvent)result[4]).Id);
        }
コード例 #5
0
        public async Task CallsSecondBatchOfEvents_LastMessageNumberIsHigherThanOneBatchSize()
        {
            var httpClientWrapper =
                new FakeEventHttpClientWrapper(
                    resourceName: "test-message-type",
                    events: new List <EventInfo>
            {
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("7a60d915-a25a-4678-b25e-e35a45a2f0c0")
                    }
                },
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("9435310E-098B-4598-A378-5862D9A9E9AE")
                    }
                },
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("73DE4B51-1F0E-4428-8D59-8DFBEA8091BA")
                    }
                },
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("093626F4-7125-41CD-9982-785B7D52BCAA")
                    }
                },
                new EventInfo
                {
                    EventType = "resource-created",
                    Event     =
                        new TestResourceCreatedEvent
                    {
                        Id = new Guid("C911BCDB-F23C-4FA3-BE57-E2C0EC3793DC")
                    }
                }
            });

            var apiHelper = new EventApi("http://localhost/", httpClientWrapper, TestResourceEventConverter.Instance,
                                         3);

            var result = await apiHelper.GetEventsAfterAsync(resourceName : "resource", lastMessageNumber : MessageNumber.New(3));

            Assert.Equal(1, httpClientWrapper.MessagesSent.Count);
            Assert.Equal("/events/resource/3-5", httpClientWrapper.MessagesSent[0].RequestUri.AbsolutePath);

            Assert.Equal(1, result.Count);
            Assert.Equal(new Guid("C911BCDB-F23C-4FA3-BE57-E2C0EC3793DC"), ((TestResourceCreatedEvent)result[0]).Id);
        }