-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathR2DownloadResume_Tests.cs
More file actions
163 lines (145 loc) · 6.95 KB
/
R2DownloadResume_Tests.cs
File metadata and controls
163 lines (145 loc) · 6.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
using Autofac;
using ByteSync.Business.Actions.Shared;
using ByteSync.Business.Inventories;
using ByteSync.Client.IntegrationTests.TestHelpers;
using ByteSync.Client.IntegrationTests.TestHelpers.Http;
using ByteSync.Common.Business.Actions;
using ByteSync.Common.Business.Inventories;
using ByteSync.Common.Business.SharedFiles;
using ByteSync.TestsCommon;
using ByteSync.DependencyInjection;
using ByteSync.Interfaces.Controls.Communications.Http;
using ByteSync.Interfaces.Factories;
using ByteSync.Interfaces.Repositories;
using ByteSync.Interfaces.Services.Communications;
using ByteSync.Interfaces.Services.Sessions;
using ByteSync.ServerCommon.Business.Settings;
using ByteSync.ServerCommon.Interfaces.Services.Storage;
using ByteSync.ServerCommon.Interfaces.Services.Storage.Factories;
using ByteSync.ServerCommon.Services.Storage;
using ByteSync.ServerCommon.Services.Storage.Factories;
using ByteSync.Services.Communications.Transfers.Downloading;
using ByteSync.Services.Communications.Transfers.Uploading;
namespace ByteSync.Client.IntegrationTests.Services.Communications.Transfers;
public class R2DownloadResume_Tests : AbstractTester
{
private ILifetimeScope _clientScope = null!;
[SetUp]
public void SetUp()
{
CreateTestDirectory();
// ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract
if (ByteSync.Services.ContainerProvider.Container == null)
{
ServiceRegistrar.RegisterComponents();
}
_clientScope = ByteSync.Services.ContainerProvider.Container!.BeginLifetimeScope(b =>
{
// Simulate one GET 500 during download by replacing IHttpClientFactory used by strategies
b.RegisterInstance(new FlakyCounter(putFailures: 0, getFailures: 1)).As<IFlakyCounter>().SingleInstance();
b.RegisterType<FlakyHttpClientFactory>().As<IHttpClientFactory>().SingleInstance();
b.RegisterType<CloudflareR2ClientFactory>().As<ICloudflareR2ClientFactory>().SingleInstance();
b.RegisterType<CloudflareR2Service>().As<ICloudflareR2Service>().SingleInstance();
b.Register(_ => GlobalTestSetup.Container.Resolve<Microsoft.Extensions.Options.IOptions<CloudflareR2Settings>>())
.As<Microsoft.Extensions.Options.IOptions<CloudflareR2Settings>>();
b.RegisterType<R2FileTransferApiClient>().As<IFileTransferApiClient>().SingleInstance();
});
// Set AES key for encryption/decryption used by SlicerEncrypter
using var scope = _clientScope.BeginLifetimeScope();
var cloudSessionConnectionRepository = scope.Resolve<ICloudSessionConnectionRepository>();
cloudSessionConnectionRepository.SetAesEncryptionKey(AesGenerator.GenerateKey());
}
[TearDown]
public void TearDown()
{
_clientScope.Dispose();
if (TestDirectory?.Exists == true)
{
TestDirectory.Delete(true);
}
}
[Test]
[Category("Cloud")]
public async Task Download_WithTransientFailure_Should_Retry_And_Succeed()
{
// ReSharper disable once UseAwaitUsing
using var scope = _clientScope;
var uploaderFactory = scope.Resolve<IFileUploaderFactory>();
var downloaderFactory = scope.Resolve<IFileDownloaderFactory>();
var r2Service = scope.Resolve<ICloudflareR2Service>();
var sharedActionsGroupRepository = scope.Resolve<ISharedActionsGroupRepository>();
var sessionService = scope.Resolve<ISessionService>();
var connectionService = scope.Resolve<IConnectionService>();
var shared = new SharedFileDefinition
{
SessionId = "itests-" + Guid.NewGuid().ToString("N"),
ClientInstanceId = Guid.NewGuid().ToString("N"),
SharedFileType = SharedFileTypes.FullSynchronization,
IV = AesGenerator.GenerateIV()
};
// Minimal session/endPoint/context so DownloadTargetBuilder can resolve destinations
connectionService.CurrentEndPoint = new ByteSync.Common.Business.EndPoints.ByteSyncEndpoint
{
ClientInstanceId = shared.ClientInstanceId,
ClientId = shared.ClientInstanceId,
Version = "itests",
IpAddress = "127.0.0.1",
OSPlatform = Common.Business.Misc.OSPlatforms.Windows
};
await sessionService.SetSessionStatus(ByteSync.Business.Sessions.SessionStatus.Preparation);
var sag = new SharedActionsGroup
{
ActionsGroupId = Guid.NewGuid().ToString("N"),
SynchronizationType = SynchronizationTypes.Full,
Source = new SharedDataPart
{
ClientInstanceId = shared.ClientInstanceId,
RootPath = TestDirectory.FullName,
InventoryPartType = FileSystemTypes.File,
Name = "itests",
InventoryCodeAndId = "itests"
},
PathIdentity = new PathIdentity
{
FileSystemType = FileSystemTypes.File,
LinkingKeyValue = "itests"
}
};
sag.Targets.Add(new SharedDataPart
{
ClientInstanceId = shared.ClientInstanceId,
RootPath = Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N") + ".tmp"),
InventoryPartType = FileSystemTypes.File,
Name = "itests",
InventoryCodeAndId = "itests"
});
shared.ActionsGroupIds = [sag.ActionsGroupId];
sharedActionsGroupRepository.SetSharedActionsGroups([sag]);
// First upload a file so we can download it
var inputContent = new string('z', 1_000_000);
var tempFile = Path.Combine(TestDirectory.FullName, Guid.NewGuid().ToString("N") + ".tmp");
await File.WriteAllTextAsync(tempFile, inputContent);
var uploader = uploaderFactory.Build(tempFile, shared);
(uploader as FileUploader)!.MaxSliceLength = 1 * 1024 * 1024;
await uploader.Upload();
var downloader = downloaderFactory.Build(shared);
var expectedKeyPrefix = shared.SessionId + "_" + shared.ClientInstanceId + "_synchronization_" + shared.Id + ".part";
var objects = await r2Service.GetAllObjects(CancellationToken.None);
var partsCount = objects.Count(o => o.Key.StartsWith(expectedKeyPrefix));
await downloader.StartDownload();
for (int i = 1; i <= partsCount; i++)
{
await downloader.PartsCoordinator.AddAvailablePartAsync(i);
}
await downloader.PartsCoordinator.SetAllPartsKnownAsync(partsCount);
await downloader.WaitForFileFullyExtracted();
(downloader as FileDownloader)?.CleanupResources();
// Cleanup
var prefix = shared.SessionId + "_" + shared.ClientInstanceId + "_";
var all = await r2Service.GetAllObjects(CancellationToken.None);
foreach (var kvp in all.Where(o => o.Key.StartsWith(prefix)))
{
await r2Service.DeleteObjectByKey(kvp.Key, CancellationToken.None);
}
}
}