Пример #1
0
        public async Task CheckObjectThread_Heartbeat()
        {
            List <int> tickDurations = new List <int>();

            ObjectThread objThread         = new ObjectThread("TestThread_Heartbeat", 500);
            Exception    occurredException = null;

            try
            {
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                objThread.Tick += (sender, eArgs) =>
                {
                    tickDurations.Add((int)stopwatch.Elapsed.TotalMilliseconds);

                    stopwatch.Reset();
                    stopwatch.Start();
                };
                objThread.Start();

                // Wait some thime since we have some heartbeats
                await Task.Delay(2500);
            }
            catch (Exception ex) { occurredException = ex; }

            // Wait for thread finish
            await objThread.StopAsync(1000);

            // Check results
            Assert.Null(occurredException);
            Assert.True(tickDurations.Count > 4);
            Assert.True(tickDurations.Count < 10);
            Assert.True(tickDurations
                        .Count((actInt) => actInt > 450 && actInt < 550) > 4);
        }