<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using NUnit.Framework;
using WampSharp.PubSub.Client;
using WampSharp.Tests.PubSub.Helpers;
using WampSharp.Tests.TestHelpers;

namespace WampSharp.Tests.PubSub
{
    [TestFixture]
    public class WampPubSubSubjectTests
    {
        [Test]
        public void BeforeSubscribeCallNoSubscriptionsAreMade()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            Assert.That(requestManager.Subscriptions, Is.Empty);
            Assert.That(requestManager.SubscriptionRemovals, Is.Empty);
            Assert.That(requestManager.Publications, Is.Empty);
        }

        [Test]
        public void SubscribeCallCallsServerProxySubscribe()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            IDisposable cancelation = subject.Subscribe(x => { });

            Assert.That(requestManager.SubscriptionRemovals, Is.Empty);
            Assert.That(requestManager.Publications, Is.Empty);
            Assert.That(requestManager.Subscriptions.Count, Is.EqualTo(1));

            WampSubscribeRequest<MockRaw> subscription =
                requestManager.Subscriptions.First();

            Assert.That(subscription.TopicUri, Is.EqualTo("myTopic"));
        }

        [Test]
        public void SubscribeCallsServerProxySubscribeOnceForAllSubscribers()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            IDisposable cancelation = subject.Subscribe(x => { });
            IDisposable cancelation2 = subject.Subscribe(x => { });

            Assert.That(requestManager.SubscriptionRemovals, Is.Empty);
            Assert.That(requestManager.Publications, Is.Empty);
            Assert.That(requestManager.Subscriptions.Count, Is.EqualTo(1));
        }

        [Test]
        public void DisposeDoesntCallServerProxyUnsubscribeIfObserversAreStillSubscribed()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            IDisposable cancelation = subject.Subscribe(x => { });
            IDisposable cancelation2 = subject.Subscribe(x => { });

            cancelation.Dispose();

            Assert.That(requestManager.SubscriptionRemovals, Is.Empty);
            Assert.That(requestManager.Publications, Is.Empty);
            Assert.That(requestManager.Subscriptions.Count, Is.EqualTo(1));
        }

        [Test]
        public void DisposeCallsServerProxyUnsubscribeIfNoObserversAreSubscribed()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            IDisposable cancelation = subject.Subscribe(x => { });
            IDisposable cancelation2 = subject.Subscribe(x => { });

            cancelation.Dispose();
            cancelation2.Dispose();

            Assert.That(requestManager.SubscriptionRemovals.Count, Is.EqualTo(1));
            Assert.That(requestManager.Publications, Is.Empty);
            Assert.That(requestManager.Subscriptions.Count, Is.EqualTo(1));

            WampSubscribeRequest<MockRaw> removal = 
                requestManager.SubscriptionRemovals.First();

            Assert.That(removal.TopicUri, Is.EqualTo("myTopic"));
        }

        [Test]
        public void ClientEventCallsObserverOnNext()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            List<object> events = new List<object>();
            IDisposable cancelation = subject.Subscribe(x => events.Add(x));

            WampSubscribeRequest<MockRaw> subscription =
                requestManager.Subscriptions.First();

            subscription.Client.Event("myTopic", new MockRaw(13));

            Assert.That(events.Count, Is.EqualTo(1));
            Assert.That(events[0], Is.EqualTo(13));
        }

        private static WampPubSubClientFactory<MockRaw> GetClientFactory(MockWampPubSubRequestManager<MockRaw> requestManager)
        {
            WampPubSubClientFactory<MockRaw> clientFactory =
                new WampPubSubClientFactory<MockRaw>
                    (new MockWampPubSubServerProxyFactory<MockRaw>
                         (client => requestManager.GetServer(client)),
                     new MockRawFormatter());
            return clientFactory;
        }
    }
}<commit_msg>Added event publishing pubsub unit tests<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Linq;
using System.Reactive.Subjects;
using NUnit.Framework;
using WampSharp.PubSub.Client;
using WampSharp.Tests.PubSub.Helpers;
using WampSharp.Tests.TestHelpers;

namespace WampSharp.Tests.PubSub
{
    [TestFixture]
    public class WampPubSubSubjectTests
    {
        [Test]
        public void BeforeSubscribeCallNoSubscriptionsAreMade()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            Assert.That(requestManager.Subscriptions, Is.Empty);
            Assert.That(requestManager.SubscriptionRemovals, Is.Empty);
            Assert.That(requestManager.Publications, Is.Empty);
        }

        [Test]
        public void SubscribeCallCallsServerProxySubscribe()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            IDisposable cancelation = subject.Subscribe(x => { });

            Assert.That(requestManager.SubscriptionRemovals, Is.Empty);
            Assert.That(requestManager.Publications, Is.Empty);
            Assert.That(requestManager.Subscriptions.Count, Is.EqualTo(1));

            WampSubscribeRequest<MockRaw> subscription =
                requestManager.Subscriptions.First();

