Esempio n. 1
0
        public async Task KeyCollectionTest1()
        {
            int[]      snapshot = null;
            List <int> list     = new List <int>();

            using FiberKeyedCollection <int, int> collection = new FiberKeyedCollection <int, int>(x => x);
            using AutoResetEvent reset = new AutoResetEvent(false);
            using Fiber receive        = new Fiber();
            collection.Add(1);
            collection.Add(2);
            collection.Subscribe(receive,
                                 action =>
            {
                if (action.ActionType == ActionType.Add)
                {
                    list.Add(action.Items[0]);
                }
                else
                {
                    list.Remove(action.Items[0]);
                }

                reset.Set();
            },
                                 ints =>
            {
                snapshot = ints;
                reset.Set();
            });

            Assert.IsTrue(reset.WaitOne(1000));

            Assert.AreEqual(2, snapshot.Length);
            Assert.AreEqual(1, snapshot[0]);
            Assert.AreEqual(2, snapshot[1]);
            Assert.AreEqual(0, list.Count);

            collection.Add(3);
            Assert.IsTrue(reset.WaitOne(1000));

            Assert.AreEqual(1, list.Count);

            collection.Remove(3);
            Assert.IsTrue(reset.WaitOne(1000));

            Assert.AreEqual(0, list.Count);

            int[] items = await collection.GetItemsAsync(x => true);

            Assert.AreEqual(2, items.Length);
        }
Esempio n. 2
0
        public void KeyCollectionTest1()
        {
            int[] snapshot   = null;
            var   list       = new List <int>();
            var   collection = new FiberKeyedCollection <int, int>(x => x);
            var   receive    = Fiber.StartNew(FiberType.Pool);

            collection.Add(1);
            collection.Add(2);
            collection.Subscribe(receive,
                                 action =>
            {
                if (action.ActionType == ActionType.Add)
                {
                    list.Add(action.Item);
                }
                else
                {
                    list.Remove(action.Item);
                }
            },
                                 ints => snapshot = ints);
            Thread.Sleep(10);
            Assert.AreEqual(2, snapshot.Length);
            Assert.AreEqual(1, snapshot[0]);
            Assert.AreEqual(2, snapshot[1]);
            Assert.AreEqual(0, list.Count);
            collection.Add(3);
            Thread.Sleep(10);
            Assert.AreEqual(1, list.Count);
            collection.Remove(3);
            Thread.Sleep(10);
            Assert.AreEqual(0, list.Count);
            var items = collection.GetItems(x => true);

            Assert.AreEqual(2, items.Length);
        }