annotate Implab.Test/RunnableComponentTests.cs @ 203:4d9830a9bbb8 v2

Added 'Fail' method to RunnableComponent which allows component to move from Running to Failed state. Added PollingComponent a timer based runnable component More tests Added FailPromise a thin class to wrap exceptions Fixed error handling in SuccessPromise classes.
author cin
date Tue, 18 Oct 2016 17:49:54 +0300
parents d45bdf510514
children 8200ab154c8a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
1 using System;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
2 using System.Reflection;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
3 using System.Threading;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
4 using Implab.Parallels;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
5 using Implab.Components;
203
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 194
diff changeset
6 using Implab.Test.Mock;
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
7
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
8 #if MONO
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
9
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
10 using NUnit.Framework;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
11 using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
12 using TestMethodAttribute = NUnit.Framework.TestAttribute;
188
3071220371f8 Updated VS project
cin
parents: 185
diff changeset
13 using AssertFailedException = NUnit.Framework.AssertionException;
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
14 #else
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
15
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
16 using Microsoft.VisualStudio.TestTools.UnitTesting;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
17
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
18 #endif
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
19
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
20 namespace Implab.Test {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
21 [TestClass]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
22 public class RunnableComponentTests {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
23
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
24 static void ShouldThrow(Action action) {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
25 try {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
26 action();
188
3071220371f8 Updated VS project
cin
parents: 185
diff changeset
27 Assert.Fail();
3071220371f8 Updated VS project
cin
parents: 185
diff changeset
28 } catch (AssertFailedException) {
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
29 throw;
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
30 } catch {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
31 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
32 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
33
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
34
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
35
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
36 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
37 public void NormalFlowTest() {
203
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 194
diff changeset
38 var comp = new MockRunnableComponent(false);
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
39
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
40 Assert.AreEqual(ExecutionState.Created, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
41
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
42 comp.Init();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
43
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
44 Assert.AreEqual(ExecutionState.Ready, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
45
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
46 comp.Start().Join(1000);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
47
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
48 Assert.AreEqual(ExecutionState.Running, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
49
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
50 comp.Stop().Join(1000);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
51
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
52 Assert.AreEqual(ExecutionState.Disposed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
53
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
54 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
55
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
56 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
57 public void InitFailTest() {
203
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 194
diff changeset
58 var comp = new MockRunnableComponent(false) {
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
59 MockInit = () => {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
60 throw new Exception("BAD");
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
61 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
62 };
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
63
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
64 ShouldThrow(() => comp.Start());
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
65 ShouldThrow(() => comp.Stop());
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
66 Assert.AreEqual(ExecutionState.Created, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
67
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
68 ShouldThrow(comp.Init);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
69
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
70 Assert.AreEqual(ExecutionState.Failed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
71
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
72 ShouldThrow(() => comp.Start());
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
73 ShouldThrow(() => comp.Stop());
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
74 Assert.AreEqual(ExecutionState.Failed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
75
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
76 comp.Dispose();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
77 Assert.AreEqual(ExecutionState.Disposed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
78 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
79
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
80 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
81 public void DisposedTest() {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
82
203
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 194
diff changeset
83 var comp = new MockRunnableComponent(false);
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
84 comp.Dispose();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
85
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
86 ShouldThrow(() => comp.Start());
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
87 ShouldThrow(() => comp.Stop());
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
88 ShouldThrow(comp.Init);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
89
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
90 Assert.AreEqual(ExecutionState.Disposed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
91 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
92
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
93 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
94 public void StartCancelTest() {
203
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 194
diff changeset
95 var comp = new MockRunnableComponent(true) {
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
96 MockStart = () => PromiseHelper.Sleep(100000, 0)
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
97 };
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
98
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
99 var p = comp.Start();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
100 Assert.AreEqual(ExecutionState.Starting, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
101 p.Cancel();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
102 ShouldThrow(() => p.Join(1000));
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
103 Assert.AreEqual(ExecutionState.Failed, comp.State);
188
3071220371f8 Updated VS project
cin
parents: 185
diff changeset
104
194
d45bdf510514 working on diagnostics
cin
parents: 188
diff changeset
105 Assert.IsTrue(comp.LastError is OperationCanceledException);
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
106
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
107 comp.Dispose();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
108 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
109
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
110 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
111 public void StartStopTest() {
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
112 var stop = new Signal();
203
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 194
diff changeset
113 var comp = new MockRunnableComponent(true) {
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
114 MockStart = () => PromiseHelper.Sleep(100000, 0),
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
115 MockStop = () => AsyncPool.RunThread(stop.Wait)
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
116 };
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
117
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
118 var p1 = comp.Start();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
119 var p2 = comp.Stop();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
120 // should enter stopping state
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
121
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
122 ShouldThrow(p1.Join);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
123 Assert.IsTrue(p1.IsCancelled);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
124 Assert.AreEqual(ExecutionState.Stopping, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
125
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
126 stop.Set();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
127 p2.Join(1000);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
128 Assert.AreEqual(ExecutionState.Disposed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
129 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
130
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
131 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
132 public void StartStopFailTest() {
203
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 194
diff changeset
133 var comp = new MockRunnableComponent(true) {
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
134 MockStart = () => PromiseHelper.Sleep(100000, 0).Then(null,null,x => { throw new Exception("I'm dead"); })
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
135 };
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
136
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
137 comp.Start();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
138 var p = comp.Stop();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
139 // if Start fails to cancel, should fail to stop
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
140 ShouldThrow(() => p.Join(1000));
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
141 Assert.AreEqual(ExecutionState.Failed, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
142 Assert.IsNotNull(comp.LastError);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
143 Assert.AreEqual("I'm dead", comp.LastError.Message);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
144 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
145
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
146 [TestMethod]
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
147 public void StopCancelTest() {
203
4d9830a9bbb8 Added 'Fail' method to RunnableComponent which allows component to move from
cin
parents: 194
diff changeset
148 var comp = new MockRunnableComponent(true) {
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
149 MockStop = () => PromiseHelper.Sleep(100000, 0)
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
150 };
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
151
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
152 comp.Start();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
153 var p = comp.Stop();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
154 Assert.AreEqual(ExecutionState.Stopping, comp.State);
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
155 p.Cancel();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
156 ShouldThrow(() => p.Join(1000));
188
3071220371f8 Updated VS project
cin
parents: 185
diff changeset
157 Assert.AreEqual(ExecutionState.Failed, comp.State);
194
d45bdf510514 working on diagnostics
cin
parents: 188
diff changeset
158 Assert.IsTrue(comp.LastError is OperationCanceledException);
185
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
159
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
160 comp.Dispose();
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
161 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
162
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
163 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
164 }
822aab37b107 runnable component, work in progress
cin
parents:
diff changeset
165