public void AsyncReactiveSubjectBase_SubscribeAsync()
        {
            var s = new MyAsyncReactiveSubject <int>();

            var iv2    = default(IAsyncReactiveObserver <int>);
            var uri2   = default(Uri);
            var state2 = default(object);

            s.SubscribeImpl = async(o, u, x, ct) =>
            {
                iv2    = o;
                uri2   = u;
                state2 = x;

                await Task.Yield();

                return(null);
            };

            var iv    = new MyObserver <int>();
            var uri   = new Uri("foo://bar");
            var state = "qux";

            s.SubscribeAsync(iv, uri, state, CancellationToken.None).Wait();

            Assert.AreSame(iv, iv2);
            Assert.AreSame(uri, uri2);
            Assert.AreSame(state, state2);
        }
        public void AsyncReactiveObservableBase_Subscribe()
        {
            var s = new MyAsyncReactiveObservable <int>();

            var iv2    = default(IAsyncReactiveObserver <int>);
            var uri2   = default(Uri);
            var state2 = default(object);

            s.SubscribeAsyncImpl = (o, u, x, token) =>
            {
                iv2    = o;
                uri2   = u;
                state2 = x;

                return(Task.FromResult <IAsyncReactiveSubscription>(null));
            };

            var iv    = new MyObserver <int>();
            var uri   = new Uri("foo://bar");
            var state = "qux";

            _ = s.SubscribeAsync(iv, uri, state).Result;

            Assert.AreSame(iv, iv2);
            Assert.AreSame(uri, uri2);
            Assert.AreSame(state, state2);
        }
Exemplo n.º 3
0
        public void ReactiveObservableBase_Subscribe()
        {
            var s = new MyReactiveObservable <int>();

            var iv2    = default(IReactiveObserver <int>);
            var uri2   = default(Uri);
            var state2 = default(object);

            s.SubscribeImpl = (o, u, x) =>
            {
                iv2    = o;
                uri2   = u;
                state2 = x;

                return(null);
            };

            var iv    = new MyObserver <int>();
            var uri   = new Uri("foo://bar");
            var state = "qux";

            s.Subscribe(iv, uri, state);

            Assert.AreSame(iv, iv2);
            Assert.AreSame(uri, uri2);
            Assert.AreSame(state, state2);
        }
Exemplo n.º 4
0
        public void AsObserver_Hides()
        {
            var obs = new MyObserver();
            var res = obs.AsObserver();

            Assert.IsFalse(object.ReferenceEquals(obs, res));
        }
Exemplo n.º 5
0
        public void TestSpecialInterfaces()
        {
            var         specialPerson = new SpecialPerson();
            var         myObserver    = new MyObserver();
            IDisposable subscription  = specialPerson.Subscribe(myObserver);

            specialPerson.FallIll();
        }
Exemplo n.º 6
0
        public void Do4()
        {
            var obs = new MyObserver();

            Enumerable.Range(0, 10).Do(obs).ForEach(_ => { });

            Assert.IsTrue(obs.Done);
            Assert.AreEqual(45, obs.Sum);
        }
Exemplo n.º 7
0
        private static void Qbserver_ReentrancyDuringInitialization_Impl(Func <IAsyncReactiveQbserver <int>, Task> action, Func <MyObserver <int>, TaskCompletionSource <object> > complete, Action <MyObserver <int> > assert)
        {
            var provider = new MyProvider();
            var observer = provider.CreateQbserver <int>(Expression.Default(typeof(IAsyncReactiveQbserver <int>)));

            var opAsync = action(observer);

            provider.Acquire.WaitOne();

            AssertEx.ThrowsException <AggregateException>(
                () =>
            {
                observer.OnNextAsync(43, CancellationToken.None).Wait();
            },
                ex =>
            {
                var ioe = ex.InnerException as InvalidOperationException;
                Assert.IsNotNull(ioe);
                Assert.IsTrue(ioe.Message.Contains("Concurrent calls"));
            });

            AssertEx.ThrowsException <AggregateException>(
                () =>
            {
                observer.OnErrorAsync(new Exception(), CancellationToken.None).Wait();
            },
                ex =>
            {
                var ioe = ex.InnerException as InvalidOperationException;
                Assert.IsNotNull(ioe);
                Assert.IsTrue(ioe.Message.Contains("Concurrent calls"));
            });

            AssertEx.ThrowsException <AggregateException>(
                () =>
            {
                observer.OnCompletedAsync(CancellationToken.None).Wait();
            },
                ex =>
            {
                var ioe = ex.InnerException as InvalidOperationException;
                Assert.IsNotNull(ioe);
                Assert.IsTrue(ioe.Message.Contains("Concurrent calls"));
            });

            var iv = new MyObserver <int>();

            var tcs = provider.GetObserverAsyncCoreTask;

            tcs.SetResult(iv);

            complete(iv).SetResult(null);
            opAsync.Wait();

            assert(iv);
        }
