Example #1
0
        public void Expiration()
        {
            // Arrange
            var topicName = "HousekeepingTests.Expiration";
            var subName   = topicName + "_Sub1";
            var topic     = _publisher.AddOrUpdateTopic(new Topic {
                Name = topicName
            });
            var sub1 = _consumer.AddOrUpdateSubscription(new Subscription
            {
                Name = subName,
                TopicSubscriptions = new List <TopicSubscription> {
                    new TopicSubscription {
                        TopicId = topic.Id.Value, Enabled = true
                    }
                },
            });

            var utcNow = DateTime.UtcNow;

            _publisher.Publish(topicName, eventName: topicName + "1", expirationDateUtc: utcNow.AddSeconds(5), payload: "1");
            _publisher.Publish(topicName, eventName: topicName + "2", expirationDateUtc: utcNow.AddSeconds(5), payload: "2");
            _publisher.Publish(topicName, eventName: topicName + "3", expirationDateUtc: utcNow.AddMinutes(15), payload: "3");

            var visibilityTimeout = 1;                                                         // Must expire asap
            var ce1 = _consumer.ConsumeNext(subName, visibilityTimeout: visibilityTimeout).SingleOrDefault();
            var ce2 = _consumer.ConsumeNext(subName, visibilityTimeout: 10).SingleOrDefault(); // Still invisible after expiration
            var ce3 = _consumer.ConsumeNext(subName, visibilityTimeout: visibilityTimeout).SingleOrDefault();

            Assert.Equal("1", ce1.Payload); // Not yet expired
            Assert.Equal("2", ce2.Payload);
            Assert.Equal("3", ce3.Payload);
            Thread.Sleep(TimeSpan.FromSeconds(5 + 1));

            _consumer.PerformHouseKeepingTasks();

            // Check that only 1 se in table
            var eventNames = _fixture.GetEventNamesForFailedEvents(sub1.Id.Value);

            Assert.Equal(1, eventNames.Count);
            Assert.Equal(topicName + "1", eventNames.First());
            // 2 has expired, but is still invisible, so may still be processed
        }