Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retry for file chunks #83

Merged
merged 3 commits into from
Dec 19, 2024
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tools/
# Visual Studio 2015 cache/options directory
.vs/
.vscode/
.idea/

# MSTest test Results
[Tt]est[Rr]esult*/
Expand Down
46 changes: 0 additions & 46 deletions .reuse/dep5

This file was deleted.

1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ SPDX-License-Identifier: MPL-2.0
<PackageVersion Include="C-DEngine" Version="6.104.0" />
<PackageVersion Include="coverlet.collector" Version="6.0.2" />
<PackageVersion Include="LiteDB" Version="5.0.21" />
<PackageVersion Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Abstractions" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Binder" Version="9.0.0" />
<PackageVersion Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
Expand Down
58 changes: 58 additions & 0 deletions REUSE.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
version = 1
SPDX-PackageName = "Smart Application Framework"
SPDX-PackageSupplier = "TRUMPF Laser GmbH <support.tls560@trumpf.com>"
SPDX-PackageDownloadLocation = "https://github.com/TRUMPF-IoT/saf"

[[annotations]]
path = ".github/**/**"
precedence = "aggregate"
SPDX-FileCopyrightText = "2017-2021 TRUMPF Laser GmbH"
SPDX-License-Identifier = "MPL-2.0"

[[annotations]]
path = "**.md"
precedence = "aggregate"
SPDX-FileCopyrightText = "2017-2021 TRUMPF Laser GmbH"
SPDX-License-Identifier = "MPL-2.0"

[[annotations]]
path = "docs/_config.yml"
precedence = "aggregate"
SPDX-FileCopyrightText = "2017-2021 TRUMPF Laser GmbH"
SPDX-License-Identifier = "MPL-2.0"

[[annotations]]
path = "docs/**/**"
precedence = "aggregate"
SPDX-FileCopyrightText = "2017-2021 TRUMPF Laser GmbH"
SPDX-License-Identifier = "MPL-2.0"

[[annotations]]
path = "src/**/**.txt"
precedence = "aggregate"
SPDX-FileCopyrightText = "2017-2021 TRUMPF Laser GmbH"
SPDX-License-Identifier = "MPL-2.0"

[[annotations]]
path = "src/**.sln"
precedence = "aggregate"
SPDX-FileCopyrightText = "2017-2021 TRUMPF Laser GmbH"
SPDX-License-Identifier = "MPL-2.0"

[[annotations]]
path = "src/**/**.sln"
precedence = "aggregate"
SPDX-FileCopyrightText = "2017-2021 TRUMPF Laser GmbH"
SPDX-License-Identifier = "MPL-2.0"

[[annotations]]
path = "src/**/**.json"
precedence = "aggregate"
SPDX-FileCopyrightText = "2017-2021 TRUMPF Laser GmbH"
SPDX-License-Identifier = "MPL-2.0"

[[annotations]]
path = "src/**/app.config"
precedence = "aggregate"
SPDX-FileCopyrightText = "2017-2021 TRUMPF Laser GmbH"
SPDX-License-Identifier = "MPL-2.0"
38 changes: 26 additions & 12 deletions src/Toolbox/SAF.Toolbox.Tests/Filetransfer/FileSenderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: MPL-2.0

using Microsoft.Extensions.Options;
using NSubstitute;
using SAF.Common;
using SAF.Toolbox.FileTransfer;
Expand All @@ -15,9 +16,9 @@ public class FileSenderTests
[Theory]
[InlineData(1)] // 1 byte
[InlineData(1024)] // 1 kByte
[InlineData(1024 * 3)] // 3 kByte
[InlineData(1024 * 3)] // 3 kByte
[InlineData(1024 * 1024)] // 1 MByte
[InlineData(1024 * 1024 * 3)] // 3 MByte
[InlineData(1024 * 1024 * 3)] // 3 MByte
[InlineData(FileSender.MaxChunkSize - 1)] // excact chunk size - 1
[InlineData(FileSender.MaxChunkSize)] // excact chunk size
[InlineData(FileSender.MaxChunkSize + 1)] // excact chunk size + 1
Expand All @@ -30,7 +31,9 @@ public async Task SendInChunksCallsPublishOk(int fileSizeInBytes)
Action<Message>? senderHandler = null;
var messaging = Substitute.For<IMessagingInfrastructure>();
messaging.When(m => m.Subscribe(Arg.Any<string>(), Arg.Any<Action<Message>>()))
.Do(args => senderHandler = args.Arg<Action<Message>>());
.Do(args => senderHandler = args.Arg<Action<Message>>());
var options = Substitute.For<IOptions<FileSenderConfiguration>>();
options.Value.Returns(new FileSenderConfiguration());

