-
Notifications
You must be signed in to change notification settings - Fork 336
Expand file tree
/
Copy pathSqlConfigurableRetryLogicTest.cs
More file actions
130 lines (114 loc) · 6.11 KB
/
Copy pathSqlConfigurableRetryLogicTest.cs
File metadata and controls
130 lines (114 loc) · 6.11 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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.IO;
using System.Reflection;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.Data.SqlClient.Tests
{
public class SqlConfigurableRetryLogicTest
{
[Fact]
public async Task InvalidExecute()
{
SqlRetryLogicOption option = new SqlRetryLogicOption()
{
NumberOfTries = 5,
DeltaTime = TimeSpan.FromSeconds(10),
MinTimeInterval = TimeSpan.Zero,
MaxTimeInterval = TimeSpan.FromSeconds(120)
};
SqlRetryLogicBaseProvider retryLogicProvider = SqlConfigurableRetryFactory.CreateFixedRetryProvider(option);
Assert.Throws<ArgumentNullException>(() => retryLogicProvider.Execute<int>(null, null));
await Assert.ThrowsAsync<ArgumentNullException>(() => retryLogicProvider.ExecuteAsync(null, null));
await Assert.ThrowsAsync<ArgumentNullException>(() => retryLogicProvider.ExecuteAsync<int>(null, null));
}
[Fact]
public void InvalidCRLFactoryCreation()
{
Assert.Throws<ArgumentNullException>(() => SqlConfigurableRetryFactory.CreateFixedRetryProvider(null));
Assert.Throws<ArgumentNullException>(() => SqlConfigurableRetryFactory.CreateIncrementalRetryProvider(null));
Assert.Throws<ArgumentNullException>(() => SqlConfigurableRetryFactory.CreateExponentialRetryProvider(null));
}
[Fact]
public void ValidateRetryParameters()
{
var option = new SqlRetryLogicOption()
{
NumberOfTries = 10, // 1-60
MinTimeInterval = TimeSpan.FromMinutes(0), // 0-120
MaxTimeInterval = TimeSpan.FromSeconds(120), // 0-120
DeltaTime = TimeSpan.FromSeconds(1) // 0-120
};
option.NumberOfTries = 0;
Assert.Throws<ArgumentOutOfRangeException>(() => SqlConfigurableRetryFactory.CreateFixedRetryProvider(option));
option.NumberOfTries = 61;
Assert.Throws<ArgumentOutOfRangeException>(() => SqlConfigurableRetryFactory.CreateFixedRetryProvider(option));
option.NumberOfTries = 10;
option.DeltaTime = TimeSpan.FromSeconds(-1);
Assert.Throws<ArgumentOutOfRangeException>(() => SqlConfigurableRetryFactory.CreateFixedRetryProvider(option));
option.DeltaTime = TimeSpan.FromSeconds(121);
Assert.Throws<ArgumentOutOfRangeException>(() => SqlConfigurableRetryFactory.CreateFixedRetryProvider(option));
option.DeltaTime = TimeSpan.FromSeconds(1);
option.MinTimeInterval = TimeSpan.FromSeconds(-1);
Assert.Throws<ArgumentOutOfRangeException>(() => SqlConfigurableRetryFactory.CreateIncrementalRetryProvider(option));
option.MinTimeInterval = TimeSpan.FromSeconds(121);
Assert.Throws<ArgumentOutOfRangeException>(() => SqlConfigurableRetryFactory.CreateIncrementalRetryProvider(option));
option.MinTimeInterval = TimeSpan.FromSeconds(0);
option.MaxTimeInterval = TimeSpan.FromSeconds(-1);
Assert.Throws<ArgumentOutOfRangeException>(() => SqlConfigurableRetryFactory.CreateIncrementalRetryProvider(option));
option.MaxTimeInterval = TimeSpan.FromSeconds(121);
Assert.Throws<ArgumentOutOfRangeException>(() => SqlConfigurableRetryFactory.CreateIncrementalRetryProvider(option));
option.MinTimeInterval = TimeSpan.FromSeconds(50);
option.MaxTimeInterval = TimeSpan.FromSeconds(40);
Assert.Throws<ArgumentOutOfRangeException>(() => SqlConfigurableRetryFactory.CreateIncrementalRetryProvider(option));
option.MinTimeInterval = TimeSpan.FromSeconds(0);
option.MaxTimeInterval = TimeSpan.FromSeconds(120);
option.AuthorizedSqlCondition = null;
SqlConfigurableRetryFactory.CreateIncrementalRetryProvider(option);
}
#if NET
/// <summary>
/// Regression test: triggering the configurable retry logic loader through its normal
/// entry points must not leave a process-wide
/// <see cref="System.Runtime.Loader.AssemblyLoadContext.Default"/> resolving handler
/// installed. Such a handler participates in resolution of every assembly the host
/// application fails to find, and serves them out of this component's probing directory,
/// which can load code from an unintended location.
/// </summary>
[Fact]
public void RetryLogicProviderDoesNotLeaveAssemblyProbingEnabled()
{
// Touch the default retry logic providers to force SqlConfigurableRetryLogicLoader
// construction via its normal code path.
Assert.NotNull(new SqlCommand().RetryLogicProvider);
Assert.NotNull(new SqlConnection().RetryLogicProvider);
// A file that is not a valid assembly, planted in the loader's probing directory
// under a name no other component could be asking for. If a handler is still
// subscribed it finds this file and fails with BadImageFormatException. With correct
// behavior the runtime never looks here and reports the assembly as simply not found.
string assemblySimpleName = "MdsProbeAssembly_" + Guid.NewGuid().ToString("N");
string plantedFile = Path.Combine(AppContext.BaseDirectory, assemblySimpleName + ".dll");
File.WriteAllText(plantedFile, "not an assembly");
try
{
Assert.Throws<FileNotFoundException>(
() => Assembly.Load(new AssemblyName(assemblySimpleName)));
}
finally
{
try
{
File.Delete(plantedFile);
}
catch (IOException)
{
// Best effort cleanup.
}
}
}
#endif
}
}