            Assert.That(subscription.TopicUri, Is.EqualTo("myTopic"));
        }

        [Test]
        public void SubscribeCallsServerProxySubscribeOnceForAllSubscribers()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            IDisposable cancelation = subject.Subscribe(x => { });
            IDisposable cancelation2 = subject.Subscribe(x => { });

            Assert.That(requestManager.SubscriptionRemovals, Is.Empty);
            Assert.That(requestManager.Publications, Is.Empty);
            Assert.That(requestManager.Subscriptions.Count, Is.EqualTo(1));
        }

        [Test]
        public void DisposeDoesntCallServerProxyUnsubscribeIfObserversAreStillSubscribed()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            IDisposable cancelation = subject.Subscribe(x => { });
            IDisposable cancelation2 = subject.Subscribe(x => { });

            cancelation.Dispose();

            Assert.That(requestManager.SubscriptionRemovals, Is.Empty);
            Assert.That(requestManager.Publications, Is.Empty);
            Assert.That(requestManager.Subscriptions.Count, Is.EqualTo(1));
        }

        [Test]
        public void DisposeCallsServerProxyUnsubscribeIfNoObserversAreSubscribed()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            IDisposable cancelation = subject.Subscribe(x => { });
            IDisposable cancelation2 = subject.Subscribe(x => { });

            cancelation.Dispose();
            cancelation2.Dispose();

            Assert.That(requestManager.SubscriptionRemovals.Count, Is.EqualTo(1));
            Assert.That(requestManager.Publications, Is.Empty);
            Assert.That(requestManager.Subscriptions.Count, Is.EqualTo(1));

            WampSubscribeRequest<MockRaw> removal = 
                requestManager.SubscriptionRemovals.First();

            Assert.That(removal.TopicUri, Is.EqualTo("myTopic"));
        }

        [Test]
        public void ClientEventCallsObserverOnNext()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            List<object> events = new List<object>();
            IDisposable cancelation = subject.Subscribe(x => events.Add(x));

            WampSubscribeRequest<MockRaw> subscription =
                requestManager.Subscriptions.First();

            subscription.Client.Event("myTopic", new MockRaw(13));

            Assert.That(events.Count, Is.EqualTo(1));
            Assert.That(events[0], Is.EqualTo(13));
        }

        [Test]
        public void OnNextCallCallsServerProxyEvent()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            IDisposable cancelation = subject.Subscribe(x => { });

            subject.OnNext(108);

            Assert.That(requestManager.Publications.Count, Is.EqualTo(1));

            WampPublishRequest<MockRaw> publication =
                requestManager.Publications.FirstOrDefault();

            Assert.That(publication.Event, Is.EqualTo(108));
            Assert.That(publication.ExcludeMe, Is.EqualTo(false)); // Our specification
            Assert.That(publication.TopicUri, Is.EqualTo("myTopic"));
        }


        [Test]
        public void OnCompletedCallCallsObserverOnCompleted()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            bool onCompleted = false;
            IDisposable cancelation = subject.Subscribe(x => { }, () => onCompleted = true);

            Assert.That(onCompleted, Is.EqualTo(false));
            subject.OnCompleted();
            Assert.That(onCompleted, Is.EqualTo(true));
        }

        [Test]
        public void OnCompletedCallCallsServerProxyUnsubscribe()
        {
            MockWampPubSubRequestManager<MockRaw> requestManager =
                new MockWampPubSubRequestManager<MockRaw>();

            WampPubSubClientFactory<MockRaw> clientFactory = GetClientFactory(requestManager);

            ISubject<object> subject = clientFactory.GetSubject<object>("myTopic");

            bool onCompleted = false;
            IDisposable cancelation = subject.Subscribe(x => { });

            Assert.That(requestManager.SubscriptionRemovals.Count, Is.EqualTo(0));
            subject.OnCompleted();
            Assert.That(requestManager.SubscriptionRemovals.Count, Is.EqualTo(1));
            
            WampSubscribeRequest<MockRaw> removal = 
                requestManager.SubscriptionRemovals.First();
            Assert.That(removal.TopicUri, Is.EqualTo("myTopic"));
        }

        private static WampPubSubClientFactory<MockRaw> GetClientFactory(MockWampPubSubRequestManager<MockRaw> requestManager)
        {
            WampPubSubClientFactory<MockRaw> clientFactory =
                new WampPubSubClientFactory<MockRaw>
                    (new MockWampPubSubServerProxyFactory<MockRaw>
                         (client => requestManager.GetServer(client)),
                     new MockRawFormatter());
            return clientFactory;
        }
    }
}
{'fraction_non_alphanumeric': 0.09360197949805585, 'fraction_numerical': 0.002757158006362672, 'mean_word_length': 2.72557282064788, 'pattern_counts': {'":': 0, '<': 100, '<?xml version=': 0, '>': 122, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1366106', 'n_tokens_mistral': 3676, 'n_tokens_neox': 3553, 'n_words': 507}