-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathDownloadRobustnessTests.cs
More file actions
147 lines (123 loc) · 4.46 KB
/
Copy pathDownloadRobustnessTests.cs
File metadata and controls
147 lines (123 loc) · 4.46 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
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using GeneralUpdate.Core.Download.Abstractions;
using GeneralUpdate.Core.Download.Executors;
using GeneralUpdate.Core.Download.Models;
using GeneralUpdate.Core.Download.Policy;
using Xunit;
namespace CoreTest.Download;
public class DownloadRobustnessTests
{
[Fact]
public async Task DefaultRetryPolicy_RetriesOnTransientFailure()
{
var policy = new DefaultRetryPolicy(maxRetries: 3, initialDelay: TimeSpan.FromMilliseconds(1));
int attempts = 0;
var result = await policy.ExecuteAsync(async _ =>
{
attempts++;
if (attempts < 3) throw new HttpRequestException("timeout");
return "ok";
}, CancellationToken.None);
Assert.Equal("ok", result);
Assert.Equal(3, attempts);
}
[Fact]
public async Task DefaultRetryPolicy_DoesNotRetryOnPermanentFailure()
{
var policy = new DefaultRetryPolicy(maxRetries: 3, initialDelay: TimeSpan.FromMilliseconds(1));
int attempts = 0;
await Assert.ThrowsAsync<HttpRequestException>(() =>
policy.ExecuteAsync<string>(_ =>
{
attempts++;
throw new HttpRequestException("404 Not Found");
}, CancellationToken.None));
Assert.Equal(1, attempts);
}
[Fact]
public async Task DefaultRetryPolicy_ExponentialBackoff()
{
var policy = new DefaultRetryPolicy(maxRetries: 3, initialDelay: TimeSpan.FromMilliseconds(10), backoffMultiplier: 2.0);
int attempts = 0;
var start = DateTime.UtcNow;
await Assert.ThrowsAsync<HttpRequestException>(() =>
policy.ExecuteAsync<string>(_ =>
{
attempts++;
throw new HttpRequestException("timeout");
}, CancellationToken.None));
var elapsed = DateTime.UtcNow - start;
// 3 attempts = 2 retries: delay 10ms + 20ms = at least 30ms
Assert.InRange(elapsed.TotalMilliseconds, 25, 500);
Assert.Equal(3, attempts);
}
[Fact]
public async Task RetryPolicy_RespectsCancellation()
{
var policy = new DefaultRetryPolicy(maxRetries: 5, initialDelay: TimeSpan.FromSeconds(1));
using var cts = new CancellationTokenSource(TimeSpan.FromMilliseconds(50));
await Assert.ThrowsAsync<TaskCanceledException>(() =>
policy.ExecuteAsync<string>(_ =>
{
throw new HttpRequestException("timeout");
}, cts.Token));
}
[Fact]
public async Task RetryPolicy_RetriesOnIOException()
{
var policy = new DefaultRetryPolicy(maxRetries: 2, initialDelay: TimeSpan.FromMilliseconds(1));
int attempts = 0;
var result = await policy.ExecuteAsync(async _ =>
{
attempts++;
if (attempts < 2) throw new IOException("Network stream error");
return "recovered";
}, CancellationToken.None);
Assert.Equal("recovered", result);
Assert.Equal(2, attempts);
}
[Fact]
public async Task RetryPolicy_ReturnsOnSuccessFirstTry()
{
var policy = new DefaultRetryPolicy(maxRetries: 3, initialDelay: TimeSpan.FromMilliseconds(1));
int attempts = 0;
var result = await policy.ExecuteAsync(_ =>
{
attempts++;
return Task.FromResult("first");
}, CancellationToken.None);
Assert.Equal("first", result);
Assert.Equal(1, attempts);
}
[Fact]
public async Task DefaultRetryPolicy_RetryableStatusCodes()
{
var policy = new DefaultRetryPolicy(maxRetries: 3);
int attempts = 0;
await Assert.ThrowsAsync<HttpRequestException>(() =>
policy.ExecuteAsync<string>(_ =>
{
attempts++;
throw new HttpRequestException("503 Service Unavailable");
}, CancellationToken.None));
Assert.Equal(3, attempts);
}
[Fact]
public async Task DefaultRetryPolicy_NoRetryOn402()
{
var policy = new DefaultRetryPolicy(maxRetries: 3);
int attempts = 0;
await Assert.ThrowsAsync<HttpRequestException>(() =>
policy.ExecuteAsync<string>(_ =>
{
attempts++;
throw new HttpRequestException("402 Payment Required");
}, CancellationToken.None));
Assert.Equal(1, attempts);
}
}