コード例 #1
0
        public async Task ShouldBeAbleToScheduleAndReceive()
        {
            var tokenSource        = new CancellationTokenSource();
            var token              = tokenSource.Token;
            var receiverLoggerMock = new Mock <ILogger <DomainEventReceiver> >();
            var receiverSection    = configRoot.GetSection("Receiver.Settings");
            var receiverSettings   = GetSettings <ServiceBusReceiverSettings>(receiverSection);
            var receiver           = new DomainEventReceiver(receiverSettings, serviceProvider, receiverLoggerMock.Object);

            receiver.Closed += (r, e) => tokenSource.Cancel();

            var publisherLoggerMock = new Mock <ILogger <DomainEventPublisher> >();
            var publisherSection    = configRoot.GetSection("Publisher.Settings");
            var publisherSettings   = GetSettings <ServiceBusPublisherSettings>(publisherSection);
            var publisher           = new DomainEventPublisher(publisherSettings, publisherLoggerMock.Object);

            var @event = new TestEvent {
                TheInt    = r.Next(),
                TheString = Guid.NewGuid().ToString()
            };

            var correlationId = Guid.NewGuid().ToString();

            try {
                await publisher.ScheduleMessageAsync(@event, correlationId, DateTime.UtcNow.AddSeconds(20));
            } finally {
                Assert.Null(publisher.Error);
            }

            receiver.Receive(new Dictionary <string, Type> {
                { typeof(TestEvent).FullName,
                  typeof(TestEvent) }
            });
            var start   = DateTime.Now;
            var elapsed = DateTime.Now.Subtract(start);

            while (!TestEvent.Instances.ContainsKey(correlationId) && (DateTime.Now - start) < new TimeSpan(0, 0, 30))
            {
                if (token.IsCancellationRequested)
                {
                    if (receiver.Error != null)
                    {
                        Assert.Equal(string.Empty, receiver.Error.Description);
                        Assert.Equal(string.Empty, receiver.Error.Condition);
                    }
                    Assert.True(receiver.Error == null);
                }
                Thread.Sleep(1000);
                elapsed = DateTime.Now.Subtract(start);
            } // run for 30 seconds
            Assert.True(elapsed.TotalSeconds >= 20);
            Assert.True(TestEvent.Instances.Any());
            Assert.True(TestEvent.Instances.ContainsKey(correlationId));
            Assert.NotNull(TestEvent.Instances[correlationId]);
            Assert.Equal(@event.TheString, TestEvent.Instances[correlationId].TheString);
            Assert.Equal(@event.TheInt, TestEvent.Instances[correlationId].TheInt);
        }