var testChannel = $"tests/fileSender/{fileSizeInBytes}";
var buffer = new byte[fileSizeInBytes];
Expand All @@ -43,7 +46,7 @@ public async Task SendInChunksCallsPublishOk(int fileSizeInBytes)
});
using (var tempFile = new TemporaryFile($"file{fileSizeInBytes}.tmp", buffer))
{
var fileSender = new FileSender(messaging, null);
var fileSender = new FileSender(messaging, null, options);
var sendResult = await fileSender.SendInChunks(testChannel, tempFile.TempFilePath);
Assert.Equal(FileTransferStatus.Delivered, sendResult);
}
Expand All @@ -56,9 +59,9 @@ public async Task SendInChunksCallsPublishOk(int fileSizeInBytes)
[Theory]
[InlineData(1)] // 1 byte
[InlineData(1024)] // 1 kByte
[InlineData(1024 * 3)] // 3 kByte
[InlineData(1024 * 3)] // 3 kByte
[InlineData(1024 * 1024)] // 1 MByte
[InlineData(1024 * 1024 * 3)] // 3 MByte
[InlineData(1024 * 1024 * 3)] // 3 MByte
[InlineData(FileSender.MaxChunkSize - 1)] // excact chunk size - 1
[InlineData(FileSender.MaxChunkSize)] // excact chunk size
[InlineData(FileSender.MaxChunkSize + 1)] // excact chunk size + 1
Expand All @@ -71,6 +74,8 @@ public async Task SendInChunksAllowsWriteAccessToFileAfterSendingLastChunkOk(int
var messaging = Substitute.For<IMessagingInfrastructure>();
messaging.When(m => m.Subscribe(Arg.Any<string>(), Arg.Any<Action<Message>>()))
.Do(args => senderHandler = args.Arg<Action<Message>>());
var options = Substitute.For<IOptions<FileSenderConfiguration>>();
options.Value.Returns(new FileSenderConfiguration());

var testChannel = $"tests/fileSender/{fileSizeInBytes}";
var buffer = new byte[fileSizeInBytes];
Expand All @@ -93,7 +98,7 @@ public async Task SendInChunksAllowsWriteAccessToFileAfterSendingLastChunkOk(int
senderHandler?.Invoke(new Message { Topic = req.ReplyTo, Payload = "OK" });
});

var fileSender = new FileSender(messaging, null);
var fileSender = new FileSender(messaging, null, options);
var sendResult = await fileSender.SendInChunks(testChannel, tempFile.TempFilePath);
Assert.Equal(FileTransferStatus.Delivered, sendResult);
}
Expand Down Expand Up @@ -121,6 +126,8 @@ public async Task SendInChunksUsesSameUniqueTransferIdForEachChunkOk(int fileSiz
var messaging = Substitute.For<IMessagingInfrastructure>();
messaging.When(m => m.Subscribe(Arg.Any<string>(), Arg.Any<Action<Message>>()))
.Do(args => senderHandler = args.Arg<Action<Message>>());
var options = Substitute.For<IOptions<FileSenderConfiguration>>();
options.Value.Returns(new FileSenderConfiguration());

var testChannel = $"tests/fileSender/{fileSizeInBytes}";
var buffer = new byte[fileSizeInBytes];
Expand All @@ -139,7 +146,7 @@ public async Task SendInChunksUsesSameUniqueTransferIdForEachChunkOk(int fileSiz
});
using (var tempFile = new TemporaryFile($"file{fileSizeInBytes}.tmp", buffer))
{
var fileSender = new FileSender(messaging, null);
var fileSender = new FileSender(messaging, null, options);
var sendResult = await fileSender.SendInChunks(testChannel, tempFile.TempFilePath);
Assert.Equal(FileTransferStatus.Delivered, sendResult);
}
Expand All @@ -149,23 +156,30 @@ public async Task SendInChunksUsesSameUniqueTransferIdForEachChunkOk(int fileSiz
messaging.Received(Convert.ToInt32(expectedCalls)).Publish(Arg.Is<Message>(msg => msg.Topic == testChannel));
}

