Esempio n. 1
0
        public virtual void Test005()
        {
            var  settings    = Settings;
            long currentTime = DateTime.Now.Ticks;

            settings.GetTimestamp = () => currentTime;

            var algo = new FloodgateOrchestrator <int>(settings);

            FloodgateResponse res;
            int unq1 = 123456;
            var expectedSkippedCount = 0;

            for (int j = 0; j < 50; j++)
            {
                currentTime += settings.TimeframeTicks;

                for (int i = 0; i < 1; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }
            }


            currentTime += settings.TimeframeTicks;
            for (int i = 0; i < settings.SpilloverThreshold + 1 - settings.WindowSize; i++)
            {
                res = algo.NextEvent(unq1);

                Assert.AreEqual(expectedSkippedCount = 0, res.NumDisallowed, "NumDisallowed at " + i);
                Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at " + i);
            }

            res = algo.NextEvent(unq1);
            Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
            Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");

            for (int j = 0; j < IterationLogarithm(1) - 1; j++)
            {
                currentTime         += settings.TimeframeTicks;
                expectedSkippedCount = 1;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }

            for (int j = 0; j < IterationLogarithm(2); j++)
            {
                currentTime         += settings.TimeframeTicks;
                expectedSkippedCount = 1;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold / 2; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }


            for (int j = 0; j < IterationLogarithm(3); j++)
            {
                currentTime         += settings.TimeframeTicks;
                expectedSkippedCount = 1;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold / 3; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }

            // we will continue sending one message for each bucket indefinitley
            for (int j = 0; j < IterationLogarithm(4); j++)
            {
                currentTime         += settings.TimeframeTicks;
                expectedSkippedCount = 1;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold / 4; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }

            // we will continue sending one message for each bucket indefinitley
            for (int j = 0; j < 256; j++)
            {
                currentTime         += settings.TimeframeTicks;
                expectedSkippedCount = 1;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold / 8; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }

            // we will continue sending one message for each bucket indefinitley
            for (int j = 0; j < 8; j++)
            {
                currentTime += settings.TimeframeTicks;

                for (int i = 0; i < settings.TimeframeSpilloverThreshold / 8; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }
            }
        }
Esempio n. 2
0
        public virtual void Test003()
        {
            var  settings    = Settings;
            long currentTime = DateTime.Now.Ticks;

            settings.GetTimestamp = () => currentTime;

            var algo = new FloodgateOrchestrator <int>(settings);

            FloodgateResponse res;
            int unq1 = 123456;
            var expectedSkippedCount = 0;

            for (int i = 0; i < settings.SpilloverThreshold; i++)
            {
                res = algo.NextEvent(unq1);

                Assert.AreEqual(expectedSkippedCount = 0, res.NumDisallowed, "NumDisallowed at " + i);
                Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at " + i);
            }

            res = algo.NextEvent(unq1);
            Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
            Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");

            for (int j = 0; j < IterationLogarithm(1) - 1; j++)
            {
                currentTime         += settings.TimeframeTicks;
                expectedSkippedCount = 1;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }

            for (int j = 0; j < IterationLogarithm(2) - 1; j++)
            {
                currentTime         += settings.TimeframeTicks;
                expectedSkippedCount = 1;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold / 2; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }


            currentTime += settings.TimeframeTicks * 4;

            // The mathematical explanation for how the spillover threshold
            // is changing elludes me, but it is a predictable amount somewhat
            // related to how much spillover has taken place prior
            // to the break in activity
            for (int j = 0; j < 2; j++)
            {
                currentTime         += settings.TimeframeTicks;
                expectedSkippedCount = 1;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold / 2; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }

            // The mathematical explanation for how the spillover threshold
            // is changing elludes me, but it is a predictable amount somewhat
            // related to how much spillover has taken place prior
            // to the break in activity
            for (int j = 0; j < IterationLogarithm(1) - 42; j++)
            {
                currentTime         += settings.TimeframeTicks;
                expectedSkippedCount = 1;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }

            // at this point the chaos after the break in activity has normalized
            // and we are back to business as usual
            for (int j = 0; j < IterationLogarithm(2); j++)
            {
                currentTime         += settings.TimeframeTicks;
                expectedSkippedCount = 1;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold / 2; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }

            for (int j = 0; j < IterationLogarithm(3) + IterationLogarithm(4); j++)
            {
                currentTime         += settings.TimeframeTicks;
                expectedSkippedCount = 1;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold / 4; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }

            for (int j = 0; j < 256; j++)
            {
                currentTime += settings.TimeframeTicks;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold / 8; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }
        }
Esempio n. 3
0
        public virtual void Test002()
        {
            var  settings    = Settings;
            long currentTime = DateTime.Now.Ticks;

            settings.GetTimestamp = () => currentTime;

            var algo = new FloodgateOrchestrator <int>(settings);

            FloodgateResponse res;
            int unq1 = 123456;
            var expectedSkippedCount = 0;

            for (int i = 0; i < settings.SpilloverThreshold; i++)
            {
                res = algo.NextEvent(unq1);

                Assert.AreEqual(expectedSkippedCount = 0, res.NumDisallowed, "NumDisallowed at " + i);
                Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at " + i);
            }

            res = algo.NextEvent(unq1);
            Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
            Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");

            for (int j = 0; j < IterationLogarithm(1) - 1; j++)
            {
                currentTime         += settings.TimeframeTicks;
                expectedSkippedCount = 1;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }

            for (int j = 0; j < IterationLogarithm(2) - 1; j++)
            {
                currentTime         += settings.TimeframeTicks;
                expectedSkippedCount = 1;
                for (int i = 0; i < settings.TimeframeSpilloverThreshold / 2; i++)
                {
                    res = algo.NextEvent(unq1);

                    Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at i=" + i + " and j= " + j);
                    Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at i=" + i + " and j= " + j);
                    expectedSkippedCount = 0;
                }

                res = algo.NextEvent(unq1);
                Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
                Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
            }


            currentTime         += settings.TimeframeTicks * 4;
            expectedSkippedCount = 1;
            for (int j = 0; j < 1; j++)
            {
                currentTime += settings.TimeframeTicks;

                res = algo.NextEvent(unq1);

                Assert.AreEqual(expectedSkippedCount, res.NumDisallowed, "NumDisallowed at j= " + j);
                Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at j= " + j);
                expectedSkippedCount = 0;
            }

            // we already sent one, and then we need to sent threshold -1  more to hit the threshold

            currentTime += settings.TimeframeTicks;
            for (int i = 0; i < settings.SpilloverThreshold - 1; i++)
            {
                res = algo.NextEvent(unq1);

                Assert.AreEqual(expectedSkippedCount = 0, res.NumDisallowed, "NumDisallowed at " + i);
                Assert.AreEqual(true, res.ShouldAllow, "ShouldAllow at " + i);
            }

            res = algo.NextEvent(unq1);
            Assert.AreEqual(expectedSkippedCount = 1, res.NumDisallowed, "Num skipped after hit threshold");
            Assert.AreEqual(false, res.ShouldAllow, "ShouldAllow after hit threshold");
        }