##// END OF EJS Templates
missing files
missing files

File last commit:

r208:7d07503621fe v2
r237:f2150c16b476 v2
Show More
RunnableComponentTests.cs
228 lines | 6.4 KiB | text/x-csharp | CSharpLexer
/ Implab.Test / RunnableComponentTests.cs
cin
runnable component, work in progress
r185 using System;
using System.Reflection;
using System.Threading;
using Implab.Parallels;
using Implab.Components;
cin
Added 'Fail' method to RunnableComponent which allows component to move from...
r203 using Implab.Test.Mock;
cin
runnable component, work in progress
r185
#if MONO
using NUnit.Framework;
using TestClassAttribute = NUnit.Framework.TestFixtureAttribute;
using TestMethodAttribute = NUnit.Framework.TestAttribute;
cin
Updated VS project
r188 using AssertFailedException = NUnit.Framework.AssertionException;
cin
runnable component, work in progress
r185 #else
using Microsoft.VisualStudio.TestTools.UnitTesting;
#endif
namespace Implab.Test {
[TestClass]
public class RunnableComponentTests {
static void ShouldThrow(Action action) {
try {
action();
cin
Updated VS project
r188 Assert.Fail();
} catch (AssertFailedException) {
cin
runnable component, work in progress
r185 throw;
} catch {
}
}
[TestMethod]
public void NormalFlowTest() {
cin
Added 'Fail' method to RunnableComponent which allows component to move from...
r203 var comp = new MockRunnableComponent(false);
cin
runnable component, work in progress
r185
Assert.AreEqual(ExecutionState.Created, comp.State);
cin
Added ResetState to RunnableComponent to reset in case of failure...
r205 comp.Initialize();
cin
runnable component, work in progress
r185
Assert.AreEqual(ExecutionState.Ready, comp.State);
comp.Start().Join(1000);
Assert.AreEqual(ExecutionState.Running, comp.State);
comp.Stop().Join(1000);
Assert.AreEqual(ExecutionState.Disposed, comp.State);
}
[TestMethod]
public void InitFailTest() {
cin
Added 'Fail' method to RunnableComponent which allows component to move from...
r203 var comp = new MockRunnableComponent(false) {
cin
runnable component, work in progress
r185 MockInit = () => {
throw new Exception("BAD");
}
};
ShouldThrow(() => comp.Start());
ShouldThrow(() => comp.Stop());
Assert.AreEqual(ExecutionState.Created, comp.State);
cin
Added ResetState to RunnableComponent to reset in case of failure...
r205 ShouldThrow(comp.Initialize);
cin
runnable component, work in progress
r185
Assert.AreEqual(ExecutionState.Failed, comp.State);
ShouldThrow(() => comp.Start());
ShouldThrow(() => comp.Stop());
Assert.AreEqual(ExecutionState.Failed, comp.State);
comp.Dispose();
Assert.AreEqual(ExecutionState.Disposed, comp.State);
}
[TestMethod]
public void DisposedTest() {
cin
Added 'Fail' method to RunnableComponent which allows component to move from...
r203 var comp = new MockRunnableComponent(false);
cin
runnable component, work in progress
r185 comp.Dispose();
ShouldThrow(() => comp.Start());
ShouldThrow(() => comp.Stop());
cin
Added ResetState to RunnableComponent to reset in case of failure...
r205 ShouldThrow(comp.Initialize);
cin
runnable component, work in progress
r185
Assert.AreEqual(ExecutionState.Disposed, comp.State);
}
[TestMethod]
cin
Added ResetState to RunnableComponent to reset in case of failure...
r205 public void ShouldCallDisposeOnStop() {
var comp = new MockRunnableComponent(true);
bool disposed = false;
cin
RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)...
r208 comp.MockDispose = (disposing) => {
cin
Added ResetState to RunnableComponent to reset in case of failure...
r205 disposed = true;
};
comp.Start().Join(1000);
comp.Stop().Join(1000);
ShouldThrow(() => comp.Start());
ShouldThrow(() => comp.Stop());
ShouldThrow(comp.Initialize);
Assert.AreEqual(ExecutionState.Disposed, comp.State);
Assert.IsTrue(disposed);
}
[TestMethod]
public void ShouldNotCallDisposeOnStop() {
var comp = new MockRunnableComponent(true, true);
bool disposed = false;
cin
RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)...
r208 comp.MockDispose = (disposing) => {
cin
Added ResetState to RunnableComponent to reset in case of failure...
r205 disposed = true;
};
comp.Start().Join(1000);
comp.Stop().Join(1000);
Assert.AreEqual(ExecutionState.Ready, comp.State);
Assert.IsFalse(disposed);
}
[TestMethod]
public void SelfDisposeOnStop() {
var comp = new MockRunnableComponent(true, true);
bool disposed = false;
cin
RunnableComponent.Dispose(bool,Exception) changed to standart Dispose(bool)...
r208 comp.MockDispose = (disposing) => {
cin
Added ResetState to RunnableComponent to reset in case of failure...
r205 disposed = true;
};
comp.Start().Join(1000);
comp.Stop().Join(1000);
Assert.AreEqual(ExecutionState.Ready, comp.State);
Assert.IsFalse(disposed);
comp.MockStop = () => {
comp.Dispose();
return Promise.Success;
};
comp.Start().Join(1000);
comp.Stop().Join(1000);
Assert.AreEqual(ExecutionState.Disposed, comp.State);
Assert.IsTrue(disposed);
}
[TestMethod]
cin
runnable component, work in progress
r185 public void StartCancelTest() {
cin
Added 'Fail' method to RunnableComponent which allows component to move from...
r203 var comp = new MockRunnableComponent(true) {
cin
runnable component, work in progress
r185 MockStart = () => PromiseHelper.Sleep(100000, 0)
};
var p = comp.Start();
Assert.AreEqual(ExecutionState.Starting, comp.State);
p.Cancel();
ShouldThrow(() => p.Join(1000));
Assert.AreEqual(ExecutionState.Failed, comp.State);
cin
Updated VS project
r188
cin
working on diagnostics
r194 Assert.IsTrue(comp.LastError is OperationCanceledException);
cin
runnable component, work in progress
r185
comp.Dispose();
}
[TestMethod]
public void StartStopTest() {
var stop = new Signal();
cin
Added 'Fail' method to RunnableComponent which allows component to move from...
r203 var comp = new MockRunnableComponent(true) {
cin
runnable component, work in progress
r185 MockStart = () => PromiseHelper.Sleep(100000, 0),
MockStop = () => AsyncPool.RunThread(stop.Wait)
};
var p1 = comp.Start();
var p2 = comp.Stop();
// should enter stopping state
ShouldThrow(p1.Join);
Assert.IsTrue(p1.IsCancelled);
Assert.AreEqual(ExecutionState.Stopping, comp.State);
stop.Set();
p2.Join(1000);
Assert.AreEqual(ExecutionState.Disposed, comp.State);
}
[TestMethod]
public void StartStopFailTest() {
cin
Added 'Fail' method to RunnableComponent which allows component to move from...
r203 var comp = new MockRunnableComponent(true) {
cin
runnable component, work in progress
r185 MockStart = () => PromiseHelper.Sleep(100000, 0).Then(null,null,x => { throw new Exception("I'm dead"); })
};
comp.Start();
var p = comp.Stop();
// if Start fails to cancel, should fail to stop
ShouldThrow(() => p.Join(1000));
Assert.AreEqual(ExecutionState.Failed, comp.State);
Assert.IsNotNull(comp.LastError);
Assert.AreEqual("I'm dead", comp.LastError.Message);
}
[TestMethod]
public void StopCancelTest() {
cin
Added 'Fail' method to RunnableComponent which allows component to move from...
r203 var comp = new MockRunnableComponent(true) {
cin
runnable component, work in progress
r185 MockStop = () => PromiseHelper.Sleep(100000, 0)
};
comp.Start();
var p = comp.Stop();
Assert.AreEqual(ExecutionState.Stopping, comp.State);
p.Cancel();
ShouldThrow(() => p.Join(1000));
cin
Updated VS project
r188 Assert.AreEqual(ExecutionState.Failed, comp.State);
cin
working on diagnostics
r194 Assert.IsTrue(comp.LastError is OperationCanceledException);
cin
runnable component, work in progress
r185
comp.Dispose();
}
}
}