[Fact]
public async Task SendInChunksWithMissingAnswerReturnsTimedOutOk()
[Theory]
[InlineData(1)]
[InlineData(4)]
public async Task SendInChunksWithMissingAnswerReturnsTimedOutOk(int expectedCalls)
{
const int fileSizeInBytes = 1024;

var messaging = Substitute.For<IMessagingInfrastructure>();
var options = Substitute.For<IOptions<FileSenderConfiguration>>();
options.Value.Returns(new FileSenderConfiguration
{
RetryAttemptsForFailedChunks = expectedCalls - 1
});

var testChannel = $"tests/fileSender/{fileSizeInBytes}";
var buffer = new byte[fileSizeInBytes];
using (var tempFile = new TemporaryFile($"file{fileSizeInBytes}.tst", buffer))
{
var fileSender = new FileSender(messaging, null);
var fileSender = new FileSender(messaging, null, options);
fileSender.Timeout = 2000;
var sendResult = await fileSender.SendInChunks(testChannel, tempFile.TempFilePath);
Assert.Equal(FileTransferStatus.TimedOut, sendResult);
}

var expectedCalls = 1;
messaging.Received(expectedCalls).Publish(Arg.Is<Message>(msg => msg.Topic == testChannel));
}
}
1 change: 1 addition & 0 deletions src/Toolbox/SAF.Toolbox.Tests/SAF.Toolbox.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ SPDX-License-Identifier: MPL-2.0
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Configuration" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="NSubstitute" />
Expand Down
57 changes: 54 additions & 3 deletions src/Toolbox/SAF.Toolbox.Tests/ServiceCollectionExtensionsTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
// SPDX-FileCopyrightText: 2017-2021 TRUMPF Laser GmbH
//
// SPDX-License-Identifier: MPL-2.0

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using NSubstitute;
using SAF.Common;
using SAF.Toolbox.FileTransfer;
using SAF.Toolbox.Heartbeat;
using SAF.Toolbox.RequestClient;
using Xunit;
Expand Down Expand Up @@ -47,7 +52,7 @@ public void AddHeartbeatPoolAddsServiceOnlyOnceOk()
[Fact]
public void AddRequestClientAddsServiceAndRequiredServicesOk()
{
_services.AddSingleton(sp => Substitute.For<IMessagingInfrastructure>());
_services.AddSingleton(_ => Substitute.For<IMessagingInfrastructure>());
_services.AddRequestClient();

using var provider = _services.BuildServiceProvider();
Expand All @@ -58,7 +63,7 @@ public void AddRequestClientAddsServiceAndRequiredServicesOk()
[Fact]
public void AddRequestClientAddsServiceAfterAddHeartbeatPoolOk()
{
_services.AddSingleton(sp => Substitute.For<IMessagingInfrastructure>());
_services.AddSingleton(_ => Substitute.For<IMessagingInfrastructure>());
_services.AddHeartbeatPool();
_services.AddRequestClient();

Expand All @@ -70,7 +75,7 @@ public void AddRequestClientAddsServiceAfterAddHeartbeatPoolOk()
[Fact]
public void AddRequestClientAddsServiceOnlyOnceOk()
{
_services.AddSingleton(sp => Substitute.For<IMessagingInfrastructure>());
_services.AddSingleton(_ => Substitute.For<IMessagingInfrastructure>());

_services.AddRequestClient();
_services.AddRequestClient();
Expand All @@ -82,4 +87,50 @@ public void AddRequestClientAddsServiceOnlyOnceOk()
Assert.Single(provider.GetServices<IRequestClient>());
Assert.NotNull(provider.GetService<IRequestClient>());
}

[Fact]
public void AddFileSenderWithoutConfigAddsServiceWithDefaultConfigOk()
{
// Arrange
_services.AddSingleton(_ => Substitute.For<IMessagingInfrastructure>());
_services.AddSingleton(_ => Substitute.For<ILogger<FileSender>>());

// Act
_services.AddFileSender();

using var provider = _services.BuildServiceProvider();
var fileSender = provider.GetService<IFileSender>();
var options = provider.GetService<IOptions<FileSenderConfiguration>>();

// Assert
Assert.NotNull(fileSender);
Assert.NotNull(options);
Assert.NotNull(options.Value);
Assert.Equal(0, options.Value.RetryAttemptsForFailedChunks);
}

[Fact]
public void AddFileSenderWithConfigAddsServiceWithSpecificConfigOk()
{
// Arrange
_services.AddSingleton(_ => Substitute.For<IMessagingInfrastructure>());
_services.AddSingleton(_ => Substitute.For<ILogger<FileSender>>());
var config = new ConfigurationBuilder().AddInMemoryCollection(new Dictionary<string, string>
{
{ "FileSender:RetryAttemptsForFailedChunks", "5" }
}!).Build();

// Act
_services.AddFileSender(config);

using var provider = _services.BuildServiceProvider();
var fileSender = provider.GetService<IFileSender>();
var options = provider.GetService<IOptions<FileSenderConfiguration>>();

// Assert
Assert.NotNull(fileSender);
Assert.NotNull(options);
Assert.NotNull(options.Value);
Assert.Equal(5, options.Value.RetryAttemptsForFailedChunks);
}
}
10 changes: 10 additions & 0 deletions src/Toolbox/SAF.Toolbox/FileSenderConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-FileCopyrightText: 2017-2024 TRUMPF Laser GmbH
//
// SPDX-License-Identifier: MPL-2.0

namespace SAF.Toolbox;

public class FileSenderConfiguration
{
public int RetryAttemptsForFailedChunks { get; set; } = 0;
}
Loading
Loading