Esempio n. 1
0
        private async Task SendEvent(ServiceSubscription serviceSubscription, CompassEvent compassEvent)
        {
            await _sendToEndpointService.SendToEndpointAsync(new List <ServiceSubscription> {
                serviceSubscription
            },
                                                             compassEvent);

            await _queueEventService.DeQueueEventAsync(compassEvent);
        }
        private async Task <CompassResult> SendToSubscriptionsAsync(CompassEvent compassEvent, IReadOnlyCollection <ServiceSubscription> subscriptions)
        {
            var registeredApplicationTask =
                _dataStore.GetByDocumentsIdAsync <RegisteredApplication>(
                    subscriptions.Select(subscription => subscription.ApplicationToken.ToString()));
            var sendToEndpointTask = _sendToEndpointService.SendToEndpointAsync(subscriptions, compassEvent);
            await Task.WhenAll(registeredApplicationTask, sendToEndpointTask);

            var registeredApplications = await registeredApplicationTask;
            var responses     = await sendToEndpointTask;
            var compassResult = new CompassResult {
                Status = CompassResponseStatus.Success
            };

            if (responses != null)
            {
                compassResult.Status  = GetCompassResponseStatus(responses);
                compassResult.Payload = BuildResponse(registeredApplications, responses);
            }

            return(compassResult);
        }
        public async Task RouteRequest_SendToAllSubscriptions()
        {
            // Arrange
            var subscriptions = new List<ServiceSubscription>
            {
                new ServiceSubscription()
                {
                    ApplicationToken = Guid.NewGuid(),
                    ApplicationUri = new Uri("http://www.example.com/"),
                    SubscribedEvents = new List<string>() { "test"}
                },
                new ServiceSubscription()
                {
                    ApplicationToken = Guid.NewGuid(),
                    ApplicationUri = new Uri("http://www.example2.com/"),
                    SubscribedEvents = new List<string>() { "test"}
                }
            };

            var payload = new {test = "test"};
            var compassEvent = new CompassEvent
            {
                EventName = "test", 
                ApplicationToken = Guid.NewGuid(),
                Payload = payload
            };

            A.CallTo(() => _getServiceSubscriptionsForEventService.GetServiceSubscriptionsAsync(compassEvent))
             .Returns(subscriptions);

            // Act
            await _sut.RouteRequest(compassEvent);

            // Assert
            A.CallTo(() => _sendToEndpointService.SendToEndpointAsync(subscriptions, compassEvent))
             .MustHaveHappened(Repeated.Exactly.Once);
        }
        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);
        }