Пример #1
0
        internal void Run()
        {
            IProcessor <T, T> w;
            IProcessor <T, T> x;

            lock (this)
            {
                if (stop)
                {
                    return;
                }

                Interlocked.Increment(ref wip);
                w = window;
                if (Volatile.Read(ref cancelled))
                {
                    x      = new UnicastProcessor <T>(capacityHint, this.InnerDone);
                    window = x;
                }
                else
                {
                    x      = null;
                    window = null;
                }
            }

            w?.OnComplete();

            if (x != null)
            {
                Emit(x);
            }
        }
Пример #2
0
            public void OnNext(T element)
            {
                var w = window;

                if (w == null)
                {
                    int a = Volatile.Read(ref active);
                    if (a != 0 && Interlocked.CompareExchange(ref active, a + 1, a) == a)
                    {
                        w      = new UnicastProcessor <T>(size, OnTerminate);
                        window = w;

                        actual.OnNext(w);
                    }
                }

                w.OnNext(element);

                int c = count + 1;

                if (c == size)
                {
                    count = 0;
                    w.OnComplete();
                    window = null;
                }
                else
                {
                    count = c;
                }
            }
Пример #3
0
        public void OnNext(T t)
        {
            var w = window;

            if (w == null)
            {
                Interlocked.Increment(ref wip);

                w      = new UnicastProcessor <T>(size, this.InnerDone);
                window = w;

                actual.OnNext(w);
            }

            w.OnNext(t);

            int p = produced + 1;

            if (p == size)
            {
                w.OnComplete();
                window   = null;
                produced = 0;
            }
            else
            {
                produced = p;
            }
        }
Пример #4
0
        public void ToEnumerable_Normal_Sync_Fused_Offline()
        {
            var up = new UnicastProcessor <int>();

            List <int> expected = new List <int>();

            for (int i = 1; i <= 5; i++)
            {
                expected.Add(i);
                up.OnNext(i);
            }
            up.OnComplete();

            var ie = up.ToEnumerable();

            List <int> list = new List <int>();

            foreach (var i in ie)
            {
                list.Add(i);
            }

            Assert.AreEqual(5, list.Count);
            Assert.AreEqual(expected, list);
        }
        public void Take_Exact_Number_Async_Fused()
        {
            var up = new UnicastProcessor <int>();

            up.OnNext(1, 2, 3, 4, 5);

            up.Take(5).Test(fusionMode: FuseableHelper.ASYNC)
            .AssertResult(1, 2, 3, 4, 5);
        }
Пример #6
0
        public void UnicastProcessor_Conditional_Offline()
        {
            var up = new UnicastProcessor <int>();

            up.OnNext(1, 2, 3, 4, 5, 6);
            up.OnComplete();

            var ts = up.Filter(v => true).Test();

            ts.AssertResult(1, 2, 3, 4, 5, 6);
        }
Пример #7
0
 internal WindowBoundarySubscriber(IFlowableSubscriber <IFlowable <T> > actual, int bufferSize)
 {
     this.actual     = actual;
     this.active     = 1;
     this.queue      = new SpscLinkedArrayQueue <IFlowable <T> >(16);
     this.terminate  = OnTerminate;
     this.bufferSize = bufferSize;
     this.buffer     = new UnicastProcessor <T>(bufferSize, terminate);
     this.boundary   = new BoundarySubscriber(this);
     queue.Offer(buffer);
 }
Пример #8
0
            public void OnComplete()
            {
                var w = window;

                window = null;
                w?.OnComplete();
                if (Volatile.Read(ref once) == 0)
                {
                    actual.OnComplete();
                }
            }
Пример #9
0
            public void OnError(Exception cause)
            {
                var w = window;

                window = null;
                w?.OnError(cause);
                if (Volatile.Read(ref once) == 0)
                {
                    actual.OnError(cause);
                }
            }
Пример #10
0
        public void UnicastProcessor_Offline()
        {
            var up = new UnicastProcessor <int>();

            up.OnNext(1, 2, 3, 4, 5, 6);
            up.OnComplete();

            var ts = up.Test();

            ts.AssertResult(1, 2, 3, 4, 5, 6);
        }
