Exemple #1
0
        public void TestDisposeMultipleSubjects()
        {
            int sum1 = 0;
            int sum2 = 0;

            SubEvent1 event1 = new SubEvent1();
            SubEvent2 event2 = new SubEvent2();

            PubSubSubject <SuperEvent> subject = new PubSubSubject <SuperEvent>();
            var disposable1 = subject.Subscribe(Observer.Create <SubEvent1>(x => sum1 += 100));
            var disposable2 = subject.Subscribe(Observer.Create <SubEvent2>(x => sum2 += 500));

            subject.OnNext(event1);
            subject.OnNext(event2);
            subject.OnNext(event2);
            Assert.Equal(sum1, 100);
            Assert.Equal(sum2, 1000);

            // Check that unsubscribing from SubEvent1 does not affect other subscriptions
            disposable1.Dispose();
            subject.OnNext(event1);
            subject.OnNext(event2);
            Assert.Equal(sum1, 100);
            Assert.Equal(sum2, 1500);

            // Unsubscribe from the remaining event types
            disposable2.Dispose();
            subject.OnNext(event1);
            subject.OnNext(event2);
            Assert.Equal(sum1, 100);
            Assert.Equal(sum2, 1500);
        }
Exemple #2
0
        /// <summary>
        /// Register the IObserver for the particular Time event.
        /// </summary>
        /// <param name="observer">The handler to register</param>
        public void RegisterObserver <U>(IObserver <U> observer) where U : Time
        {
            if (_disposed)
            {
                return;
            }

            _handlers.Subscribe(observer);
        }
Exemple #3
0
        /// <summary>
        /// Create the handler to manage incoming NamingEvent types
        /// </summary>
        /// <returns>The server handler</returns>
        private IObserver <TransportEvent <NamingEvent> > CreateServerHandler()
        {
            PubSubSubject <NamingEvent> subject = new PubSubSubject <NamingEvent>();

            subject.Subscribe(new NamingLookupRequestObserver(this));
            subject.Subscribe(new NamingGetAllRequestObserver(this));
            subject.Subscribe(new NamingRegisterRequestObserver(this));
            subject.Subscribe(new NamingUnregisterRequestObserver(this));
            return(new ServerHandler(subject));
        }
Exemple #4
0
        /// <summary>
        /// Create handler to handle async responses from the NameServer.
        /// </summary>
        /// <returns>The client handler to manage responses from the NameServer</returns>
        private IObserver <TransportEvent <NamingEvent> > CreateClientHandler()
        {
            PubSubSubject <NamingEvent> subject = new PubSubSubject <NamingEvent>();

            subject.Subscribe(Observer.Create <NamingLookupResponse>(msg => HandleResponse(_lookupResponseQueue, msg)));
            subject.Subscribe(Observer.Create <NamingGetAllResponse>(msg => HandleResponse(_getAllResponseQueue, msg)));
            subject.Subscribe(Observer.Create <NamingRegisterResponse>(msg => HandleResponse(_registerResponseQueue, msg)));
            subject.Subscribe(Observer.Create <NamingUnregisterResponse>(msg => HandleResponse(_unregisterResponseQueue, msg)));
            return(new ClientObserver(subject));
        }
Exemple #5
0
        public void TestPubSubSubjectMultipleThreads()
        {
            int sum = 0;

            PubSubSubject <int> subject = new PubSubSubject <int>();

            subject.Subscribe(Observer.Create <int>(x => sum += x));

            Thread[] threads = new Thread[10];
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(() =>
                {
                    for (int j = 0; j < 10000; j++)
                    {
                        subject.OnNext(1);
                    }
                });

                threads[i].Start();
            }

            foreach (Thread thread in threads)
            {
                thread.Join();
            }

            Assert.Equal(sum, 100000);
        }
Exemple #6
0
        public void TestPubSubSubjectMultipleThreads()
        {
            int sum = 0;

            PubSubSubject<int> subject = new PubSubSubject<int>();
            subject.Subscribe(Observer.Create<int>(x => sum += x));

            Thread[] threads = new Thread[10];
            for (int i = 0; i < threads.Length; i++)
            {
                threads[i] = new Thread(() =>
                {
                    for (int j = 0; j < 10000; j++)
                    {
                        subject.OnNext(1);
                    }
                });

                threads[i].Start();
            }

            foreach (Thread thread in threads)
            {
                thread.Join();
            }

            Assert.Equal(sum, 100000);
        }
Exemple #7
0
 /// <summary>
 /// Subscribe a set of IObservers for a particular Time event
 /// </summary>
 /// <param name="observers">The set of observers to subscribe</param>
 private void Subscribe <U>(ISet <IObserver <U> > observers) where U : Time
 {
     foreach (IObserver <U> observer in observers)
     {
         _handlers.Subscribe(observer);
     }
 }
Exemple #8
0
        public void TestMultipleTypes()
        {
            int sum1 = 0;
            int sum2 = 0;

            PubSubSubject <SuperEvent> subject = new PubSubSubject <SuperEvent>();

            subject.Subscribe(Observer.Create <SubEvent1>(x => sum1 += 100));
            subject.Subscribe(Observer.Create <SubEvent2>(x => sum2 += 500));

            subject.OnNext(new SubEvent1());
            subject.OnNext(new SubEvent2());
            subject.OnNext(new SubEvent2());

            Assert.Equal(sum1, 100);
            Assert.Equal(sum2, 1000);
        }