Exemplo n.º 8
0
        public void IObservable_Subscribe()
        {
            // Arrange
            var observable = new MyObservable();
            var observer   = new MyObserver();

            // Act
            observable.Subscribe(observer);

            // Assert
        }
Exemplo n.º 9
0
        public void ReactiveObservableBase_Subscribe_ArgumentChecking()
        {
            var s = new MyReactiveObservable <int>();

            var iv    = new MyObserver <int>();
            var uri   = new Uri("foo://bar");
            var state = "qux";

            Assert.ThrowsException <ArgumentNullException>(() => s.Subscribe(null, uri, state));
            Assert.ThrowsException <ArgumentNullException>(() => s.Subscribe(iv, null, state));
        }
        public void AsyncReactiveSubjectBase_SubscribeAsync_ArgumentChecking()
        {
            var s = new MyAsyncReactiveSubject <int>();

            var iv    = new MyObserver <int>();
            var uri   = new Uri("foo://bar");
            var state = "qux";

            Assert.ThrowsException <ArgumentNullException>(() => s.SubscribeAsync(null, uri, state, CancellationToken.None));
            Assert.ThrowsException <ArgumentNullException>(() => s.SubscribeAsync(iv, null, state, CancellationToken.None));
        }
Exemplo n.º 11
0
        public void StatefulObserver_Basics()
        {
            {
                var mo = new MyObserver();

                mo.Start();
                Assert.IsTrue(mo.Started);
                //Assert.IsFalse(mo.StateChanged); // TODO: Hmm, can we do this?

                mo.OnNext(42);
                mo.OnNext(43);
                mo.OnCompleted();

                Assert.IsTrue(new[] { 42, 43 }.SequenceEqual(mo.Values));
                Assert.IsTrue(mo.Done);

                mo.Dispose();
                Assert.IsTrue(mo.Disposed);

                mo.Dispose();
                Assert.IsTrue(mo.Disposed);
            }

            {
                var mo = new MyObserver();

                mo.Start();
                Assert.IsTrue(mo.Started);
                //Assert.IsFalse(mo.StateChanged); // TODO: Hmm, can we do this?

                var ex = new Exception();

                mo.OnNext(42);
                mo.OnNext(43);
                mo.OnError(ex);

                Assert.IsTrue(new[] { 42, 43 }.SequenceEqual(mo.Values));
                Assert.AreSame(ex, mo.Error);

                mo.Dispose();
                Assert.IsTrue(mo.Disposed);

                mo.Dispose();
                Assert.IsTrue(mo.Disposed);
            }
        }
Exemplo n.º 12
0
        public void ToNotifier_Forwards()
        {
            var obsn = new MyObserver();

            obsn.ToNotifier()(Notification.CreateOnNext <int>(42));
            Assert.Equal(obsn.HasOnNext, 42);

            var ex   = new Exception();
            var obse = new MyObserver();

            obse.ToNotifier()(Notification.CreateOnError <int>(ex));
            Assert.Same(ex, obse.HasOnError);

            var obsc = new MyObserver();

            obsc.ToNotifier()(Notification.CreateOnCompleted <int>());
            Assert.True(obsc.HasOnCompleted);
        }
Exemplo n.º 13
0
        public void ToNotifier_Forwards()
        {
            var obsn = new MyObserver();

            obsn.ToNotifier()(new Notification <int> .OnNext(42));
            Assert.AreEqual(obsn.HasOnNext, 42);

            var ex   = new Exception();
            var obse = new MyObserver();

            obse.ToNotifier()(new Notification <int> .OnError(ex));
            Assert.AreSame(ex, obse.HasOnError);

            var obsc = new MyObserver();

            obsc.ToNotifier()(new Notification <int> .OnCompleted());
            Assert.IsTrue(obsc.HasOnCompleted);
        }