Пример #11
0
        void StartWindow()
        {
            Interlocked.Increment(ref wip);

            IProcessor <T, T> w = new UnicastProcessor <T>(bufferSize, this.InnerDone);

            lock (this)
            {
                window = w;
            }

            Emit(w);
        }
            public void OnNext(T element)
            {
                var q   = queue;
                int idx = index;

                bool drain = false;

                if (idx == 0)
                {
                    int a = Volatile.Read(ref active);
                    if (a != 0 && Interlocked.CompareExchange(ref active, a + 1, a) == a)
                    {
                        var up = new UnicastProcessor <T>(size, OnTerminate);
                        q.Offer(up);

                        windows.Offer(up);
                        drain = true;
                    }
                }

                q.ForEach(element, CallNext);

                int c = count + 1;

                if (c == size)
                {
                    count = c - skip;

                    q.Poll(out var item);

                    item.OnComplete();
                }
                else
                {
                    count = c;
                }

                if (++idx == skip)
                {
                    index = 0;
                }
                else
                {
                    index = idx;
                }

                if (drain)
                {
                    Drain();
                }
            }
        public void Backpressure()
        {
            var pp = new UnicastProcessor <int>();

            var ts1 = pp.Test(20);

            pp.OnNext(1);
            pp.OnNext(2);

            ts1.AssertValues(1, 2);

            pp.OnNext(3);

            ts1.AssertValues(1, 2, 3);
        }
Пример #14
0
            public void OnComplete()
            {
                boundary.Cancel();
                UnicastProcessor <T> b;

                lock (this)
                {
                    b = buffer;
                    if (b == null)
                    {
                        return;
                    }
                    buffer = null;
                }
                b.OnComplete();
                Volatile.Write(ref done, true);
                Drain();
            }
Пример #15
0
            void BoundaryComplete()
            {
                SubscriptionHelper.Cancel(ref upstream);
                UnicastProcessor <T> b;

                lock (this)
                {
                    b = buffer;
                    if (b == null)
                    {
                        return;
                    }
                    buffer = null;
                }
                b.OnComplete();
                Volatile.Write(ref done, true);
                Drain();
            }
Пример #16
0
        void StartWindow()
        {
            UnicastProcessor <T> up = new UnicastProcessor <T>(bufferSize, this.InnerDone);

            lock (this)
            {
                var queue = q;
                if (queue == null)
                {
                    return;
                }
                Interlocked.Increment(ref wip);

                queue.Offer(up);
            }

            Emit(up);
        }
        public override IPublisher <int> CreatePublisher(long elements)
        {
            var pp = new UnicastProcessor <int>();

            Task.Factory.StartNew(() => {
                while (!pp.HasSubscribers)
                {
                    Thread.Sleep(1);
                }
                for (int i = 0; i < elements; i++)
                {
                    pp.OnNext(i);
                }
                pp.OnComplete();
            }, TaskCreationOptions.LongRunning);

            return(pp);
        }
Пример #18
0
        public void UnicastProcessor_Online_Fused_Scheduled()
        {
            for (int i = 0; i < 10000; i++)
            {
                var up = new UnicastProcessor <int>();

                Task.Run(() =>
                {
                    up.OnNext(1, 2, 3, 4, 5, 6);
                    up.OnComplete();
                });

                var ts = up.Test(fusionMode: FuseableHelper.ANY);

                ts
                .AwaitTerminalEvent(TimeSpan.FromSeconds(5))
                .AssertResult(1, 2, 3, 4, 5, 6);
            }
        }
Пример #19
0
            public void OnError(Exception cause)
            {
                boundary.Cancel();
                UnicastProcessor <T> b;

                lock (this)
                {
                    b = buffer;
                    if (b == null)
                    {
                        return;
                    }
                    buffer = null;
                    error  = cause;
                }
                b.OnError(cause);
                Volatile.Write(ref done, true);
                Drain();
            }
