AddSubscription() public method

public AddSubscription ( ISubscription subscription ) : void
subscription ISubscription
return void
        public void TopicStateHasSubscriptionsIfMoreThanOne()
        {
            var topic = new Topic(100, TimeSpan.Zero);
            var sub1 = new Mock<ISubscription>();
            sub1.Setup(m => m.Identity).Returns("1");

            topic.AddSubscription(new Mock<ISubscription>().Object);
            topic.AddSubscription(sub1.Object);
            topic.RemoveSubscription(sub1.Object);

            Assert.Equal(TopicState.HasSubscriptions, topic.State);
        }
        public void TopicStateHasSubscriptions()
        {
            var topic = new Topic(100, TimeSpan.Zero);

            topic.AddSubscription(new Mock<ISubscription>().Object);

            Assert.Equal(TopicState.HasSubscriptions, topic.State);
        }
        public virtual IDisposable Subscribe(ISubscriber subscriber, string cursor, Func <MessageResult, object, Task <bool> > callback, int maxMessages, object state)
        {
            if (subscriber == null)
            {
                throw new ArgumentNullException("subscriber");
            }

            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            Subscription subscription = CreateSubscription(subscriber, cursor, callback, maxMessages, state);

            // Set the subscription for this subscriber
            subscriber.Subscription = subscription;

            var topics = new HashSet <Topic>();

            foreach (var key in subscriber.EventKeys)
            {
                // Create or retrieve topic and set it as HasSubscriptions
                Topic topic = SubscribeTopic(key);

                // Set the subscription for this topic
                subscription.SetEventTopic(key, topic);

                topics.Add(topic);
            }

            subscriber.EventKeyAdded   += _addEvent;
            subscriber.EventKeyRemoved += _removeEvent;
            subscriber.WriteCursor      = subscription.WriteCursor;

            var subscriptionState = new SubscriptionState(subscriber);
            var disposable        = new DisposableAction(_disposeSubscription, subscriptionState);

            // When the subscription itself is disposed then dispose it
            subscription.Disposable = disposable;

            // Add the subscription when it's all set and can be scheduled
            // for work. It's important to do this after everything is wired up for the
            // subscription so that publishes can schedule work at the right time.
            foreach (var topic in topics)
            {
                topic.AddSubscription(subscription);
            }

            subscriptionState.Initialized.Set();

            // If there's a cursor then schedule work for this subscription
            if (!String.IsNullOrEmpty(cursor))
            {
                _broker.Schedule(subscription);
            }

            return(disposable);
        }
        public void TopicStateNoSubscriptions()
        {
            var topic = new Topic(100, TimeSpan.Zero);
            var mock = new Mock<ISubscription>();
            mock.Setup(m => m.Identity).Returns("1");

            topic.AddSubscription(mock.Object);
            topic.RemoveSubscription(mock.Object);

            Assert.Equal(TopicState.NoSubscriptions, topic.State);
        }
        private void AddEvent(ISubscriber subscriber, string eventKey)
        {
            Topic topic = SubscribeTopic(eventKey);

            // Add or update the cursor (in case it already exists)
            if (subscriber.Subscription.AddEvent(eventKey, topic))
            {
                // Add it to the list of subs
                topic.AddSubscription(subscriber.Subscription);
            }
        }
Beispiel #6
0
        public virtual IDisposable Subscribe(ISubscriber subscriber, string cursor, Func <MessageResult, object, Task <bool> > callback, int maxMessages, object state)
        {
            if (subscriber == null)
            {
                throw new ArgumentNullException("subscriber");
            }

            if (callback == null)
            {
                throw new ArgumentNullException("callback");
            }

            Subscription subscription = CreateSubscription(subscriber, cursor, callback, maxMessages, state);

            // Set the subscription for this subscriber
            subscriber.Subscription = subscription;

            var topics = new HashSet <Topic>();

            foreach (var key in subscriber.EventKeys)
            {
                Topic topic = GetTopic(key);

                // Set the subscription for this topic
                subscription.SetEventTopic(key, topic);

                topics.Add(topic);
            }

            subscriber.EventKeyAdded   += _addEvent;
            subscriber.EventKeyRemoved += _removeEvent;
            subscriber.WriteCursor      = subscription.WriteCursor;

            // Add the subscription when it's all set and can be scheduled
            // for work
            foreach (var topic in topics)
            {
                topic.AddSubscription(subscription);
            }

            var disposable = new DisposableAction(_disposeSubscription, subscriber);

            // When the subscription itself is disposed then dispose it
            subscription.Disposable = disposable;

            // If there's a cursor then schedule work for this subscription
            if (!String.IsNullOrEmpty(cursor))
            {
                _broker.Schedule(subscription);
            }

            return(disposable);
        }