Skip to content

Commit

Permalink
Fix Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-dmxc committed Mar 25, 2024
1 parent 363e0d2 commit 0207531
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 19 deletions.
29 changes: 25 additions & 4 deletions RDMSharp/RDM/AsyncRDMRequestHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,42 @@
using System;
using System.Collections.Concurrent;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace RDMSharp
{
public class AsyncRDMRequestHelper
public class AsyncRDMRequestHelper : IDisposable
{
private static readonly ILogger Logger = null;
private static readonly Random random = new Random();
private readonly ConcurrentDictionary<int, Tuple<RDMMessage, RDMMessage>> buffer = new ConcurrentDictionary<int, Tuple<RDMMessage, RDMMessage>>();
private readonly Func<RDMMessage, Task> _sendMethode;
private CancellationTokenSource _cts;
public bool IsDisposing, IsDisposed;
public AsyncRDMRequestHelper(Func<RDMMessage, Task> sendMethode)
{
_cts = new CancellationTokenSource();
_sendMethode = sendMethode;
}

public void Dispose()
{
if (this.IsDisposing || this.IsDisposed)
return;
this.IsDisposing = true;
_cts.Cancel();
buffer.Clear();
_cts.Dispose();
this.IsDisposed = true;
this.IsDisposing= false;
}

public bool ReceiveMethode(RDMMessage rdmMessage)
{
if (this.IsDisposing || this.IsDisposed)
return false;

if (rdmMessage.Command == ERDM_Command.DISCOVERY_COMMAND_RESPONSE)
{
var o = buffer.FirstOrDefault(b => b.Value.Item1.Parameter == rdmMessage.Parameter);
Expand Down Expand Up @@ -60,21 +78,24 @@ public async Task<RequestResult> RequestParameter(RDMMessage requerst)
int count = 0;
do
{
if (this.IsDisposing || this.IsDisposed)
return new RequestResult(requerst);

buffer.TryGetValue(key, out Tuple<RDMMessage, RDMMessage> tuple1);
response = tuple1.Item2;
if (response != null)
break;
await Task.Delay(5);
await Task.Delay(5, _cts.Token);
if (requerst.Command == ERDM_Command.NONE)
{
throw new Exception("Command is not set");
}
count++;
if (count % 300 == 299)
{
await Task.Delay(TimeSpan.FromTicks(random.Next(33, 777)));
await Task.Delay(TimeSpan.FromTicks(random.Next(33, 777)), _cts.Token);
await _sendMethode.Invoke(requerst);
await Task.Delay(TimeSpan.FromTicks(random.Next(33, 777)));
await Task.Delay(TimeSpan.FromTicks(random.Next(33, 777)), _cts.Token);
}
if (count > 1 && requerst.Command == ERDM_Command.DISCOVERY_COMMAND)
break;
Expand Down
6 changes: 6 additions & 0 deletions RDMSharp/RDM/Device/AbstractRDMDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ private async Task sendRDMRequestMessage(RDMMessage rdmMessage)
protected abstract Task SendRDMMessage(RDMMessage rdmMessage);
protected async Task ReceiveRDMMessage(RDMMessage rdmMessage)
{
if (this.IsDisposed || IsDisposing)
return;

try
{
if (!IsGenerated)
Expand Down Expand Up @@ -311,6 +314,8 @@ protected RDMMessage processRequestMessage(RDMMessage rdmMessage)
break;

list.TryGetValue(index, out responseValue);
if (responseValue == null)
break;
response = _descriptionParameterWrapper.BuildGetResponseMessage(responseValue);
break;

Expand Down Expand Up @@ -1002,6 +1007,7 @@ public void Dispose()
if (IsDisposing || IsDisposed)
return;
IsDisposing = true;
asyncRDMRequestHelper.Dispose();
try
{
OnDispose();
Expand Down
43 changes: 28 additions & 15 deletions RDMSharpTests/Devices/TestRDMSendReceive.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ public void Setup()
remote = new MockDevice(uid, false);
}
[TearDown]
public void TearDown()
public async Task TearDown()
{
generated.Dispose();
remote.Dispose();
await Task.Delay(500);
}

[Test]
Expand All @@ -28,8 +29,6 @@ public async Task TestDevice1()
{
var parameterValuesRemote = remote.GetAllParameterValues();
var parameterValuesGenerated = generated.GetAllParameterValues();
var sensorsRemote = remote.Sensors.Values.ToList();
var sensorsGenerated = generated.Sensors.ToList();

Assert.Multiple(() =>
{
Expand All @@ -45,11 +44,6 @@ public async Task TestDevice1()
}
Assert.That(parameterValuesRemote, Has.Count.EqualTo(parameterValuesGenerated.Count));
});
Assert.Multiple(() =>
{
Assert.That(remote.DeviceModel.SupportedBlueprintParameters, Contains.Item(ERDM_Parameter.SENSOR_DEFINITION));
Assert.That(remote.DeviceModel.SupportedNonBlueprintParameters, Contains.Item(ERDM_Parameter.SENSOR_VALUE));
});

Assert.Multiple(() =>
{
Expand All @@ -58,12 +52,6 @@ public async Task TestDevice1()
Assert.That(remote.GetAllParameterValues()[ERDM_Parameter.DEVICE_MODEL_DESCRIPTION], Is.EqualTo(generated.DeviceModelDescription));
Assert.That(remote.GetAllParameterValues()[ERDM_Parameter.MANUFACTURER_LABEL], Is.EqualTo(generated.ManufacturerLabel));
Assert.That(((RDMDMXPersonality)remote.GetAllParameterValues()[ERDM_Parameter.DMX_PERSONALITY]).Index, Is.EqualTo(generated.CurrentPersonality));
});
Assert.Multiple(() =>
{

Assert.That(sensorsRemote, Has.Count.EqualTo(sensorsGenerated.Count));
Assert.That(sensorsRemote, Is.EqualTo(sensorsGenerated));
});

await remote.SetParameter(ERDM_Parameter.DMX_START_ADDRESS, (ushort)512);
Expand Down Expand Up @@ -107,8 +95,13 @@ public async Task TestDevice1()
Assert.Throws(typeof(NotSupportedException), () => { generated.TrySetParameter(ERDM_Parameter.DEVICE_LABEL, "Test"); });
Assert.Throws(typeof(NotSupportedException), () => { generated.TrySetParameter(ERDM_Parameter.DISC_MUTE, null); });
Assert.Throws(typeof(NotSupportedException), () => { generated.TrySetParameter(ERDM_Parameter.DEVICE_LABEL, new RDMDeviceInfo()); });
});
});
}

[Test]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion", "NUnit2010:Use EqualConstraint for better assertion messages in case of failure", Justification = "<Ausstehend>")]
public void TestDevice1Slots()
{
var slotIntensity = remote.Slots[0];
var slotStrobe = remote.Slots[1];
var slotRed = remote.Slots[2];
Expand Down Expand Up @@ -183,6 +176,26 @@ public async Task TestDevice1()
Assert.That(slots.Add(slotBlue), Is.False);
Assert.That(slots, Does.Contain(slotBlue));
});
}

[Test]
[System.Diagnostics.CodeAnalysis.SuppressMessage("Assertion", "NUnit2010:Use EqualConstraint for better assertion messages in case of failure", Justification = "<Ausstehend>")]
public void TestDevice1Sensor()
{
var sensorsRemote = remote.Sensors.Values.ToList();
var sensorsGenerated = generated.Sensors.ToList();

Assert.Multiple(() =>
{
Assert.That(remote.DeviceModel.SupportedBlueprintParameters, Contains.Item(ERDM_Parameter.SENSOR_DEFINITION));
Assert.That(remote.DeviceModel.SupportedNonBlueprintParameters, Contains.Item(ERDM_Parameter.SENSOR_VALUE));
});

Assert.Multiple(() =>
{
Assert.That(sensorsRemote, Has.Count.EqualTo(sensorsGenerated.Count));
Assert.That(sensorsRemote, Is.EqualTo(sensorsGenerated));
});
}
}
}

0 comments on commit 0207531

Please sign in to comment.