Implementing Yats cancellable test case

By | November 10, 2015

Here is a code snippet how a cancellable test case can be implemented in Yats.

using yats.Attributes;
using yats.Ports;
using yats.Ports.Utilities;
using yats.TestCase.Interface;
using yats.TestCase.Parameter;
using yats.TestRepositoryManager.YatsNativeTestCase;
using log4net;
using System;
using System.Threading;

namespace yats.TestCase.Example
{
[Description("Test case description")]
public class Example : IYatsTestCase, ICancellable
{
internal static ILog Logger = LogManager.GetLogger("Example");

[Parameter]
public TimeSpan Timeout;

[Parameter]
public SerialPortSettingsParameter PortSettings;

public ITestResult Execute()
{
AbstractPort port = null;
using (AutoResetEvent cancelWaitEvent = new AutoResetEvent(false))
{
EventHandler myDelegate = (sender, e) => { cancelWaitEvent.Set(); };
cancelHelper.OnCancel += myDelegate;

try
{
port = PortOpenHelper.Open(PortSettings, cancelWaitEvent);
var parser = new //some parser type
ReadOperationResult result = port.Execute(Timeout, parser, cancelWaitEvent);
return ResultHelper.Convert(result);
}
catch
{
throw;
}
finally
{
cancelHelper.OnCancel -= myDelegate;
PortOpenHelper.Close(port);
}
}
}

#region ICancellable Members

private CancelHelper cancelHelper = new CancelHelper();

public void Cancel()
{
cancelHelper.Cancel();
}

#endregion
}
}

Leave a Reply

Your email address will not be published.