Ejemplo n.º 1
0
        public void ExplicitRelease()
        {
            int       onnext      = 0;
            Exception e           = null;
            bool      isCompleted = false;

            var observable = new OnDemandObservable <int>();
            var dispose    = observable.AsWeakObservable().Subscribe(new DelegateObserver <int>(i => onnext += i, ex => e = ex, () => { isCompleted = true; }));

            observable.OnNext(3);
            onnext.Is(3);

            observable.OnError(E);
            e.IsNotNull();

            observable.OnCompleted();
            isCompleted.IsTrue();

            // reset values.
            e           = null;
            isCompleted = false;

            // unsubscribe
            dispose.Dispose();

            observable.OnNext(6);
            onnext.Is(3);    // it should not be notified.

            observable.OnError(E);
            e.IsNull();

            observable.OnCompleted();
            isCompleted.IsFalse();
        }
Ejemplo n.º 2
0
        public void ImplicitReleaseByLostObserver()
        {
            int       onnext      = 0;
            Exception e           = null;
            bool      isCompleted = false;

            var         observable = new OnDemandObservable <int>();
            IDisposable disposable = null;
            WeakReference <IObserver <int> > weak = null;

            void exec()
            {
                var o = new DelegateObserver <int>(i => onnext += i, ex => e = ex, () => { isCompleted = true; });

                disposable = observable.AsWeakObservable().Subscribe(o);
                weak       = new WeakReference <IObserver <int> >(o, true);
            };

            // execute inner method.
            exec();

            observable.OnNext(3);
            onnext.Is(3);

            observable.OnError(E);
            e.IsNotNull();

            observable.OnCompleted();
            isCompleted.IsTrue();

            // reset values.
            e           = null;
            isCompleted = false;

            // Force GC.
            GC.Collect(0, GCCollectionMode.Forced);
            GC.WaitForPendingFinalizers();
            GC.Collect(0, GCCollectionMode.Forced); // just to be sure
            System.Threading.Tasks.Task.Delay(100).Wait();

            // It should be notified. Because reference of IDisposable is alive and not unsubscribe automatically,
            // and so subscription is valid. Reference of IObserver<T> is kept internally, and was not collected by
            // the garbage collector.
            observable.OnNext(6);
            onnext.Is(9);

            observable.OnError(E);
            e.IsNotNull();

            observable.OnCompleted();
            isCompleted.IsTrue();
        }
Ejemplo n.º 3
0
        public void ImplicitReleaseByLostObserver()
        {
            int       onnext      = 0;
            Exception e           = null;
            bool      isCompleted = false;

            var         observable = new OnDemandObservable <int>();
            IDisposable disposable = null;
            WeakReference <IObserver <int> > weak = null;

            void exec()
            {
                var o = new DelegateObserver <int>(i => onnext += i, ex => e = ex, () => { isCompleted = true; });

                disposable = observable.VeryWeakSubscribe(o);
                weak       = new WeakReference <IObserver <int> >(o, true);
            };

            // execute inner method.
            exec();

            observable.OnNext(3);
            onnext.Is(3);

            observable.OnError(E);
            e.IsNotNull();

            observable.OnCompleted();
            isCompleted.IsTrue();

            // reset values.
            e           = null;
            isCompleted = false;

            // Force GC.
            GC.Collect(0, GCCollectionMode.Forced);
            GC.WaitForPendingFinalizers();
            GC.Collect(0, GCCollectionMode.Forced); // just to be sure
            System.Threading.Tasks.Task.Delay(100).Wait();

            observable.OnNext(6);
            onnext.Is(3);    // it should not be notified.

            observable.OnError(E);
            e.IsNull();

            observable.OnCompleted();
            isCompleted.IsFalse();
        }
        public void NoSpecifiedDelegates()
        {
            var testObservable = new OnDemandObservable <int>();
            var observer       = new DelegateObserver <int>();

            testObservable.Subscribe(observer);

            testObservable.OnNext(1);
            testObservable.OnError(new ArgumentException());
            testObservable.OnCompleted();
        }
Ejemplo n.º 5
0
        public void KeepReferences()
        {
            int       onnext      = 0;
            Exception e           = null;
            bool      isCompleted = false;

            var observable = new OnDemandObservable <int>();
            var observer   = new DelegateObserver <int>(i => onnext += i, ex => e = ex, () => { isCompleted = true; });
            var dispose    = observable.WeakSubscribe(observer);

            // observer = null;

            observable.OnNext(3);
            onnext.Is(3);

            observable.OnError(E);
            e.IsNotNull();

            observable.OnCompleted();
            isCompleted.IsTrue();

            // reset values.
            e           = null;
            isCompleted = false;

            // Force GC.
            GC.Collect(0, GCCollectionMode.Forced);
            GC.WaitForPendingFinalizers();
            GC.Collect(0, GCCollectionMode.Forced); // just to be sure.
            System.Threading.Tasks.Task.Delay(100).Wait();

            observable.OnNext(6);
            onnext.Is(9);    // it should be notified.

            observable.OnError(E);
            e.IsNotNull();

            observable.OnCompleted();
            isCompleted.IsTrue();
        }
        public void SpecifyFull()
        {
            int       t         = 0;
            bool      completed = false;
            Exception ee        = null;

            var testObservable = new OnDemandObservable <int>();
            var observer       = new DelegateObserver <int>(i => t = i, exception => ee = exception, () => completed = true);

            testObservable.Subscribe(observer);

            testObservable.OnNext(1);
            t.Is(1);

            testObservable.OnError(new Exception());
            ee.IsNotNull();

            testObservable.OnCompleted();
            completed.IsTrue();
        }