Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
luca-domenichini committed Jan 10, 2024
2 parents 8660616 + b2a6014 commit ea616ba
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class TcpClientConnector : AbstractPublisherConnector
private TcpClient? _tcpClient;
private readonly IStreamMessageSerializer _messageSerializer;
private readonly CancellationTokenSource _stopToken = new CancellationTokenSource();
private readonly ManualResetEventSlim _reconnectTaskTerminated = new ManualResetEventSlim();
private readonly SemaphoreSlim _reconnectTaskTerminated = new SemaphoreSlim(0, 1);
private readonly CountdownLatch _clients = new CountdownLatch();

public TcpClientConnector(TcpClientConnectorOptions options)
Expand Down Expand Up @@ -167,7 +167,7 @@ await connectorInterface.RunInitializationActionAsync(async (deviceStatusEvents,
}
finally
{
_reconnectTaskTerminated.Set();
_reconnectTaskTerminated.Release();
}
});
}
Expand Down Expand Up @@ -266,6 +266,8 @@ public override async Task StopAsync()
_tcpClient?.Close();

_clients.WaitUntilZero();
_reconnectTaskTerminated.Wait();
await _reconnectTaskTerminated.WaitAsync();

_stopToken.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -217,5 +217,7 @@ public override async Task StopAsync()
{
client.Close();
}

_stopToken.Dispose();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public override Task StartAsync(ISmartIOTConnectorInterface connectorInterface)
public override Task StopAsync()
{
_stopToken.Cancel();
_stopToken.Dispose();

ConnectorInterface!.OnConnectorStopped(new ConnectorStoppedEventArgs(this, $"Connector stopped {ConnectionString}"));

Expand Down
3 changes: 3 additions & 0 deletions Core/SmartIOT.Connector.Core/Scheduler/TagSchedulerEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -524,6 +524,9 @@ private void OnTagWrite(TagScheduleEvent evt)

public bool IsRestartNeeded()
{
if (DeviceDriver.Device.DeviceStatus == DeviceStatus.DISABLED)
return false;

bool restart = _lastRestartInstant == null;
if (!restart
&& _timeService.IsTimeoutElapsed(_lastRestartInstant!.Value, _configuration.RestartDeviceInErrorTimeout))
Expand Down
35 changes: 35 additions & 0 deletions Tests/SmartIOT.Connector.Core.Tests/TagSchedulerEngineTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,41 @@ private static (TagSchedulerEngine engine, Model.Device device, Tag tag20, Tag?
return (engine, device, device.Tags.First(x => x.TagId == "DB20")!, device.Tags.FirstOrDefault(x => x.TagId == "DB21"), device.Tags.First(x => x.TagId == "DB22")!);
}

[Fact]
public void Test_should_not_restart_on_disabled_device()
{
var timeService = new FakeTimeService
{
Now = DateTime.Now
};

SchedulerConfiguration schedulerConfiguration = new SchedulerConfiguration();

(TagSchedulerEngine engine, Model.Device device, Tag tag20, Tag? tag21, Tag tag22) = SetupSystem(device => new MockDeviceDriver(device), timeService, schedulerConfiguration, true, false, 0);
device.SetEnabled(false); // disable device

var eventListener = new FakeConnector();

engine.TagReadEvent += eventListener.OnTagReadEvent;
engine.TagWriteEvent += eventListener.OnTagWriteEvent;
engine.DeviceStatusEvent += eventListener.OnDeviceStatusEvent;
engine.ExceptionHandler += eventListener.OnException;

// verifica stato iniziale
Assert.Equal(DeviceStatus.DISABLED, device.DeviceStatus);
Assert.False(tag20.IsInitialized);
Assert.Equal(0, tag20.ErrorCode);
Assert.False(tag21!.IsInitialized);
Assert.Equal(0, tag21.ErrorCode);
Assert.False(tag22.IsInitialized);
Assert.Equal(0, tag22.ErrorCode);

// check driver restart
Assert.False(engine.IsRestartNeeded());

Assert.Throws<TagSchedulerWaitException>(() => engine.GetNextTagSchedule());
}

[Fact]
public void Test_read_two_tags_cycle()
{
Expand Down
2 changes: 2 additions & 0 deletions Tests/SmartIOT.Connector.Tcp.Tests/TcpConnectorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ public async Task Test_scheduler_and_TcpClientConnector(string serializerType, i
await connector.StopAsync();

token?.Cancel();
token?.Dispose();
stream?.Close();
server.Stop();
}
Expand Down Expand Up @@ -337,6 +338,7 @@ public async Task Test_scheduler_and_TcpServerConnector(string serializerType, i
await connector.StopAsync();

token?.Cancel();
token?.Dispose();
stream?.Close();
client.Close();
}
Expand Down

0 comments on commit ea616ba

Please sign in to comment.