public async Task Subscription_ReplayEvents()
        {
            // Arrange
            var serviceSubscription = new ServiceSubscription
            {
                SubscribedEvents = { "test", "test1" },
                ApplicationToken = Guid.NewGuid()
            };

            A.CallTo(() => _validateApplicationTokenService.ValidateApplicationTokenAsync(serviceSubscription.ApplicationToken))
            .Returns(Task.FromResult(new RegisteredApplication()));

            // Act
            await _sut.SubscribeAsync(serviceSubscription);

            // Assert
            A.CallTo(() => _replayQueuedEventsService.ReplayQueuedEvents(serviceSubscription))
            .MustHaveHappened(Repeated.Exactly.Once);
        }
Exemplo n.º 2
0
        public async Task <ServiceSubscription> SubscribeAsync(ServiceSubscription serviceSubcription)
        {
            var registeredApplication =
                await _validateApplicationTokenService.ValidateApplicationTokenAsync(
                    serviceSubcription.ApplicationToken);

            var subscription = await SaveSubscriptionAsync(serviceSubcription);

            var updateTask = UpdateLastSeenAsync(registeredApplication);

            // TODO: Determine if we should replay events...
            // TODO: How do we prevent duplicate data while replaying events?
            // This could happen if Service A went down, and Service B, listening
            // to the same event is still up when Service A comes back up. It will
            // receive the the event a second time.
            var replayTask = _replayQueuedEventsService.ReplayQueuedEvents(serviceSubcription);

            await Task.WhenAll(new List <Task> {
                replayTask, updateTask
            });

            return(subscription);
        }
        public async Task ReplayQueuedEvents_SendMultipleEvents()
        {
            // Arrange
            var serviceSubscription = new ServiceSubscription
            {
                ApplicationUri   = new Uri("http://www.example.com/"),
                SubscribedEvents = new List <string>()
                {
                    "test", "test2"
                }
            };

            var compassEvents = new List <CompassEvent>()
            {
                new CompassEvent
                {
                    ApplicationToken = Guid.NewGuid(),
                    EventName        = "test",
                    Identifier       = Guid.NewGuid(),
                    Payload          = new { test = "test" }
                },
                new CompassEvent
                {
                    ApplicationToken = Guid.NewGuid(),
                    EventName        = "test2",
                    Identifier       = Guid.NewGuid(),
                    Payload          = new { test2 = "test" }
                }
            };

            var queuedEvent = new QueuedEvents {
                Events = compassEvents
            };

            A.CallTo(() => _dataStore.GetQueuedEventsAsync())
            .Returns(queuedEvent);
            A.CallTo(() => _dataStore.GetRegisteredApplicationAsync(serviceSubscription.ApplicationToken))
            .Returns(Task.FromResult(
                         new RegisteredApplication()
            {
                LastEventsSubscribed = new List <string>()
                {
                    "test"
                }
            }));

            // Act
            await _sut.ReplayQueuedEvents(serviceSubscription);

            // Assert
            A.CallTo(() => _sendToEndpointService.SendToEndpointAsync(
                         A <List <ServiceSubscription> > .That.IsSameSequenceAs(
                             new List <ServiceSubscription> {
                serviceSubscription
            }), compassEvents[0]))
            .MustHaveHappened(Repeated.Exactly.Once);

            A.CallTo(() => _sendToEndpointService.SendToEndpointAsync(
                         A <List <ServiceSubscription> > .That.IsSameSequenceAs(
                             new List <ServiceSubscription> {
                serviceSubscription
            }), compassEvents[1]))
            .MustHaveHappened(Repeated.Exactly.Once);
        }