Пример #20
0
            void BoundaryError(Exception cause)
            {
                SubscriptionHelper.Cancel(ref upstream);
                UnicastProcessor <T> b;

                lock (this)
                {
                    b = buffer;
                    if (b == null)
                    {
                        return;
                    }
                    buffer = null;
                    error  = cause;
                }
                b.OnError(cause);
                Volatile.Write(ref done, true);
                Drain();
            }
Пример #21
0
        public void UnicastProcessor_Online_Hidden_Scheduled()
        {
            for (int i = 0; i < 10000; i++)
            {
                var up = new UnicastProcessor <int>();

                Task.Run(() =>
                {
                    up.OnNext(1, 2, 3, 4, 5, 6);
                    up.OnComplete();
                });

                var ts = up.Hide().Test();

                ts
                .AwaitTerminalEvent(TimeSpan.FromSeconds(5))
                .AssertResult(1, 2, 3, 4, 5, 6);
            }
        }
Пример #22
0
        public void OnNext(T t)
        {
            int i = index;

            if (i == 0 && !Volatile.Read(ref cancelled))
            {
                Interlocked.Increment(ref wip);

                var b = new UnicastProcessor <T>(size, this.InnerDone);

                q.Offer(b);

                actual.OnNext(b);
            }
            i++;
            if (i == skip)
            {
                index = 0;
            }
            else
            {
                index = i;
            }

            q.ForEach(b => b.OnNext(t));

            int p = produced + 1;

            if (p == size)
            {
                IProcessor <T, T> b;
                q.Poll(out b);

                b.OnComplete();

                produced = size - skip;
            }
            else
            {
                produced = p;
            }
        }
        public void Async()
        {
            var pp = new UnicastProcessor <int>();

            Task.Factory.StartNew(() =>
            {
                while (!pp.HasSubscribers)
                {
                    Thread.Sleep(10);
                }
                for (int i = 0; i < 5; i++)
                {
                    pp.OnNext(i);
                }
                pp.OnComplete();
            }, TaskCreationOptions.LongRunning);

            pp.Test().AwaitDone(TimeSpan.FromSeconds(5))
            .AssertResult(0, 1, 2, 3, 4);
        }
        public void Normal()
        {
            var dp = new UnicastProcessor <int>();

            Assert.IsFalse(dp.HasSubscribers);

            var ts = dp.Test();

            Assert.IsTrue(dp.HasSubscribers);

            dp.OnNext(1);
            dp.OnNext(2);
            dp.OnNext(3);
            dp.OnNext(4);

            ts.AssertValues(1, 2, 3, 4);

            dp.OnComplete();

            ts.AssertResult(1, 2, 3, 4);
        }
Пример #25
0
            void BoundaryNext()
            {
                int a = Volatile.Read(ref active);

                if (a != 0 && Interlocked.CompareExchange(ref active, a + 1, a) == a)
                {
                    var u = new UnicastProcessor <T>(bufferSize, terminate);
                    UnicastProcessor <T> b;
                    lock (this)
                    {
                        b = buffer;
                        if (b == null)
                        {
                            return;
                        }
                        buffer = u;
                        queue.Offer(u);
                    }
                    b.OnComplete();
                    Drain();
                }
            }
Пример #26
0
        public void UnicastProcessor_Online_Fused_Scheduled_5()
        {
            for (int i = 0; i < 10000; i++)
            {
                var up = new UnicastProcessor <int>();

                var ts = new TestSubscriber <int>(fusionMode: FuseableHelper.ANY);

                int[] wait = { 2 };

                Task.Run(() =>
                {
                    up.OnNext(1, 2);
                    up.OnNext(3, 4);
                    up.OnNext(5, 6);

                    Interlocked.Decrement(ref wait[0]);
                    while (Volatile.Read(ref wait[0]) != 0)
                    {
                        ;
                    }

                    up.OnComplete();
                });

                Interlocked.Decrement(ref wait[0]);
                while (Volatile.Read(ref wait[0]) != 0)
                {
                    ;
                }

                up.Subscribe(ts);

                ts
                .AwaitTerminalEvent(TimeSpan.FromSeconds(5))
                .AssertResult(1, 2, 3, 4, 5, 6);
            }
        }