Exemplo n.º 14
0
        public void AsObserver_Forwards()
        {
            var obsn = new MyObserver();

            obsn.AsObserver().OnNext(42);
            Assert.AreEqual(obsn.HasOnNext, 42);

            var ex   = new Exception();
            var obse = new MyObserver();

            obse.AsObserver().OnError(ex);
            Assert.AreSame(ex, obse.HasOnError);

            var obsc = new MyObserver();

            obsc.AsObserver().OnCompleted();
            Assert.IsTrue(obsc.HasOnCompleted);
        }
Exemplo n.º 15
0
        public void test_observer_notify()
        {
            MySubject subject = new MySubject();

            MyObserver[] observers = new MyObserver[] { new MyObserver(), new MyObserver() };

            foreach (MyObserver o in observers)
            {
                subject.attach(o);
            }

            subject.notify();

            foreach (MyObserver o in observers)
            {
                Assert.IsTrue(o.hasBeenNotified);
            }
        }
Exemplo n.º 16
0
        public static void Test4()
        {
            var id = Thread.CurrentThread.ManagedThreadId.ToString();

            Console.WriteLine($"Thread id = {id}");

            IObservable <int> input = Observable.Range(1, 15);

            input.Where(i => i % 2 == 0).Subscribe(Console.Write);

            var ob            = new MyObserver <int>();
            var subscripttion = input.Subscribe(ob);

            var observable    = new MyRangeObservable(5, 8);
            var subscription2 = observable.Subscribe(ob);

            subscripttion.Dispose();
            subscription2.Dispose();
        }
Exemplo n.º 17
0
        public void Qbserver_RegularFlow()
        {
            var provider = new MyProvider();
            var observer = provider.CreateQbserver <int>(Expression.Default(typeof(IAsyncReactiveQbserver <int>)));

            var iv = new MyObserver <int>();

            provider.GetObserverAsyncCoreTask.SetResult(iv);

            iv.OnNextTask.SetResult(null);
            observer.OnNextAsync(42, CancellationToken.None).Wait();
            observer.OnNextAsync(43, CancellationToken.None).Wait();
            observer.OnNextAsync(44, CancellationToken.None).Wait();

            iv.OnCompletedTask.SetResult(null);
            observer.OnCompletedAsync(CancellationToken.None).Wait();

            Assert.IsTrue(iv.OnNextLog.SequenceEqual(new[] { 42, 43, 44 }));
            Assert.IsTrue(iv.OnCompletedLog);
        }
Exemplo n.º 18
0
        public void Should_allow_rx_subscribers()
        {
            var observer = new MyObserver <A>();

            using (var bus = ServiceBusFactory.New(x =>
            {
                x.ReceiveFrom("loopback://localhost/queue");

                x.Subscribe(s =>
                {
                    s.Observe(observer);
                    s.Observe(Observer.Create <A>(m => Console.WriteLine(m.Name)));
                });
            }))
            {
                bus.Publish(new A {
                    Name = "Joe"
                });

                observer.Value.Wait(8.Seconds()).ShouldBeTrue();
            }
        }
Exemplo n.º 19
0
        public void StatefulObserver_Versioning_NotSupportedByDefault()
        {
            var state         = new MockOperatorStateContainer();
            var writerFactory = state.CreateWriter();
            var readerFactory = state.CreateReader();

            var mo = new MyObserver()
            {
                State = 42
            };

            mo.Start();

            var writer = writerFactory.Create(mo);

            Assert.ThrowsException <NotSupportedException>(() => mo.SaveState(writer, new Version(2, 0, 0, 0)));

            var mor = new MyObserver();

            var reader = readerFactory.Create(mor);

            Assert.ThrowsException <NotSupportedException>(() => mor.LoadState(reader, new Version(2, 0, 0, 0)));
        }
