-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProviderRetryTests.cs
More file actions
184 lines (168 loc) · 6.29 KB
/
Copy pathProviderRetryTests.cs
File metadata and controls
184 lines (168 loc) · 6.29 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Linq;
using System.Reflection;
using Microsoft.Data.SqlClient;
using MySqlConnector;
using Npgsql;
using Microsoft.Data.Sqlite;
using Oracle.ManagedDataAccess.Client;
namespace DbaClientX.Tests;
public class ProviderRetryTests
{
private class MySqlRetryClient : DBAClientX.MySql
{
public T Run<T>(Func<T> operation) => ExecuteWithRetry(operation);
}
private static MySqlException CreateMySqlException(MySqlErrorCode code)
{
var ctor = typeof(MySqlException).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
null, new[] { typeof(MySqlErrorCode), typeof(string), typeof(string), typeof(Exception) }, null)!;
return (MySqlException)ctor.Invoke(new object?[] { code, null, string.Empty, null });
}
[Fact]
public void MySql_RetriesTransientErrors()
{
using var client = new MySqlRetryClient { MaxRetryAttempts = 3, RetryDelay = TimeSpan.Zero };
var exception = CreateMySqlException(MySqlErrorCode.LockDeadlock);
var attempts = 0;
var result = client.Run(() =>
{
if (++attempts < 3)
{
throw exception;
}
return 1;
});
Assert.Equal(1, result);
Assert.Equal(3, attempts);
}
private class PostgreSqlRetryClient : DBAClientX.PostgreSql
{
public T Run<T>(Func<T> operation) => ExecuteWithRetry(operation);
}
[Fact]
public void PostgreSql_RetriesTransientErrors()
{
using var client = new PostgreSqlRetryClient { MaxRetryAttempts = 3, RetryDelay = TimeSpan.Zero };
var exception = new PostgresException("msg", "S", "S", "40001");
var attempts = 0;
var result = client.Run(() =>
{
if (++attempts < 3)
{
throw exception;
}
return 1;
});
Assert.Equal(1, result);
Assert.Equal(3, attempts);
}
private class SqlServerRetryClient : DBAClientX.SqlServer
{
public T Run<T>(Func<T> operation) => ExecuteWithRetry(operation);
public bool IsConnectionOpenRetryable(Exception exception) => IsConnectionOpenTransient(exception);
public bool IsCommandRetryable(Exception exception) => IsTransient(exception);
}
private static SqlException CreateSqlException(int number)
{
var errorCtor = typeof(SqlError).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)
.First(c => c.GetParameters().Length == 8);
var error = errorCtor.Invoke(new object?[]
{
number, (byte)0, (byte)0, string.Empty, string.Empty, string.Empty, 1, null
});
var collection = (SqlErrorCollection)typeof(SqlErrorCollection).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0]
.Invoke(null);
typeof(SqlErrorCollection).GetMethod("Add", BindingFlags.NonPublic | BindingFlags.Instance)!
.Invoke(collection, new[] { error });
var ctor = typeof(SqlException).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)
.First(c => c.GetParameters().Length == 4);
return (SqlException)ctor.Invoke(new object?[] { "msg", collection, null, Guid.NewGuid() });
}
[Fact]
public void SqlServer_RetriesTransientErrors()
{
using var client = new SqlServerRetryClient { MaxRetryAttempts = 3, RetryDelay = TimeSpan.Zero };
var exception = CreateSqlException(1205);
var attempts = 0;
var result = client.Run(() =>
{
if (++attempts < 3)
{
throw exception;
}
return 1;
});
Assert.Equal(1, result);
Assert.Equal(3, attempts);
}
[Fact]
public void SqlServer_RetriesConnectionTimeoutsOnlyForConnectionOpen()
{
using var client = new SqlServerRetryClient();
var exception = CreateSqlException(-2);
Assert.True(client.IsConnectionOpenRetryable(exception));
Assert.False(client.IsCommandRetryable(exception));
}
private class SqliteRetryClient : DBAClientX.SQLite
{
public T Run<T>(Func<T> operation) => ExecuteWithRetry(operation);
}
[Fact]
public void Sqlite_RetriesTransientErrors()
{
using var client = new SqliteRetryClient { MaxRetryAttempts = 3, RetryDelay = TimeSpan.Zero };
var exception = new SqliteException("msg", 5);
var attempts = 0;
var result = client.Run(() =>
{
if (++attempts < 3)
{
throw exception;
}
return 1;
});
Assert.Equal(1, result);
Assert.Equal(3, attempts);
}
private class OracleRetryClient : DBAClientX.Oracle
{
public T Run<T>(Func<T> operation) => ExecuteWithRetry(operation);
}
private static OracleException CreateOracleException(int number)
{
var ctor = typeof(OracleException).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
null, new[] { typeof(int), typeof(string), typeof(string), typeof(string), typeof(int) }, null)!;
return (OracleException)ctor.Invoke(new object?[] { number, string.Empty, string.Empty, string.Empty, 0 });
}
[Fact]
public void Oracle_RetriesTransientErrors()
{
using var client = new OracleRetryClient { MaxRetryAttempts = 3, RetryDelay = TimeSpan.Zero };
var exception = CreateOracleException(12541);
var attempts = 0;
var result = client.Run(() =>
{
if (++attempts < 3)
{
throw exception;
}
return 1;
});
Assert.Equal(1, result);
Assert.Equal(3, attempts);
}
[Fact]
public void Oracle_DoesNotRetry_NonTransientErrors()
{
using var client = new OracleRetryClient { MaxRetryAttempts = 3, RetryDelay = TimeSpan.Zero };
var exception = CreateOracleException(942);
var attempts = 0;
Assert.Throws<OracleException>(() => client.Run<int>(() =>
{
attempts++;
throw exception;
}));
Assert.Equal(1, attempts);
}
}