view Implab.Test/RunnableComponentTests.cs @ 259:7d52dc684bbd v3

PollingComponent: implemented correct stopping
author cin
date Fri, 13 Apr 2018 03:57:39 +0300
parents
children 547a2fc0d93e
line wrap: on
line source

using System;
using System.Threading;
using System.Threading.Tasks;
using Implab.Components;
using Xunit;

namespace Implab.Test
{
    class TimeLog : PollingComponent {
        public TimeLog() : base(true) {
        }

        protected override Task Poll(CancellationToken ct) {
            Console.WriteLine("Poll");
            return Task.CompletedTask;
        }
    }

    public class UnitTest1
    {
        [Fact]
        public async Task Test1()
        {

            using(var  tl = new TimeLog()) {
                tl.StateChanged += (self, args) => Console.WriteLine("{0}", args.State);
                tl.Delay = 1000;
                tl.Interval = 500;

                
                tl.Start(CancellationToken.None);
                await tl.Completion;

                await Task.Delay(2000);

                tl.Stop(CancellationToken.None);
                await tl.Completion;
                await Task.Delay(3000);
            }
        }
    }
}