Example #1
0
        public void ThrottledTimer()
        {
            // Create a lossy policy that throttles and drops messages once the maximum queue size (1) is reached
            var lossyThrottlePolicy = DeliveryPolicy.QueueSizeConstrained(1, true);

            int countA = 0, countB = 0, countC = 0;

            using (var p = Pipeline.Create())
            {
                Timers.Timer(p, TimeSpan.FromMilliseconds(1), (dt, ts) => countA++)
                .Do(_ => countB++, lossyThrottlePolicy)
                .Do(
                    _ =>
                {
                    Thread.Sleep(5);
                    countC++;
                }, lossyThrottlePolicy);

                p.RunAsync();
                p.WaitAll(100);
            }

            // Timer continues to post so messages will be dropped at receiver B until C stops throttling it
            Assert.IsTrue(countA > 0);
            Assert.IsTrue(countA > countB);
            Assert.AreEqual(countB, countC);
        }
Example #2
0
        public void DeliveryPolicyWithGuarantees()
        {
            var latest = new List <int>();
            var latestWithGuarantees        = new List <int>();
            var latestWithGuaranteesChained = new List <int>();
            var queueSizeTwoWithGuarantees  = new List <int>();

            // create a pipeline consisting of three chained components A -> B -> C
            using (var p = Pipeline.Create())
            {
                var generator = Generators.Range(p, 0, 10, TimeSpan.FromMilliseconds(1));
                generator
                .Do(_ => { Thread.Sleep(50); }, DeliveryPolicy.LatestMessage)
                .Do(m => latest.Add(m));

                generator
                .Do(_ => { Thread.Sleep(50); }, DeliveryPolicy.LatestMessage.WithGuarantees <int>(i => i % 2 == 0))
                .Do(m => latestWithGuarantees.Add(m));

                generator
                .Do(_ => { Thread.Sleep(50); }, DeliveryPolicy.LatestMessage.WithGuarantees <int>(i => i % 2 == 0).WithGuarantees(i => i % 3 == 0))
                .Do(m => latestWithGuaranteesChained.Add(m));

                generator
                .Do(_ => { Thread.Sleep(50); }, DeliveryPolicy.QueueSizeConstrained(2).WithGuarantees <int>(i => i % 2 == 0))
                .Do(m => queueSizeTwoWithGuarantees.Add(m));

                p.Run();
            }

            // with latest we only get the first message since the others are dropped, and the
            // last message.
            CollectionAssert.AreEqual(latest, new List <int>()
            {
                0, 9
            });

            // with guarantees we get all the even messages.
            CollectionAssert.AreEqual(latestWithGuarantees, new List <int>()
            {
                0, 2, 4, 6, 8
            });

            // with guarantees we get all the even and multiple of 3 messages.
            CollectionAssert.AreEqual(latestWithGuaranteesChained, new List <int>()
            {
                0, 2, 3, 4, 6, 8, 9
            });

            // with guarantees, even when queue size is 2, we get all the even messages.
            CollectionAssert.AreEqual(queueSizeTwoWithGuarantees, new List <int>()
            {
                0, 2, 4, 6, 8
            });
        }
Example #3
0
        public void DeliveryPolicyWithGuarantees()
        {
            var latest = new List <int>();
            var latestWithGuarantees        = new List <int>();
            var latestWithGuaranteesChained = new List <int>();
            var queueSizeTwoWithGuarantees  = new List <int>();

            // create a pipeline consisting of three chained components A -> B -> C
            using (var p = Pipeline.Create())
            {
                var generator = Generators.Range(p, 0, 10, TimeSpan.FromMilliseconds(1));
                generator
                .Do(_ => { Thread.Sleep(50); }, DeliveryPolicy.LatestMessage)
                .Do(m => latest.Add(m));

                generator
                .Do(_ => { Thread.Sleep(50); }, DeliveryPolicy.LatestMessage.WithGuarantees <int>(i => i % 2 == 0))
                .Do(m => latestWithGuarantees.Add(m));

                generator
                .Do(_ => { Thread.Sleep(50); }, DeliveryPolicy.LatestMessage.WithGuarantees <int>(i => i % 2 == 0).WithGuarantees(i => i % 3 == 0))
                .Do(m => latestWithGuaranteesChained.Add(m));

                generator
                .Do(_ => { Thread.Sleep(50); }, DeliveryPolicy.QueueSizeConstrained(2).WithGuarantees <int>(i => i % 2 == 0))
                .Do(m => queueSizeTwoWithGuarantees.Add(m));

                p.Run();
            }

            // with latest we may drop some messages except the last message.
            CollectionAssert.IsSubsetOf(new List <int>()
            {
                9
            }, latest);
            CollectionAssert.AllItemsAreUnique(latest);                         // ensure uniqueness
            CollectionAssert.AreEqual(latest.OrderBy(x => x).ToList(), latest); // ensure order

            // with guarantees we get all the even messages.
            CollectionAssert.IsSubsetOf(new List <int>()
            {
                0, 2, 4, 6, 8
            }, latestWithGuarantees);
            CollectionAssert.AllItemsAreUnique(latestWithGuarantees);
            CollectionAssert.AreEqual(latestWithGuarantees.OrderBy(x => x).ToList(), latestWithGuarantees);

            // with guarantees we get all the even and multiple of 3 messages.
            CollectionAssert.IsSubsetOf(new List <int>()
            {
                0, 2, 3, 4, 6, 8, 9
            }, latestWithGuaranteesChained);
            CollectionAssert.AllItemsAreUnique(latestWithGuaranteesChained);
            CollectionAssert.AreEqual(latestWithGuaranteesChained.OrderBy(x => x).ToList(), latestWithGuaranteesChained);

            // with guarantees, even when queue size is 2, we get all the even messages.
            CollectionAssert.IsSubsetOf(new List <int>()
            {
                0, 2, 4, 6, 8
            }, queueSizeTwoWithGuarantees);
            CollectionAssert.AllItemsAreUnique(queueSizeTwoWithGuarantees);
            CollectionAssert.AreEqual(queueSizeTwoWithGuarantees.OrderBy(x => x).ToList(), queueSizeTwoWithGuarantees);
        }