comparison 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
comparison
equal deleted inserted replaced
202:2651cb9a4250 203:4d9830a9bbb8
1 using System; 1 using System;
2 using System.Reflection; 2 using System.Reflection;
3 using System.Threading; 3 using System.Threading;
4 using Implab.Parallels; 4 using Implab.Parallels;
5 using Implab.Components; 5 using Implab.Components;
6 using Implab.Test.Mock;
6 7
7 #if MONO 8 #if MONO
8 9
9 using NUnit.Framework; 10 using NUnit.Framework;
10 using TestClassAttribute = NUnit.Framework.TestFixtureAttribute; 11 using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
28 throw; 29 throw;
29 } catch { 30 } catch {
30 } 31 }
31 } 32 }
32 33
33 class Runnable : RunnableComponent {
34 public Runnable(bool initialized) : base(initialized) {
35 }
36
37 public Action MockInit {
38 get;
39 set;
40 }
41 34
42 public Func<IPromise> MockStart {
43 get;
44 set;
45 }
46
47 public Func<IPromise> MockStop {
48 get;
49 set;
50 }
51
52 protected override IPromise OnStart() {
53 return MockStart != null ? MockStart() : base.OnStart();
54 }
55
56 protected override IPromise OnStop() {
57 return MockStop != null ? MockStop() : base.OnStart();
58 }
59
60 protected override void OnInitialize() {
61 if (MockInit != null)
62 MockInit();
63 }
64 }
65 35
66 [TestMethod] 36 [TestMethod]
67 public void NormalFlowTest() { 37 public void NormalFlowTest() {
68 var comp = new Runnable(false); 38 var comp = new MockRunnableComponent(false);
69 39
70 Assert.AreEqual(ExecutionState.Created, comp.State); 40 Assert.AreEqual(ExecutionState.Created, comp.State);
71 41
72 comp.Init(); 42 comp.Init();
73 43
83 53
84 } 54 }
85 55
86 [TestMethod] 56 [TestMethod]
87 public void InitFailTest() { 57 public void InitFailTest() {
88 var comp = new Runnable(false) { 58 var comp = new MockRunnableComponent(false) {
89 MockInit = () => { 59 MockInit = () => {
90 throw new Exception("BAD"); 60 throw new Exception("BAD");
91 } 61 }
92 }; 62 };
93 63
108 } 78 }
109 79
110 [TestMethod] 80 [TestMethod]
111 public void DisposedTest() { 81 public void DisposedTest() {
112 82
113 var comp = new Runnable(false); 83 var comp = new MockRunnableComponent(false);
114 comp.Dispose(); 84 comp.Dispose();
115 85
116 ShouldThrow(() => comp.Start()); 86 ShouldThrow(() => comp.Start());
117 ShouldThrow(() => comp.Stop()); 87 ShouldThrow(() => comp.Stop());
118 ShouldThrow(comp.Init); 88 ShouldThrow(comp.Init);
120 Assert.AreEqual(ExecutionState.Disposed, comp.State); 90 Assert.AreEqual(ExecutionState.Disposed, comp.State);
121 } 91 }
122 92
123 [TestMethod] 93 [TestMethod]
124 public void StartCancelTest() { 94 public void StartCancelTest() {
125 var comp = new Runnable(true) { 95 var comp = new MockRunnableComponent(true) {
126 MockStart = () => PromiseHelper.Sleep(100000, 0) 96 MockStart = () => PromiseHelper.Sleep(100000, 0)
127 }; 97 };
128 98
129 var p = comp.Start(); 99 var p = comp.Start();
130 Assert.AreEqual(ExecutionState.Starting, comp.State); 100 Assert.AreEqual(ExecutionState.Starting, comp.State);
138 } 108 }
139 109
140 [TestMethod] 110 [TestMethod]
141 public void StartStopTest() { 111 public void StartStopTest() {
142 var stop = new Signal(); 112 var stop = new Signal();
143 var comp = new Runnable(true) { 113 var comp = new MockRunnableComponent(true) {
144 MockStart = () => PromiseHelper.Sleep(100000, 0), 114 MockStart = () => PromiseHelper.Sleep(100000, 0),
145 MockStop = () => AsyncPool.RunThread(stop.Wait) 115 MockStop = () => AsyncPool.RunThread(stop.Wait)
146 }; 116 };
147 117
148 var p1 = comp.Start(); 118 var p1 = comp.Start();
158 Assert.AreEqual(ExecutionState.Disposed, comp.State); 128 Assert.AreEqual(ExecutionState.Disposed, comp.State);
159 } 129 }
160 130
161 [TestMethod] 131 [TestMethod]
162 public void StartStopFailTest() { 132 public void StartStopFailTest() {
163 var comp = new Runnable(true) { 133 var comp = new MockRunnableComponent(true) {
164 MockStart = () => PromiseHelper.Sleep(100000, 0).Then(null,null,x => { throw new Exception("I'm dead"); }) 134 MockStart = () => PromiseHelper.Sleep(100000, 0).Then(null,null,x => { throw new Exception("I'm dead"); })
165 }; 135 };
166 136
167 comp.Start(); 137 comp.Start();
168 var p = comp.Stop(); 138 var p = comp.Stop();
173 Assert.AreEqual("I'm dead", comp.LastError.Message); 143 Assert.AreEqual("I'm dead", comp.LastError.Message);
174 } 144 }
175 145
176 [TestMethod] 146 [TestMethod]
177 public void StopCancelTest() { 147 public void StopCancelTest() {
178 var comp = new Runnable(true) { 148 var comp = new MockRunnableComponent(true) {
179 MockStop = () => PromiseHelper.Sleep(100000, 0) 149 MockStop = () => PromiseHelper.Sleep(100000, 0)
180 }; 150 };
181 151
182 comp.Start(); 152 comp.Start();
183 var p = comp.Stop(); 153 var p = comp.Stop();