public void CollectionKeyIsNotACollection_ThrowsException()
        {
            Task CallNext(GraphQueryExecutionContext context, CancellationToken token)
            {
                return(Task.CompletedTask);
            }

            var next      = new GraphMiddlewareInvocationDelegate <GraphQueryExecutionContext>(CallNext);
            var queue     = new SubscriptionEventQueue();
            var publisher = new PublishRaisedSubscriptionEventsMiddleware <GraphSchema>(queue);

            var server = new TestServerBuilder()
                         .Build();

            var context = server.CreateQueryContextBuilder()
                          .Build();

            var col = context.Items.GetOrAdd(
                SubscriptionConstants.RAISED_EVENTS_COLLECTION_KEY, (_) => new object());

            Assert.ThrowsAsync <GraphExecutionException>(async() =>
            {
                await publisher.InvokeAsync(context, next, default);
            });
        }
        public async Task EmptyCollectionOnContext_YieldsNothingPublished()
        {
            var nextCalled = false;

            Task CallNext(GraphQueryExecutionContext context, CancellationToken token)
            {
                nextCalled = true;
                return(Task.CompletedTask);
            }

            var next      = new GraphMiddlewareInvocationDelegate <GraphQueryExecutionContext>(CallNext);
            var queue     = new SubscriptionEventQueue();
            var publisher = new PublishRaisedSubscriptionEventsMiddleware <GraphSchema>(queue);

            var server = new TestServerBuilder()
                         .Build();

            var context = server.CreateQueryContextBuilder()
                          .Build();

            var col = context.Items.GetOrAdd(
                SubscriptionConstants.RAISED_EVENTS_COLLECTION_KEY, (_) => new List <SubscriptionEventProxy>());

            await publisher.InvokeAsync(context, next, default);

            Assert.IsTrue(nextCalled);
            Assert.AreEqual(0, queue.Count);
        }
        public async Task NoItemsOnContext_YieldsNothingPublished()
        {
            var nextCalled = false;

            Task CallNext(GraphQueryExecutionContext context, CancellationToken token)
            {
                nextCalled = true;
                return(Task.CompletedTask);
            }

            var next      = new GraphMiddlewareInvocationDelegate <GraphQueryExecutionContext>(CallNext);
            var queue     = new SubscriptionEventQueue();
            var publisher = new PublishRaisedSubscriptionEventsMiddleware <GraphSchema>(queue);

            var server = new TestServerBuilder()
                         .Build();

            var context = server.CreateQueryContextBuilder()
                          .Build();

            await publisher.InvokeAsync(context, next, default);

            Assert.IsTrue(nextCalled);
            Assert.AreEqual(0, queue.Count);
        }
        public async Task PollEventQueue_EmptiesQueueOfEventsAndPublishesThem()
        {
            var logger = new Mock <IGraphEventLogger>();

            logger.Setup(x => x.Log(It.IsAny <LogLevel>(), It.IsAny <Func <SubscriptionEventPublishedLogEntry> >()));

            var publisher = new Mock <ISubscriptionEventPublisher>();

            publisher.Setup(x => x.PublishEvent(It.IsAny <SubscriptionEvent>())).Returns(Task.CompletedTask);

            var collection = new ServiceCollection();

            collection.AddSingleton <IGraphEventLogger>(logger.Object);
            collection.AddSingleton <ISubscriptionEventPublisher>(publisher.Object);
            var provider = collection.BuildServiceProvider();

            var eventData = new SubscriptionEvent()
            {
                Id             = Guid.NewGuid().ToString(),
                SchemaTypeName = "someSchemaName",
                Data           = new object(),
                DataTypeName   = "someTypeName",
                EventName      = "testEvent",
            };

            var eventQueue = new SubscriptionEventQueue();

            eventQueue.Enqueue(eventData);

            var publicationService = new SubscriptionPublicationService(provider, eventQueue);
            await publicationService.PollEventQueue();

            logger.Verify(x => x.Log(LogLevel.Debug, It.IsAny <Func <IGraphLogEntry> >()), Times.Once(), "logger not called exactly once");
            publisher.Verify(x => x.PublishEvent(It.IsAny <SubscriptionEvent>()), Times.Once(), "published failed to publish one times");
            Assert.AreEqual(0, eventQueue.Count);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="PublishRaisedSubscriptionEventsMiddleware{TSchema}" /> class.
 /// </summary>
 /// <param name="eventQueue">The event queue to add outgoing events to.</param>
 public PublishRaisedSubscriptionEventsMiddleware(SubscriptionEventQueue eventQueue)
 {
     _eventQueue = Validation.ThrowIfNullOrReturn(eventQueue, nameof(eventQueue));
 }
예제 #6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SubscriptionPublicationService" /> class.
 /// </summary>
 /// <param name="provider">The provider.</param>
 /// <param name="eventQueue">The event queue.</param>
 public SubscriptionPublicationService(IServiceProvider provider, SubscriptionEventQueue eventQueue)
 {
     _eventsToRaise = Validation.ThrowIfNullOrReturn(eventQueue, nameof(eventQueue));
     _provider      = Validation.ThrowIfNullOrReturn(provider, nameof(provider));
 }