Skip to content
Snippets Groups Projects
Commit 654cb7f1 authored by Tobias's avatar Tobias
Browse files

ConfigOperationMode does not try to change in current state anymore,

Reactivated Exception on not allowed state change
parent 03cf9e3d
No related branches found
No related tags found
No related merge requests found
......@@ -43,7 +43,8 @@ namespace ControlComponents.Core
private void ExecutionStateChanged(object sender, ExecutionStateEventArgs e)
{
base.execution.SetState(e.ExecutionState);
if(e.ExecutionState != base.execution.EXST)
base.execution.SetState(e.ExecutionState);
}
protected override async Task Resetting(CancellationToken token)
......@@ -120,7 +121,7 @@ namespace ControlComponents.Core
protected override async Task Aborting(CancellationToken token)
{
if(_externalCC.OpModeName != "NONE" && _externalCC.EXST != ExecutionState.ABORTING)
if(!token.IsCancellationRequested && _externalCC.OpModeName != "NONE" && _externalCC.EXST != ExecutionState.ABORTING)
{
_externalCC.Abort(base.execution.ComponentName);
}
......
......@@ -61,7 +61,7 @@ namespace ControlComponents.Core
}
else
{
logger.Warn($"{ComponentName} not allowed to change from {EXST} to {newState}");
throw new ExecutionException($"{ComponentName} not allowed to change from {EXST} to {newState}");
}
}
......
using System;
using System.Runtime.Serialization;
namespace ControlComponents.Core
{
[Serializable]
public class ExecutionException : Exception
{
public ExecutionException(string message) : base(message)
{
}
}
}
\ No newline at end of file
......@@ -3,6 +3,7 @@ using System.Reflection;
namespace ControlComponents.Core
{
// https://web.archive.org/web/20141020092917/http://flurfunk.sdx-ag.de/2012/05/c-performance-bei-der-befullungmapping.html
public static class PropertyCache
{
public static Func<TReturn> BuildTypedGetter<TReturn>(PropertyInfo propertyInfo, object instance)
......@@ -17,33 +18,17 @@ namespace ControlComponents.Core
var reflGet = Delegate.CreateDelegate(typeof(Func<TReturn>), instance, methodInfo);
return (Func<TReturn>)reflGet;
}
public static Func<TParam, TReturn> BuildTypedFunc<TParam, TReturn>(MethodInfo methodInfo, object instance)
{
var reflGet = Delegate.CreateDelegate(typeof(Func<TParam, TReturn>), instance, methodInfo);
return (Func<TParam, TReturn>)reflGet;
}
public static Func<TParam1, TParam2, TReturn> BuildTypedFunc<TParam1, TParam2, TReturn>(MethodInfo methodInfo, object instance)
{
var reflGet = Delegate.CreateDelegate(typeof(Func<TParam1, TParam2, TReturn>), instance, methodInfo);
return (Func<TParam1, TParam2, TReturn>)reflGet;
}
public static Action<TParam> BuildTypedAction<TParam>(MethodInfo methodInfo, object instance)
{
var reflGet = Delegate.CreateDelegate(typeof(Action<TParam>), instance, methodInfo);
return (Action<TParam>)reflGet;
}
public static Action BuildTypedAction(MethodInfo methodInfo, object instance)
{
var reflGet = Delegate.CreateDelegate(typeof(Action), instance, methodInfo);
return (Action)reflGet;
}
// public static Action<T, TProperty> BuildTypedSetter<T, TProperty>(PropertyInfo propertyInfo)
// {
// Action<T, TProperty> reflSet = (Action<T, TProperty>)Delegate.CreateDelegate(typeof(Action<T, TProperty>), propertyInfo.GetSetMethod());
// return reflSet;
// }
}
}
\ No newline at end of file
......@@ -124,7 +124,7 @@ namespace ControlComponents.Core.Tests
}
[Test]
public void Given_Stopped_When_UserActions_Then_Throw()
public void Given_NoOperationModeSelected_When_UserActions_Then_Throw()
{
InvalidOperationException e = Assert.Throws<InvalidOperationException>(() => cc.Reset(SENDER));
Assert.AreEqual($"{cc.ComponentName} cannot change to {ExecutionState.RESETTING}, if no operation mode is selected", e.Message);
......
......@@ -84,20 +84,19 @@ namespace ControlComponents.Core.Tests
}
[Test]
[Ignore("Need to return bool to indicate success")]
public void Given_Stopped_When_NotAllowedOperations_Then_Throw()
{
// Assert.Throws<ExecutionException>(() => cc.Start(SENDER));
Assert.Throws<ExecutionException>(() => cc.Start(SENDER));
// Assert.Throws<ExecutionException>(() => cc.Suspend(SENDER));
Assert.Throws<ExecutionException>(() => cc.Suspend(SENDER));
// Assert.Throws<ExecutionException>(() => cc.Unsuspend(SENDER));
Assert.Throws<ExecutionException>(() => cc.Unsuspend(SENDER));
// Assert.Throws<ExecutionException>(() => cc.Stop(SENDER));
Assert.Throws<ExecutionException>(() => cc.Stop(SENDER));
// Assert.Throws<ExecutionException>(() => cc.Hold(SENDER));
Assert.Throws<ExecutionException>(() => cc.Hold(SENDER));
// Assert.Throws<ExecutionException>(() => cc.Unhold(SENDER));
Assert.Throws<ExecutionException>(() => cc.Unhold(SENDER));
}
[Test]
......
......@@ -22,19 +22,26 @@ namespace ControlComponents.Core.Tests
}
[Test, AutoData]
public void Test_CallMethod(ControlComponent sut)
public async Task Test_CallMethod(ControlComponent sut)
{
sut.AddOperationMode(new OperationModeAsync(OPMODE));
MethodInfo free = sut.GetType().GetMethod(nameof(sut.Reset));
MethodInfo stop = sut.GetType().GetMethod(nameof(sut.Stop));
MethodInfo select = sut.GetType().GetMethod(nameof(sut.SelectOperationMode));
MethodInfo deselect = sut.GetType().GetMethod(nameof(sut.DeselectOperationMode));
var selectFunc = PropertyCache.BuildTypedFunc<string,Task>(select, sut);
Task running = selectFunc(OPMODE);
var freeFunc = PropertyCache.BuildTypedAction<string>(free, sut);
freeFunc("SENDER");
var stopFunc = PropertyCache.BuildTypedAction<string>(stop, sut);
var deselectFunc = PropertyCache.BuildTypedFunc<Task>(deselect, sut);
Task running = selectFunc(OPMODE);
freeFunc("SENDER");
sut.EXST.Should().Be(ExecutionState.RESETTING);
stopFunc("SENDER");
await sut.WaitForStopped();
await deselectFunc();
}
[Test, AutoData]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment