Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions GpioController/Commands/GpioSetCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ protected override void RunOptionalPostCommandLogic(GpioSetRequest request, Gpio

var sleepTime = request.Options?.Milliseconds ?? 0;

for (var timesRepeated = 1; timesRepeated < request.Options?.RepeatTimes*2; timesRepeated++)
for (var timesRepeated = 1; timesRepeated < request.Options?.RepeatTimes * 2; timesRepeated++)
{
Thread.Sleep(sleepTime);
if (request.CancellationToken.IsCancellationRequested)
break;
Thread.Sleep(sleepTime);
RunOpposite(request);
}
}
Expand Down
5 changes: 4 additions & 1 deletion GpioController/Services/StateService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public GpioReadResult GetStateByGpioId(int chipsetId, int gpioId)
public void UpdateSingleState(GpioSetRequest request)
{
gpioService.GetGpioById(request.Chipset, request.Gpios.First());


request.CancellationToken = tokenManagementService.CreateToken(request);
var command = commandFactory.GetCommand<GpioSetRequest, GpioSetResult>();
command.Execute(request);
}
Expand All @@ -42,6 +43,8 @@ public void UpdateMultipleStates(IEnumerable<GpioSetRequest> updateRequests)
request.CancellationToken = tokenManagementService.CreateToken(request);
var command = commandFactory.GetCommand<GpioSetRequest, GpioSetResult>();
command.Execute(request);
if (request.CancellationToken.IsCancellationRequested)
break;
}
}).StartOnBackgroundThread();
}
Expand Down
125 changes: 125 additions & 0 deletions GpioControllerTests/Services/StateServiceTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
using FakeItEasy;
using GpioController.Commands;
using GpioController.Commands.Request;
using GpioController.Commands.Results;
using GpioController.Factories;
using GpioController.Models;
using GpioController.Services;

namespace GpioControllerTests.Services;

public class StateServiceTests
{
private ICommandFactory commandFactory;

Check warning on line 13 in GpioControllerTests/Services/StateServiceTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'commandFactory' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
private IGpioService gpioService;

Check warning on line 14 in GpioControllerTests/Services/StateServiceTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'gpioService' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.
private ITokenManagementService tokenManagementService;

Check warning on line 15 in GpioControllerTests/Services/StateServiceTests.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable field 'tokenManagementService' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the field as nullable.

private StateService GetSystemUnderTest()
{
commandFactory = A.Fake<ICommandFactory>();
gpioService = A.Fake<IGpioService>();
tokenManagementService = A.Fake<ITokenManagementService>();

return new StateService(commandFactory, gpioService, tokenManagementService);
}

[Fact]
public void UpdateMultipleStates_WhenItHasMultipleRequests_CancellingOneTaskShouldCancelAll()
{
var sut = GetSystemUnderTest();

var requests = new List<GpioSetRequest>
{
new GpioSetRequest
{
Chipset = 1,
Gpios = [91, 92, 81],
State = "0",
Options = new OptionalSettings
{
Milliseconds = 1000,
RepeatTimes = 1
}
},
new GpioSetRequest
{
Chipset = 1,
Gpios = [91, 92, 95],
State = "0",
Options = new OptionalSettings
{
Milliseconds = 1000,
RepeatTimes = 1
}
},
new GpioSetRequest
{
Chipset = 1,
Gpios = [91, 92, 80],
State = "0",
Options = new OptionalSettings
{
Milliseconds = 1000,
RepeatTimes = 1
}
}
};

var cancellationSource = new CancellationTokenSource();
var cancellationToken = cancellationSource.Token;

A.CallTo(() => tokenManagementService.CreateToken(requests[0])).Returns(cancellationToken);
A.CallTo(() => gpioService.GetGpios()).Returns(new List<Gpio>
{
new()
{
Chipset = 1,
Name = "GPIO 1",
Id = 91
},
new()
{
Chipset = 1,
Name = "GPIO 2",
Id = 92
},
new()
{
Chipset = 1,
Name = "GPIO 3",
Id = 95
},
new()
{
Chipset = 1,
Name = "GPIO 4",
Id = 81
},
new()
{
Chipset = 1,
Name = "GPIO 5",
Id = 80
}
});

var command = A.Fake<ICommand<GpioSetRequest, GpioSetResult>>();

A.CallTo(() => commandFactory.GetCommand<GpioSetRequest, GpioSetResult>()).Returns(command);
A.CallTo(() => command.Execute(A<GpioSetRequest>._))
.Invokes((GpioSetRequest request, GpioSetResult result) =>
{
Thread.Sleep(request.Options?.Milliseconds ?? 0);
});

sut.UpdateMultipleStates(requests);

Thread.Sleep(50);

cancellationSource.Cancel();

Thread.Sleep(3500);

A.CallTo(() => command.Execute(A<GpioSetRequest>._)).MustHaveHappenedOnceExactly();
}
}
Binary file modified Installation/linux-arm64/gpio-controller-api-1.4.deb
Binary file not shown.
Binary file modified Installation/linux-x64/gpio-controller-api-1.4.deb
Binary file not shown.
Loading