コード例 #1
0
        public void ObservableProgressorWithPause()
        {
            var source     = new SlowObservable <int>(new[] { 0, 1, 2, 3, 4, 5 });
            var progressor = Progressor <int> .CreateFromIObservable(source);

            Assert.IsFalse(progressor.IsClosed);
            var data = new[] { 0, 0, 0 };

            using
            (
                progressor.SubscribeAction
                (
                    value =>
            {
                if (value != Volatile.Read(ref data[0]))
                {
                    Volatile.Write(ref data[2], 1);
                }

                Interlocked.Increment(ref data[0]);
            }
                )
            )
            {
#if TARGETS_NET
                Thread.MemoryBarrier();
                Thread.MemoryBarrier();
#else
                Interlocked.MemoryBarrier();
                Interlocked.MemoryBarrier();
#endif
                source.Show();
#if TARGETS_NET
                Thread.MemoryBarrier();
#else
                Interlocked.MemoryBarrier();
#endif
                while (progressor.TryTake(out var item))
                {
                    Assert.AreEqual(0, Volatile.Read(ref data[2]));
                    Assert.AreEqual(item, Volatile.Read(ref data[1]));
                    Interlocked.Increment(ref data[1]);
                }

                if (Volatile.Read(ref data[0]) != 6 || Volatile.Read(ref data[1]) != 6)
                {
                    Debugger.Break();
                }
            }

            Assert.AreEqual(6, Volatile.Read(ref data[0]));
            Assert.AreEqual(Volatile.Read(ref data[0]), Volatile.Read(ref data[1]));
            Assert.IsTrue(progressor.IsClosed);
        }
コード例 #2
0
        public void ObservableProgressorWithPause()
        {
            var source    = new SlowObservable <int>(new[] { 0, 1, 2, 3, 4, 5 });
            var progresor = new Progressor <int>(source);

            Assert.IsFalse(progresor.IsClosed);
            int indexA = 0;
            int indexB = 0;

            progresor.SubscribeAction
            (
                value =>
            {
                Assert.AreEqual(value, indexB);
                indexB++;
            }
            );
            int item;

            while (progresor.TryTake(out item))
            {
                Assert.AreEqual(item, indexA);
                indexA++;
            }
            Assert.AreEqual(0, indexA);
            Assert.AreEqual(indexA, indexB);
            Assert.IsFalse(progresor.IsClosed);
            source.Show();
            while (indexA < 6)
            {
                while (progresor.TryTake(out item))
                {
                    Assert.AreEqual(item, indexA);
                    indexA++;
                }
            }
            Assert.AreEqual(indexA, indexB);
            Assert.IsFalse(progresor.IsClosed);
            source.Close();
            Assert.IsFalse(progresor.IsClosed);
            Assert.IsFalse(progresor.TryTake(out item));
            Assert.IsTrue(progresor.IsClosed);
        }