diff --git a/GpioController/Commands/GpioSetCommand.cs b/GpioController/Commands/GpioSetCommand.cs index 8a2ed08..2dfd97f 100644 --- a/GpioController/Commands/GpioSetCommand.cs +++ b/GpioController/Commands/GpioSetCommand.cs @@ -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); } } diff --git a/GpioController/Services/StateService.cs b/GpioController/Services/StateService.cs index 55174ef..11ac9df 100644 --- a/GpioController/Services/StateService.cs +++ b/GpioController/Services/StateService.cs @@ -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(); command.Execute(request); } @@ -42,6 +43,8 @@ public void UpdateMultipleStates(IEnumerable updateRequests) request.CancellationToken = tokenManagementService.CreateToken(request); var command = commandFactory.GetCommand(); command.Execute(request); + if (request.CancellationToken.IsCancellationRequested) + break; } }).StartOnBackgroundThread(); } diff --git a/GpioControllerTests/Services/StateServiceTests.cs b/GpioControllerTests/Services/StateServiceTests.cs new file mode 100644 index 0000000..8cf4e7e --- /dev/null +++ b/GpioControllerTests/Services/StateServiceTests.cs @@ -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; + private IGpioService gpioService; + private ITokenManagementService tokenManagementService; + + private StateService GetSystemUnderTest() + { + commandFactory = A.Fake(); + gpioService = A.Fake(); + tokenManagementService = A.Fake(); + + return new StateService(commandFactory, gpioService, tokenManagementService); + } + + [Fact] + public void UpdateMultipleStates_WhenItHasMultipleRequests_CancellingOneTaskShouldCancelAll() + { + var sut = GetSystemUnderTest(); + + var requests = new List + { + 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 + { + 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>(); + + A.CallTo(() => commandFactory.GetCommand()).Returns(command); + A.CallTo(() => command.Execute(A._)) + .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._)).MustHaveHappenedOnceExactly(); + } +} \ No newline at end of file diff --git a/Installation/linux-arm64/gpio-controller-api-1.4.deb b/Installation/linux-arm64/gpio-controller-api-1.4.deb index 380ff20..2203099 100644 Binary files a/Installation/linux-arm64/gpio-controller-api-1.4.deb and b/Installation/linux-arm64/gpio-controller-api-1.4.deb differ diff --git a/Installation/linux-x64/gpio-controller-api-1.4.deb b/Installation/linux-x64/gpio-controller-api-1.4.deb index 74973e7..afe5e7e 100644 Binary files a/Installation/linux-x64/gpio-controller-api-1.4.deb and b/Installation/linux-x64/gpio-controller-api-1.4.deb differ