annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
259
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
1 using System;
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
2 using System.Threading;
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
3 using System.Threading.Tasks;
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
4 using Implab.Components;
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
5 using Xunit;
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
6
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
7 namespace Implab.Test
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
8 {
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
9 class TimeLog : PollingComponent {
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
10 public TimeLog() : base(true) {
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
11 }
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
12
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
13 protected override Task Poll(CancellationToken ct) {
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
14 Console.WriteLine("Poll");
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
15 return Task.CompletedTask;
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
16 }
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
17 }
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
18
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
19 public class UnitTest1
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
20 {
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
21 [Fact]
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
22 public async Task Test1()
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
23 {
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
24
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
25 using(var tl = new TimeLog()) {
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
26 tl.StateChanged += (self, args) => Console.WriteLine("{0}", args.State);
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
27 tl.Delay = 1000;
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
28 tl.Interval = 500;
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
29
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
30
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
31 tl.Start(CancellationToken.None);
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
32 await tl.Completion;
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
33
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
34 await Task.Delay(2000);
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
35
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
36 tl.Stop(CancellationToken.None);
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
37 await tl.Completion;
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
38 await Task.Delay(3000);
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
39 }
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
40 }
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
41 }
7d52dc684bbd PollingComponent: implemented correct stopping
cin
parents:
diff changeset
42 }