Exemple #9
0
        public void TestMultipleTypes()
        {
            int sum1 = 0;
            int sum2 = 0;

            PubSubSubject<SuperEvent> subject = new PubSubSubject<SuperEvent>();
            subject.Subscribe(Observer.Create<SubEvent1>(x => sum1 += 100));
            subject.Subscribe(Observer.Create<SubEvent2>(x => sum2 += 500));

            subject.OnNext(new SubEvent1());
            subject.OnNext(new SubEvent2());
            subject.OnNext(new SubEvent2());

            Assert.Equal(sum1, 100);
            Assert.Equal(sum2, 1000);
        }
Exemple #10
0
        public void TestDisposeSingleSubject()
        {
            int sum = 0;

            PubSubSubject <int> subject = new PubSubSubject <int>();
            var disposable = subject.Subscribe(Observer.Create <int>(x => sum += x));

            subject.OnNext(10);
            subject.OnNext(10);
            subject.OnNext(10);
            Assert.Equal(30, sum);

            // Unregister the subject and check that calling OnNext does nothing
            disposable.Dispose();
            subject.OnNext(10);
            Assert.Equal(30, sum);
        }
Exemple #11
0
        public void TestOnError()
        {
            int sum = 0;

            PubSubSubject <int> subject = new PubSubSubject <int>();

            subject.Subscribe(Observer.Create <int>(x => sum += x));

            subject.OnNext(10);
            Assert.Equal(10, sum);

            subject.OnNext(10);
            Assert.Equal(20, sum);

            // Check that after calling OnError, OnNext will do nothing
            subject.OnError(new Exception("error"));
            subject.OnNext(10);
            Assert.Equal(20, sum);
        }
Exemple #12
0
        public void TestOnCompleted()
        {
            int sum = 0;

            PubSubSubject <int> subject = new PubSubSubject <int>();

            subject.Subscribe(Observer.Create <int>(x => sum += x));

            subject.OnNext(10);
            Assert.Equal(10, sum);

            subject.OnNext(10);
            Assert.Equal(20, sum);

            // Check that after calling OnCompleted, OnNext will do nothing
            subject.OnCompleted();
            subject.OnNext(10);
            Assert.Equal(20, sum);
        }
Exemple #13
0
        public void TestPubSubSubjectSingleThread()
        {
            int sum = 0;

            // Observer that adds sum of numbers up to and including x
            PubSubSubject<int> subject = new PubSubSubject<int>();
            subject.Subscribe(Observer.Create<int>(
                x =>
                {
                    for (int i = 0; i <= x; i++)
                    {
                        sum += i;
                    }
                }));

            subject.OnNext(10);
            subject.OnCompleted();
            Assert.Equal(sum, 55);
        }
Exemple #14
0
        public void TestPubSubSubjectSingleThread()
        {
            int sum = 0;

            // Observer that adds sum of numbers up to and including x
            PubSubSubject <int> subject = new PubSubSubject <int>();

            subject.Subscribe(Observer.Create <int>(
                                  x =>
            {
                for (int i = 0; i <= x; i++)
                {
                    sum += i;
                }
            }));

            subject.OnNext(10);
            subject.OnCompleted();
            Assert.Equal(sum, 55);
        }
Exemple #15
0
        public void TestOnError()
        {
            int sum = 0;

            PubSubSubject<int> subject = new PubSubSubject<int>();
            subject.Subscribe(Observer.Create<int>(x => sum += x));

            subject.OnNext(10);
            Assert.Equal(10, sum);

            subject.OnNext(10);
            Assert.Equal(20, sum);

            // Check that after calling OnError, OnNext will do nothing
            subject.OnError(new Exception("error"));
            subject.OnNext(10);
            Assert.Equal(20, sum);
        }
Exemple #16
0
        public void TestDisposeSingleSubject()
        {
            int sum = 0;

            PubSubSubject<int> subject = new PubSubSubject<int>();
            var disposable = subject.Subscribe(Observer.Create<int>(x => sum += x));
            
            subject.OnNext(10);
            subject.OnNext(10);
            subject.OnNext(10);
            Assert.Equal(30, sum);

            // Unregister the subject and check that calling OnNext does nothing
            disposable.Dispose();
            subject.OnNext(10);
            Assert.Equal(30, sum);
        }
Exemple #17
0
        public void TestOnCompleted()
        {
            int sum = 0;

            PubSubSubject<int> subject = new PubSubSubject<int>();
            subject.Subscribe(Observer.Create<int>(x => sum += x));

            subject.OnNext(10);
            Assert.Equal(10, sum);

            subject.OnNext(10);
            Assert.Equal(20, sum);

            // Check that after calling OnCompleted, OnNext will do nothing
            subject.OnCompleted();
            subject.OnNext(10);
            Assert.Equal(20, sum);
        }
Exemple #18
0
        public void TestDisposeMultipleSubjects()
        {
            int sum1 = 0;
            int sum2 = 0;

            SubEvent1 event1 = new SubEvent1();
            SubEvent2 event2 = new SubEvent2();

            PubSubSubject<SuperEvent> subject = new PubSubSubject<SuperEvent>();
            var disposable1 = subject.Subscribe(Observer.Create<SubEvent1>(x => sum1 += 100));
            var disposable2 = subject.Subscribe(Observer.Create<SubEvent2>(x => sum2 += 500));

            subject.OnNext(event1);
            subject.OnNext(event2);
            subject.OnNext(event2);
            Assert.Equal(sum1, 100);
            Assert.Equal(sum2, 1000);

            // Check that unsubscribing from SubEvent1 does not affect other subscriptions
            disposable1.Dispose();
            subject.OnNext(event1);
            subject.OnNext(event2);
            Assert.Equal(sum1, 100);
            Assert.Equal(sum2, 1500);

            // Unsubscribe from the remaining event types
            disposable2.Dispose();
            subject.OnNext(event1);
            subject.OnNext(event2);
            Assert.Equal(sum1, 100);
            Assert.Equal(sum2, 1500);
        }