Пример #1
0
        public void StopsWhenTimestampIsOutOfRange()
        {
            // Sampling rate should be small to enough to fail quickly
            var clock = new SamplesCounterClock(0.000001, SamplesCounterClockOptions.Default);

            while (true)
            {
                clock.Increase(UInt32.MaxValue);
            }
        }
Пример #2
0
        public void AutoInitializesReferenceAndCount()
        {
            const int samplingRate = 10;
            var       time         = DateTimeOffset.UtcNow;

            var clock = new SamplesCounterClock(samplingRate, SamplesCounterClockOptions.Default);

            clock.Increase(samplingRate); // More or less one second after "time"

            Assert.IsTrue(clock.Current > time);
        }
Пример #3
0
        public void CountsWithGivenReference()
        {
            const int samplingRate = 128;
            var       time         = DateTimeOffset.UtcNow;

            var clock = new SamplesCounterClock(samplingRate, SamplesCounterClockOptions.Default);

            clock.Reference = time;
            clock.Increase(samplingRate);

            Assert.AreEqual(time.AddSeconds(1), clock.Current);
        }
Пример #4
0
        protected override void SetupCore()
        {
            base.SetupCore();

            double samplingRate = Channels.First().SamplingRate;

            _timestampGenerator = new SamplesCounterClock(samplingRate, SamplesCounterClockOptions.Default);
            _generators         = Channels.Select(x => new Generator(x.Id, x.Range)).ToArray();

            // This is an example, a true device driver should not generate one sample
            // for each tick (better to pack them in bigger DataPacket) and also interval measurement
            // should be more precise. Expect floating point errors (and 1 ms rounding!) if you calculate
            // timer interval in this way.
            TimeSpan timerInterval = TimeSpan.FromSeconds(1 / samplingRate);

            _dataTimer = new System.Threading.Timer(OnDataTimerTick, null, TimeSpan.Zero, timerInterval);
        }
Пример #5
0
        public void CanAutoResetOnOverflow()
        {
            // Very high sampling frequency otherwise we will exhaust DateTime range before we reach MaximumNumberOfSamples
            var clock = new SamplesCounterClock(1000000,
                                                SamplesCounterClockOptions.AdjustForOverflow | SamplesCounterClockOptions.ForceMonotonic);

            // Force creations of reference time
            var time = clock.Current;

            // Enqueue enough samples to cause an overflow
            DateTimeOffset latestPacketTime = DateTimeOffset.MinValue;

            for (long i = 0; i <= (SamplesCounterClock.MaximumNumberOfSamples + 1) / UInt32.MaxValue; ++i)
            {
                var thisPacketTime = clock.Increase(UInt32.MaxValue);
                Assert.IsTrue(thisPacketTime > latestPacketTime);

                latestPacketTime = thisPacketTime;
            }

            // Now reference time has been "regenerated"
            Assert.AreNotEqual(time, clock.Reference.Value);
        }