Example #1
0
        public void TestDoubleTickerWithStop()
        {
            // arrange
            var start = DateTime.Now;
            var ticker = new Ticker(TimeSpan.FromSeconds(1));

            // act
            bool ok1;
            var sentAt1 = (DateTime)ticker.C.ReceiveOk(out ok1);
            var duration1 = DateTime.Now - start;
            var offBy1 = DateTime.Now - sentAt1;

            ticker.Stop();

            var newTicker = new Ticker(TimeSpan.FromSeconds(5));
            bool? ok2 = null;
            Select
                .CaseReceiveOk(ticker.C, (d, b) => ok2 = false)
                .CaseReceive(newTicker.C, _ => ok2 = true)
                .NoDefault();

            newTicker.Stop();

            // assert
            Assert.IsTrue(ok1, "ok1");
            Assert.GreaterOrEqual(duration1, TimeSpan.FromSeconds(1), "duration1");
            Assert.Less(duration1, TimeSpan.FromSeconds(1.5), "duration1");
            Assert.Less(offBy1, TimeSpan.FromSeconds(0.5), "offBy1");

            Assert.IsNotNull(ok2, "ok2");
            Assert.IsTrue(ok2.Value, "ok2");
        }
Example #2
0
        public void TestDoubleTicker()
        {
            // arrange
            var start = DateTime.Now;
            var ticker = new Ticker(TimeSpan.FromSeconds(1));

            // act
            bool ok1;
            var sentAt1 = (DateTime)ticker.C.ReceiveOk(out ok1);
            var duration1 = DateTime.Now - start;
            var offBy1 = DateTime.Now - sentAt1;

            bool ok2;
            var sentAt2 = (DateTime)ticker.C.ReceiveOk(out ok2);
            var duration2 = DateTime.Now - start;
            var offBy2 = DateTime.Now - sentAt2;

            ticker.Stop();

            // assert
            Assert.IsTrue(ok1, "ok1");
            Assert.GreaterOrEqual(duration1, TimeSpan.FromSeconds(1), "duration1");
            Assert.Less(duration1, TimeSpan.FromSeconds(1.5), "duration1");
            Assert.Less(offBy1, TimeSpan.FromSeconds(0.5), "offBy1");

            Assert.IsTrue(ok2, "ok2");
            Assert.GreaterOrEqual(duration2, TimeSpan.FromSeconds(2), "duration2");
            Assert.Less(duration2, TimeSpan.FromSeconds(2.5), "duration2");
            Assert.Less(offBy2, TimeSpan.FromSeconds(0.5), "offBy2");
        }
Example #3
0
        private void rdyLoop()
        {
            var redistributeTicker = new Ticker(_config.RDYRedistributeInterval);

            bool doLoop = true;
            using (var select =
                    Select
                        .CaseReceive(redistributeTicker.C, o => redistributeRDY())
                        .CaseReceive(_exitChan, o => doLoop = false)
                        .NoDefault(defer: true))
            {
                // ReSharper disable once LoopVariableIsNeverChangedInsideLoop
                while (doLoop)
                {
                    select.Execute();
                }
            }

            redistributeTicker.Stop();
            log(LogLevel.Info, "rdyLoop exiting");
            _wg.Done();
        }
Example #4
0
        /// <summary>
        /// poll all known lookup servers every LookupdPollInterval
        /// </summary>
        private void lookupdLoop()
        {
            // add some jitter so that multiple consumers discovering the same topic,
            // when restarted at the same time, dont all connect at once.
            var jitter = new TimeSpan((long)(_rng.Float64() * _config.LookupdPollJitter * _config.LookupdPollInterval.Ticks));

            bool doLoop = true;

            Select
                .CaseReceive(Time.After(jitter))
                .CaseReceive(_exitChan, o => doLoop = false)
                .NoDefault();

            var ticker = new Ticker(_config.LookupdPollInterval);

            using (var select =
                    Select
                        .CaseReceive(ticker.C, o => queryLookupd())
                        .CaseReceive(_lookupdRecheckChan, o => queryLookupd())
                        .CaseReceive(_exitChan, o => doLoop = false)
                        .NoDefault(defer: true))
            {
                // ReSharper disable once LoopVariableIsNeverChangedInsideLoop
                while (doLoop)
                {
                    select.Execute();
                }
            }

            ticker.Stop();
            log(LogLevel.Info, "exiting lookupdLoop");
            _wg.Done();
        }
Example #5
0
        public void TestSingleTicker()
        {
            // arrange
            var start = DateTime.Now;
            var ticker = new Ticker(TimeSpan.FromSeconds(1));

            // act
            bool ok;
            var sentAt = (DateTime)ticker.C.ReceiveOk(out ok);
            var duration = DateTime.Now - start;
            var offBy = DateTime.Now - sentAt;

            ticker.Stop();

            // assert
            Assert.IsTrue(ok, "ok");
            Assert.GreaterOrEqual(duration, TimeSpan.FromSeconds(1), "duration");
            Assert.Less(duration, TimeSpan.FromSeconds(1.5), "duration");
            Assert.Less(offBy, TimeSpan.FromSeconds(0.5), "offBy");
        }
Example #6
0
        public void TestTickerLoopWithNemesisChan()
        {
            var start = DateTime.Now;
            var ticker = new Ticker(TimeSpan.FromSeconds(1));

            var listOfTimes = new List<TimeSpan>();
            var exitChan = new Chan<bool>();
            var lookupdRecheckChan = new Chan<bool>();
            bool doLoop = true;
            using (var select =
                    Select
                        .CaseReceive(ticker.C,
                                     o =>
                                     {
                                         Console.WriteLine("Received tick");
                                         listOfTimes.Add(DateTime.Now - start);

                                         if (listOfTimes.Count == 5)
                                         {
                                             GoFunc.Run(() => lookupdRecheckChan.Send(true), "lookupd recheck sender");
                                         }
                                     })
                        .CaseReceive(lookupdRecheckChan,
                                     o =>
                                     {
                                         Console.WriteLine("Nemesis");
                                         Thread.Sleep(5000);
                                     })
                        .CaseReceive(exitChan, o => doLoop = false)
                        .NoDefault(defer: true))
            {
                // ReSharper disable once LoopVariableIsNeverChangedInsideLoop
                while (doLoop)
                {
                    select.Execute();
                    if (listOfTimes.Count >= 10)
                    {
                        GoFunc.Run(() => exitChan.Send(true), "exit notifier");
                    }
                }
            }

            ticker.Stop();

            var duration = DateTime.Now - start;
            Console.WriteLine(duration);
            Assert.AreEqual(10, listOfTimes.Count, "listOfTimes.Count");
            Assert.GreaterOrEqual(duration, TimeSpan.FromSeconds(14), "duration");
            Assert.Less(duration, TimeSpan.FromSeconds(17));
        }