Exemplo n.º 20
0
        static void Main(string[] args)
        {
            var query = from n in Enumerable.Range(1, 5) select n;

            #region Without Rx

            //foreach (var item in query)
            //{
            //    Console.WriteLine(item);
            //}

            //IAmDone(); // inform external function about completion of processing

            #endregion

            #region With Rx

            // Implementation using Observable
            //Console.WriteLine("UI Thread Id : " + Thread.CurrentThread.ManagedThreadId);
            //var observableQuery2 = query.ToObservable();
            //observableQuery2.Subscribe(DisplayNumber, IAmDone); // first function will run for each result in the observable query, second function will run at the end

            #endregion

            #region With Rx and Threading

            //Console.WriteLine("Threading Example");
            //Console.WriteLine("UI Thread Id : " + Thread.CurrentThread.ManagedThreadId);
            //var observableQuery3 = query.ToObservable(NewThreadScheduler.Default); // Produces value on a diff thread than UI thread
            //observableQuery3.Subscribe(DisplayNumber, IAmDone);

            #endregion

            #region With Rx and Observer Pattern

            Console.WriteLine("With Observer Pattern");
            Console.WriteLine("UI Thread Id : " + Thread.CurrentThread.ManagedThreadId);
            var observableQuery4 = query.ToObservable(NewThreadScheduler.Default);

            var observer = Observer.Create <int>(DisplayNumber); // observer creation
            observableQuery4.Subscribe(observer);                // observer subscribing to observarble query aka subject

            #endregion

            #region With RX - Completion function

            Console.WriteLine("With RX - Completion function");
            var newQuery = from n in Enumerable.Range(1, 25) select n.ToString();

            var newObservableQuery = newQuery.ToObservable();

            newObservableQuery.Subscribe(n =>
            {
                Console.WriteLine(string.Format("{0}", SlowString(n)));
            }, IAmDone);

            #endregion


            #region Custom Observer

            Console.WriteLine("Custom Observer");

            var numbers = new List <int> {
                1, 2, 3
            }.ToObservable();
            var myObserver = new MyObserver();
            numbers.Subscribe(myObserver);

            #endregion

            Console.ReadLine();
        }
Exemplo n.º 21
0
 public void AddObserver(MyObserver observer)
 {
     observers.Add(observer);
 }
Exemplo n.º 22
0
        public void StatefulObserver_State()
        {
            var state         = new MockOperatorStateContainer();
            var writerFactory = state.CreateWriter();
            var readerFactory = state.CreateReader();

            var mo = new MyObserver()
            {
                State = 42
            };

            mo.Start();
            Assert.IsTrue(mo.Started);
            Assert.IsTrue(mo.StateChanged);

            Assert.ThrowsException <ArgumentNullException>(() => mo.SaveState(null, mo.Version));

            var writer = writerFactory.Create(mo);

            mo.SaveState(writer, mo.Version);

            Assert.IsTrue(mo.StateChanged);
            mo.OnStateSaved();
            Assert.IsFalse(mo.StateChanged);

            var mor = new MyObserver();

            var reader = readerFactory.Create(mor);

            Assert.ThrowsException <ArgumentNullException>(() => mor.LoadState(null, mor.Version));

            mor.LoadState(reader, mor.Version);

            mor.Start();
            Assert.IsTrue(mor.Started);

            Assert.AreEqual(42, mor.State);

            mo.State = 43;
            Assert.IsTrue(mo.StateChanged);

            writer = writerFactory.Create(mo);
            mo.SaveState(writer, mo.Version);
            mo.OnStateSaved();
            Assert.IsFalse(mo.StateChanged);

            var moq = new MyObserver();

            reader = readerFactory.Create(moq);
            moq.LoadState(reader, moq.Version);

            moq.Start();
            Assert.IsTrue(moq.Started);

            Assert.AreEqual(43, moq.State);

            mo.Dispose();

            Assert.IsTrue(mo.Disposed);

            writer = writerFactory.Create(mo);
            mo.SaveState(writer, mo.Version);
            mo.OnStateSaved();

            var mop = new MyObserver();

            reader = readerFactory.Create(mop);
            mop.LoadState(reader, mop.Version);
            mop.Start();

            Assert.IsTrue(mop.Disposed);
            Assert.IsFalse(mop.Started);
        }
Exemplo n.º 23
0
 public void RemoveObserver(MyObserver observer)
 {
     observers.Remove(observer);
 }
Exemplo n.º 24
0
 public static void SubscribeService(MyObserver <T> observer)
 {
     m_Observers.Add(observer);
     observer.Update(m_Service);
 }